diff --git a/.changeset/nasty-trains-invite.md b/.changeset/nasty-trains-invite.md new file mode 100644 index 000000000000..48f3cb1c84d6 --- /dev/null +++ b/.changeset/nasty-trains-invite.md @@ -0,0 +1,7 @@ +--- +'astro': patch +--- + +Fixes a regression that was introduced by an internal refactor of how the middleware is loaded by the Astro application. The regression was introduced by [#11550](https://github.com/withastro/astro/pull/11550). + +When the edge middleware feature is opted in, Astro removes the middleware function from the SSR manifest, and this wasn't taken into account during the refactor. diff --git a/packages/astro/src/core/app/types.ts b/packages/astro/src/core/app/types.ts index 73ddea268d9a..66035e4d5697 100644 --- a/packages/astro/src/core/app/types.ts +++ b/packages/astro/src/core/app/types.ts @@ -68,7 +68,7 @@ export type SSRManifest = { serverIslandNameMap?: Map; key: Promise; i18n: SSRManifestI18n | undefined; - middleware: () => Promise | AstroMiddlewareInstance; + middleware?: () => Promise | AstroMiddlewareInstance; checkOrigin: boolean; // TODO: remove experimental prefix experimentalEnvGetSecretEnabled: boolean; diff --git a/packages/astro/src/core/base-pipeline.ts b/packages/astro/src/core/base-pipeline.ts index 8a448133ab3d..b1fc5568e0a6 100644 --- a/packages/astro/src/core/base-pipeline.ts +++ b/packages/astro/src/core/base-pipeline.ts @@ -109,7 +109,10 @@ export abstract class Pipeline { async getMiddleware(): Promise { if (this.resolvedMiddleware) { return this.resolvedMiddleware; - } else { + } + // The middleware can be undefined when using edge middleware. + // This is set to undefined by the plugin-ssr.ts + else if (this.middleware) { const middlewareInstance = await this.middleware(); const onRequest = middlewareInstance.onRequest ?? NOOP_MIDDLEWARE_FN; if (this.manifest.checkOrigin) { @@ -118,6 +121,9 @@ export abstract class Pipeline { this.resolvedMiddleware = onRequest; } return this.resolvedMiddleware; + } else { + this.resolvedMiddleware = NOOP_MIDDLEWARE_FN; + return this.resolvedMiddleware } } }