diff --git a/.changeset/bright-crabs-kick.md b/.changeset/bright-crabs-kick.md new file mode 100644 index 000000000000..c94a34e9cf9d --- /dev/null +++ b/.changeset/bright-crabs-kick.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a bug where the middleware was incorrectly imported during the build diff --git a/packages/astro/src/core/build/pipeline.ts b/packages/astro/src/core/build/pipeline.ts index 14bb08662a98..3908dffed238 100644 --- a/packages/astro/src/core/build/pipeline.ts +++ b/packages/astro/src/core/build/pipeline.ts @@ -131,11 +131,11 @@ export class BuildPipeline extends Pipeline { const renderers = await import(renderersEntryUrl.toString()); const middleware = internals.middlewareEntryPoint - ? await import(internals.middlewareEntryPoint.toString()).then((mod) => { - return function () { - return { onRequest: mod.onRequest }; - }; - }) + ? async function () { + // @ts-expect-error: the compiler can't understand the previous check + const mod = await import(internals.middlewareEntryPoint.toString()); + return { onRequest: mod.onRequest }; + } : manifest.middleware; if (!renderers) { diff --git a/packages/astro/test/fixtures/middleware-full-ssr/package.json b/packages/astro/test/fixtures/middleware-full-ssr/package.json new file mode 100644 index 000000000000..4f8b625c0b2c --- /dev/null +++ b/packages/astro/test/fixtures/middleware-full-ssr/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/middleware-full-ssr", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/middleware-full-ssr/src/error.js b/packages/astro/test/fixtures/middleware-full-ssr/src/error.js new file mode 100644 index 000000000000..6eed03a82c07 --- /dev/null +++ b/packages/astro/test/fixtures/middleware-full-ssr/src/error.js @@ -0,0 +1 @@ +throw new Error("Shoud not error at build time") diff --git a/packages/astro/test/fixtures/middleware-full-ssr/src/middleware.js b/packages/astro/test/fixtures/middleware-full-ssr/src/middleware.js new file mode 100644 index 000000000000..0061049c13a6 --- /dev/null +++ b/packages/astro/test/fixtures/middleware-full-ssr/src/middleware.js @@ -0,0 +1,2 @@ +import "./error.js" +export const onRequest = (_ , next) => next(); diff --git a/packages/astro/test/fixtures/middleware-full-ssr/src/pages/index.astro b/packages/astro/test/fixtures/middleware-full-ssr/src/pages/index.astro new file mode 100644 index 000000000000..395a4d695cfa --- /dev/null +++ b/packages/astro/test/fixtures/middleware-full-ssr/src/pages/index.astro @@ -0,0 +1,14 @@ +--- +const data = Astro.locals; +--- + + +
+{data?.name}
+ + diff --git a/packages/astro/test/middleware.test.js b/packages/astro/test/middleware.test.js index 26bc06d77f47..c7c95b5b6645 100644 --- a/packages/astro/test/middleware.test.js +++ b/packages/astro/test/middleware.test.js @@ -171,6 +171,21 @@ describe('Middleware in PROD mode, SSG', () => { }); }); +describe('Middleware should not be executed or imported during', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + it('should build the project without errors', async () => { + fixture = await loadFixture({ + root: './fixtures/middleware-full-ssr/', + output: 'server', + adapter: testAdapter({}), + }); + await fixture.build(); + assert.ok('Should build'); + }); +}); + describe('Middleware API in PROD mode, SSR', () => { /** @type {import('./test-utils').Fixture} */ let fixture; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0270d490a29b..a351d308082f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3324,6 +3324,12 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/middleware-full-ssr: + dependencies: + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/middleware-no-user-middleware: dependencies: astro: