From 2aaed2d2a96ab35461af24e8d12b20f1da33983f Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 10 Dec 2024 20:48:38 +0000 Subject: [PATCH] fix(middleware): don't import via entrypoint (#12707) * fix(middleware): don't import via entrypoint * fix(middleware): don't import via entrypoint --- .changeset/bright-crabs-kick.md | 5 +++++ packages/astro/src/core/build/pipeline.ts | 10 +++++----- .../fixtures/middleware-full-ssr/package.json | 8 ++++++++ .../fixtures/middleware-full-ssr/src/error.js | 1 + .../middleware-full-ssr/src/middleware.js | 2 ++ .../middleware-full-ssr/src/pages/index.astro | 14 ++++++++++++++ packages/astro/test/middleware.test.js | 15 +++++++++++++++ pnpm-lock.yaml | 6 ++++++ 8 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 .changeset/bright-crabs-kick.md create mode 100644 packages/astro/test/fixtures/middleware-full-ssr/package.json create mode 100644 packages/astro/test/fixtures/middleware-full-ssr/src/error.js create mode 100644 packages/astro/test/fixtures/middleware-full-ssr/src/middleware.js create mode 100644 packages/astro/test/fixtures/middleware-full-ssr/src/pages/index.astro diff --git a/.changeset/bright-crabs-kick.md b/.changeset/bright-crabs-kick.md new file mode 100644 index 0000000000..c94a34e9cf --- /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 14bb08662a..3908dffed2 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 0000000000..4f8b625c0b --- /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 0000000000..6eed03a82c --- /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 0000000000..0061049c13 --- /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 0000000000..395a4d695c --- /dev/null +++ b/packages/astro/test/fixtures/middleware-full-ssr/src/pages/index.astro @@ -0,0 +1,14 @@ +--- +const data = Astro.locals; +--- + + + + Testing + + + + Index +

{data?.name}

+ + diff --git a/packages/astro/test/middleware.test.js b/packages/astro/test/middleware.test.js index 26bc06d77f..c7c95b5b66 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 0270d490a2..a351d30808 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: