sequence() — astro Function Reference
Architecture documentation for the sequence() function in sequence.ts from the astro codebase.
Entity Profile
Dependency Diagram
graph TD f687a44a_26fd_4ffb_5c19_c4d1256a45d5["sequence()"] 16288903_f811_ef4a_1a6e_6d77b1db7c8b["sequence.ts"] f687a44a_26fd_4ffb_5c19_c4d1256a45d5 -->|defined in| 16288903_f811_ef4a_1a6e_6d77b1db7c8b style f687a44a_26fd_4ffb_5c19_c4d1256a45d5 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/astro/src/core/middleware/sequence.ts lines 15–97
export function sequence(...handlers: MiddlewareHandler[]): MiddlewareHandler {
const filtered = handlers.filter((h) => !!h);
const length = filtered.length;
if (!length) {
return defineMiddleware((_context, next) => {
return next();
});
}
return defineMiddleware((context, next) => {
/**
* This variable is used to carry the rerouting payload across middleware functions.
*/
let carriedPayload: RewritePayload | undefined = undefined;
return applyHandle(0, context);
function applyHandle(i: number, handleContext: APIContext) {
const handle = filtered[i];
// @ts-expect-error
// SAFETY: Usually `next` always returns something in user land, but in `sequence` we are actually
// doing a loop over all the `next` functions, and eventually we call the last `next` that returns the `Response`.
const result = handle(handleContext, async (payload?: RewritePayload) => {
if (i < length - 1) {
if (payload) {
let newRequest;
if (payload instanceof Request) {
newRequest = payload;
} else if (payload instanceof URL) {
// Cloning the original request ensures that the new Request gets its own copy of the body stream
// Without this it will throw an error if they both try to consume the stream, which will happen in a rewrite
newRequest = new Request(payload, handleContext.request.clone());
} else {
newRequest = new Request(
new URL(payload, handleContext.url.origin),
handleContext.request.clone(),
);
}
const oldPathname = handleContext.url.pathname;
const pipeline: Pipeline = Reflect.get(handleContext, pipelineSymbol);
const { routeData, pathname } = await pipeline.tryRewrite(
payload,
handleContext.request,
);
// This is a case where the user tries to rewrite from a SSR route to a prerendered route (SSG).
// This case isn't valid because when building for SSR, the prerendered route disappears from the server output because it becomes an HTML file,
// so Astro can't retrieve it from the emitted manifest.
if (
pipeline.manifest.serverLike === true &&
handleContext.isPrerendered === false &&
routeData.prerender === true
) {
throw new AstroError({
...ForbiddenRewrite,
message: ForbiddenRewrite.message(
handleContext.url.pathname,
pathname,
routeData.component,
),
hint: ForbiddenRewrite.hint(routeData.component),
});
}
carriedPayload = payload;
handleContext.request = newRequest;
handleContext.url = new URL(newRequest.url);
handleContext.params = getParams(routeData, pathname);
handleContext.routePattern = routeData.route;
setOriginPathname(
handleContext.request,
oldPathname,
pipeline.manifest.trailingSlash,
pipeline.manifest.buildFormat,
);
}
return applyHandle(i + 1, handleContext);
} else {
return next(payload ?? carriedPayload);
}
});
return result;
}
Domain
Subdomains
Source
Frequently Asked Questions
What does sequence() do?
sequence() is a function in the astro codebase, defined in packages/astro/src/core/middleware/sequence.ts.
Where is sequence() defined?
sequence() is defined in packages/astro/src/core/middleware/sequence.ts at line 15.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free