MCPcopy
hub / github.com/withastro/astro / applyRewriteToState

Function applyRewriteToState

packages/astro/src/core/rewrites/handler.ts:34–96  ·  view source on GitHub ↗
(
	state: FetchState,
	payload: RewritePayload,
	{ routeData, componentInstance, newUrl, pathname }: TryRewriteResult,
	{ mergeCookies = false }: { mergeCookies?: boolean } = {},
)

Source from the content-addressed store, hash-verified

32 * and `AstroMiddleware` (middleware `next(payload)`).
33 */
34export function applyRewriteToState(
35 state: FetchState,
36 payload: RewritePayload,
37 { routeData, componentInstance, newUrl, pathname }: TryRewriteResult,
38 { mergeCookies = false }: { mergeCookies?: boolean } = {},
39): void {
40 const pipeline = state.pipeline;
41 const oldPathname = state.pathname;
42
43 // Disallow SSR→prerender rewrites: the prerendered route becomes a
44 // static HTML file during build and isn't available in the server
45 // manifest. Allow i18n fallback routes as an exception.
46 const isI18nFallback = routeData.fallbackRoutes && routeData.fallbackRoutes.length > 0;
47 if (
48 pipeline.manifest.serverLike &&
49 !state.routeData!.prerender &&
50 routeData.prerender &&
51 !isI18nFallback
52 ) {
53 throw new AstroError({
54 ...ForbiddenRewrite,
55 message: ForbiddenRewrite.message(state.pathname, pathname, routeData.component),
56 hint: ForbiddenRewrite.hint(routeData.component),
57 });
58 }
59
60 state.routeData = routeData;
61 state.componentInstance = componentInstance;
62 if (payload instanceof Request) {
63 state.request = payload;
64 } else {
65 state.request = copyRequest(
66 newUrl,
67 state.request,
68 routeData.prerender,
69 pipeline.logger,
70 state.routeData!.route,
71 );
72 }
73 state.url = createNormalizedUrl(state.request.url);
74 if (mergeCookies) {
75 const newCookies = new AstroCookies(state.request);
76 if (state.cookies) {
77 newCookies.merge(state.cookies);
78 }
79 state.cookies = newCookies;
80 }
81 state.params = getParams(routeData, pathname);
82 state.pathname = pathname;
83 state.isRewriting = true;
84 state.status = 200;
85
86 setOriginPathname(
87 state.request,
88 oldPathname,
89 pipeline.manifest.trailingSlash,
90 pipeline.manifest.buildFormat,
91 );

Callers 3

nextMethod · 0.85
executeMethod · 0.85

Calls 6

mergeMethod · 0.95
copyRequestFunction · 0.85
createNormalizedUrlFunction · 0.85
getParamsFunction · 0.85
setOriginPathnameFunction · 0.85
invalidateContextsMethod · 0.80

Tested by

no test coverage detected