From 065ed83982c8d7048977e922fb0c95aa249b22d8 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Fri, 10 Nov 2023 13:34:19 +0000 Subject: [PATCH 1/4] fix(intergration defined middleware): prevent importing non-existent user middleware --- .../astro/src/core/middleware/vite-plugin.ts | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/astro/src/core/middleware/vite-plugin.ts b/packages/astro/src/core/middleware/vite-plugin.ts index 790c76129cf3..47eaf44b4693 100644 --- a/packages/astro/src/core/middleware/vite-plugin.ts +++ b/packages/astro/src/core/middleware/vite-plugin.ts @@ -59,19 +59,29 @@ export function vitePluginMiddleware({ settings }: { settings: AstroSettings }): const preMiddleware = createMiddlewareImports(settings.middlewares.pre, 'pre'); const postMiddleware = createMiddlewareImports(settings.middlewares.post, 'post'); + const userMiddleware = { + importsCode: resolvedMiddlewareId ? `import { onRequest as userOnRequest } from '${resolvedMiddlewareId}';` : undefined, + sequenceCode: resolvedMiddlewareId ? 'userOnRequest' : undefined + }; + + const importsCode = [ + preMiddleware.importsCode, + userMiddleware.importsCode, + postMiddleware.importsCode + ].filter(Boolean).join('\n'); + + const sequenceCode = [ + preMiddleware.sequenceCode, + userMiddleware.sequenceCode, + postMiddleware.sequenceCode + ].filter(Boolean).join(', '); const source = ` -import { onRequest as userOnRequest } from '${resolvedMiddlewareId}'; import { sequence } from 'astro:middleware'; -${preMiddleware.importsCode}${postMiddleware.importsCode} +${importsCode} -export const onRequest = sequence( - ${preMiddleware.sequenceCode}${preMiddleware.sequenceCode ? ',' : ''} - userOnRequest${postMiddleware.sequenceCode ? ',' : ''} - ${postMiddleware.sequenceCode} -); +export const onRequest = sequence(${sequenceCode}); `.trim(); - return source; } }, From 6ab188a0a6099c11a6864ddfe22ecb3e62613e2a Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Fri, 10 Nov 2023 13:52:35 +0000 Subject: [PATCH 2/4] add test --- .../astro.config.mjs | 23 +++++++++++++++++ .../integration-middleware-post.js | 13 ++++++++++ .../integration-middleware-pre.js | 13 ++++++++++ .../src/pages/404.astro | 1 + packages/astro/test/middleware.test.js | 25 +++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 packages/astro/test/fixtures/middleware-from-integration/astro.config.mjs create mode 100644 packages/astro/test/fixtures/middleware-from-integration/integration-middleware-post.js create mode 100644 packages/astro/test/fixtures/middleware-from-integration/integration-middleware-pre.js create mode 100644 packages/astro/test/fixtures/middleware-from-integration/src/pages/404.astro diff --git a/packages/astro/test/fixtures/middleware-from-integration/astro.config.mjs b/packages/astro/test/fixtures/middleware-from-integration/astro.config.mjs new file mode 100644 index 000000000000..3fee066cda47 --- /dev/null +++ b/packages/astro/test/fixtures/middleware-from-integration/astro.config.mjs @@ -0,0 +1,23 @@ +import { defineConfig } from 'astro/config'; +import { fileURLToPath } from 'node:url'; + +export default defineConfig({ + integrations: [ + { + name: 'my-middleware', + hooks: { + 'astro:config:setup':({ addMiddleware }) => { + addMiddleware({ + entrypoint: fileURLToPath(new URL('./integration-middleware-pre.js', import.meta.url)), + order: 'pre' + }); + + addMiddleware({ + entrypoint: fileURLToPath(new URL('./integration-middleware-post.js', import.meta.url)), + order: 'post' + }); + } + } + } + ] +}); diff --git a/packages/astro/test/fixtures/middleware-from-integration/integration-middleware-post.js b/packages/astro/test/fixtures/middleware-from-integration/integration-middleware-post.js new file mode 100644 index 000000000000..4cc63c6b7c49 --- /dev/null +++ b/packages/astro/test/fixtures/middleware-from-integration/integration-middleware-post.js @@ -0,0 +1,13 @@ +import { sequence, defineMiddleware } from 'astro:middleware'; + +export const onRequest = defineMiddleware((context, next) => { + if(context.url.pathname === '/integration-post') { + return new Response(JSON.stringify({ post: 'works' }), { + headers: { + 'content-type': 'application/json' + } + }); + } + + return next(); +}); diff --git a/packages/astro/test/fixtures/middleware-from-integration/integration-middleware-pre.js b/packages/astro/test/fixtures/middleware-from-integration/integration-middleware-pre.js new file mode 100644 index 000000000000..3bf484b2becb --- /dev/null +++ b/packages/astro/test/fixtures/middleware-from-integration/integration-middleware-pre.js @@ -0,0 +1,13 @@ +import { sequence, defineMiddleware } from 'astro:middleware'; + +export const onRequest = defineMiddleware((context, next) => { + if(context.url.pathname === '/integration-pre') { + return new Response(JSON.stringify({ pre: 'works' }), { + headers: { + 'content-type': 'application/json' + } + }); + } + + return next(); +}); diff --git a/packages/astro/test/fixtures/middleware-from-integration/src/pages/404.astro b/packages/astro/test/fixtures/middleware-from-integration/src/pages/404.astro new file mode 100644 index 000000000000..ed7ca80fa2b8 --- /dev/null +++ b/packages/astro/test/fixtures/middleware-from-integration/src/pages/404.astro @@ -0,0 +1 @@ +

404.astro allows the middleware to run for every request

diff --git a/packages/astro/test/middleware.test.js b/packages/astro/test/middleware.test.js index a9487950eb65..bfd6ebb7275e 100644 --- a/packages/astro/test/middleware.test.js +++ b/packages/astro/test/middleware.test.js @@ -101,6 +101,31 @@ describe('Middleware in DEV mode', () => { }); }); +describe('Integration hooks', () => { + + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/middleware-from-integration/', + }); + await fixture.build(); + }); + + it('Integration middleware marked as "pre" runs', async () => { + let res = await fixture.fetch('/integration-pre'); + let json = await res.json(); + expect(json.pre).to.equal('works'); + }); + + it('Integration middleware marked as "post" runs', async () => { + let res = await fixture.fetch('/integration-post'); + let json = await res.json(); + expect(json.post).to.equal('works'); + }); +}) + describe('Middleware in PROD mode, SSG', () => { /** @type {import('./test-utils').Fixture} */ let fixture; From c3f935d2cb1bf5ec4c0dd95336570914fc8a9970 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Fri, 10 Nov 2023 13:53:45 +0000 Subject: [PATCH 3/4] add changeset --- .changeset/clean-cars-drive.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/clean-cars-drive.md diff --git a/.changeset/clean-cars-drive.md b/.changeset/clean-cars-drive.md new file mode 100644 index 000000000000..360feeca90ca --- /dev/null +++ b/.changeset/clean-cars-drive.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes an issue where integration defined middleware failed when user defined middleware was not present. From f65f700f0606bb022ff71306e162020ded7106c5 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Fri, 10 Nov 2023 14:20:25 +0000 Subject: [PATCH 4/4] fix tests --- .../middleware-from-integration/package.json | 8 ++++++++ packages/astro/test/middleware.test.js | 12 +++++++++--- pnpm-lock.yaml | 6 ++++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 packages/astro/test/fixtures/middleware-from-integration/package.json diff --git a/packages/astro/test/fixtures/middleware-from-integration/package.json b/packages/astro/test/fixtures/middleware-from-integration/package.json new file mode 100644 index 000000000000..3232e0eec381 --- /dev/null +++ b/packages/astro/test/fixtures/middleware-from-integration/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/middleware-from-integration", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/middleware.test.js b/packages/astro/test/middleware.test.js index bfd6ebb7275e..6dd031e168b9 100644 --- a/packages/astro/test/middleware.test.js +++ b/packages/astro/test/middleware.test.js @@ -106,21 +106,27 @@ describe('Integration hooks', () => { /** @type {import('./test-utils').Fixture} */ let fixture; + /** @type {import('../src/core/app/index').App} */ + let app; + before(async () => { fixture = await loadFixture({ root: './fixtures/middleware-from-integration/', + output: 'server', + adapter: testAdapter({}), }); await fixture.build(); + app = await fixture.loadTestAdapterApp(); }); - + it('Integration middleware marked as "pre" runs', async () => { - let res = await fixture.fetch('/integration-pre'); + let res = await app.render(new Request('http://example.com/integration-pre')); let json = await res.json(); expect(json.pre).to.equal('works'); }); it('Integration middleware marked as "post" runs', async () => { - let res = await fixture.fetch('/integration-post'); + let res = await app.render(new Request('http://example.com/integration-post')); let json = await res.json(); expect(json.post).to.equal('works'); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cf80efc75923..c14e31d14a52 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2957,6 +2957,12 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/middleware-from-integration: + dependencies: + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/middleware-ssg: dependencies: astro: