diff --git a/.changeset/chilled-swans-appear.md b/.changeset/chilled-swans-appear.md new file mode 100644 index 000000000000..af7b37da6deb --- /dev/null +++ b/.changeset/chilled-swans-appear.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Correctly infer the presence of an user middleware diff --git a/packages/astro/src/core/middleware/vite-plugin.ts b/packages/astro/src/core/middleware/vite-plugin.ts index 790c76129cf3..0447134910f0 100644 --- a/packages/astro/src/core/middleware/vite-plugin.ts +++ b/packages/astro/src/core/middleware/vite-plugin.ts @@ -15,6 +15,7 @@ export function vitePluginMiddleware({ settings }: { settings: AstroSettings }): let resolvedMiddlewareId: string | undefined = undefined; const hasIntegrationMiddleware = settings.middlewares.pre.length > 0 || settings.middlewares.post.length > 0; + let userMiddlewareIsPresent = false; return { name: '@astro/plugin-middleware', @@ -29,6 +30,7 @@ export function vitePluginMiddleware({ settings }: { settings: AstroSettings }): const middlewareId = await this.resolve( `${decodeURI(settings.config.srcDir.pathname)}${MIDDLEWARE_PATH_SEGMENT_NAME}` ); + userMiddlewareIsPresent = !!middlewareId; if (middlewareId) { resolvedMiddlewareId = middlewareId.id; return MIDDLEWARE_MODULE_ID; @@ -61,13 +63,17 @@ export function vitePluginMiddleware({ settings }: { settings: AstroSettings }): const postMiddleware = createMiddlewareImports(settings.middlewares.post, 'post'); const source = ` -import { onRequest as userOnRequest } from '${resolvedMiddlewareId}'; + ${ + userMiddlewareIsPresent + ? `import { onRequest as userOnRequest } from '${resolvedMiddlewareId}';` + : '' + } import { sequence } from 'astro:middleware'; ${preMiddleware.importsCode}${postMiddleware.importsCode} export const onRequest = sequence( ${preMiddleware.sequenceCode}${preMiddleware.sequenceCode ? ',' : ''} - userOnRequest${postMiddleware.sequenceCode ? ',' : ''} + ${userMiddlewareIsPresent ? `userOnRequest${postMiddleware.sequenceCode ? ',' : ''}` : ''} ${postMiddleware.sequenceCode} ); `.trim(); diff --git a/packages/astro/test/fixtures/middleware space/integration-middleware-post.js b/packages/astro/test/fixtures/middleware space/integration-middleware-post.js index 4cc63c6b7c49..a4e0c90ad9b6 100644 --- a/packages/astro/test/fixtures/middleware space/integration-middleware-post.js +++ b/packages/astro/test/fixtures/middleware space/integration-middleware-post.js @@ -1,11 +1,11 @@ -import { sequence, defineMiddleware } from 'astro:middleware'; +import { defineMiddleware } from 'astro:middleware'; export const onRequest = defineMiddleware((context, next) => { - if(context.url.pathname === '/integration-post') { + if (context.url.pathname === '/integration-post') { return new Response(JSON.stringify({ post: 'works' }), { headers: { - 'content-type': 'application/json' - } + 'content-type': 'application/json', + }, }); } diff --git a/packages/astro/test/fixtures/middleware space/integration-middleware-pre.js b/packages/astro/test/fixtures/middleware space/integration-middleware-pre.js index 3bf484b2becb..1ebf28066350 100644 --- a/packages/astro/test/fixtures/middleware space/integration-middleware-pre.js +++ b/packages/astro/test/fixtures/middleware space/integration-middleware-pre.js @@ -1,11 +1,11 @@ -import { sequence, defineMiddleware } from 'astro:middleware'; +import { defineMiddleware } from 'astro:middleware'; export const onRequest = defineMiddleware((context, next) => { - if(context.url.pathname === '/integration-pre') { + if (context.url.pathname === '/integration-pre') { return new Response(JSON.stringify({ pre: 'works' }), { headers: { - 'content-type': 'application/json' - } + 'content-type': 'application/json', + }, }); } diff --git a/packages/astro/test/fixtures/middleware-no-user-middleware/astro.config.mjs b/packages/astro/test/fixtures/middleware-no-user-middleware/astro.config.mjs new file mode 100644 index 000000000000..ffae133bebc1 --- /dev/null +++ b/packages/astro/test/fixtures/middleware-no-user-middleware/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-no-user-middleware/integration-middleware-post.js b/packages/astro/test/fixtures/middleware-no-user-middleware/integration-middleware-post.js new file mode 100644 index 000000000000..18c8969c308b --- /dev/null +++ b/packages/astro/test/fixtures/middleware-no-user-middleware/integration-middleware-post.js @@ -0,0 +1,13 @@ +import { defineMiddleware } from 'astro:middleware'; + +export const onRequest = defineMiddleware((context, next) => { + if (context.url.pathname === '/post') { + return new Response(JSON.stringify({ post: 'works' }), { + headers: { + 'content-type': 'application/json', + }, + }); + } + + return next(); +}); diff --git a/packages/astro/test/fixtures/middleware-no-user-middleware/integration-middleware-pre.js b/packages/astro/test/fixtures/middleware-no-user-middleware/integration-middleware-pre.js new file mode 100644 index 000000000000..07bb16b4600b --- /dev/null +++ b/packages/astro/test/fixtures/middleware-no-user-middleware/integration-middleware-pre.js @@ -0,0 +1,13 @@ +import { defineMiddleware } from 'astro:middleware'; + +export const onRequest = defineMiddleware((context, next) => { + if (context.url.pathname === '/pre') { + return new Response(JSON.stringify({ pre: 'works' }), { + headers: { + 'content-type': 'application/json', + }, + }); + } + + return next(); +}); diff --git a/packages/astro/test/fixtures/middleware-no-user-middleware/package.json b/packages/astro/test/fixtures/middleware-no-user-middleware/package.json new file mode 100644 index 000000000000..9a2a378ed116 --- /dev/null +++ b/packages/astro/test/fixtures/middleware-no-user-middleware/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/middleware-no-user-middlewaqre", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/index.astro b/packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/index.astro new file mode 100644 index 000000000000..822b15cd318c --- /dev/null +++ b/packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/index.astro @@ -0,0 +1,14 @@ +--- +const data = Astro.locals; +--- + + + + Testing + + + +Index +

{data?.name}

+ + diff --git a/packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/post.astro b/packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/post.astro new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/pre.astro b/packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/pre.astro new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/astro/test/middleware.test.js b/packages/astro/test/middleware.test.js index a9487950eb65..8332dfe41949 100644 --- a/packages/astro/test/middleware.test.js +++ b/packages/astro/test/middleware.test.js @@ -99,6 +99,31 @@ describe('Middleware in DEV mode', () => { expect(json.post).to.equal('works'); }); }); + + describe('Integration hooks with no user middleware', () => { + before(async () => { + fixture = await loadFixture({ + root: './fixtures/middleware-no-user-middleware/', + }); + devServer = await fixture.startDevServer(); + }); + + after(async () => { + await devServer.stop(); + }); + + it('Integration middleware marked as "pre" runs', async () => { + let res = await fixture.fetch('/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('/post'); + let json = await res.json(); + expect(json.post).to.equal('works'); + }); + }); }); describe('Middleware in PROD mode, SSG', () => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 322d1a4f7717..d7ee1b767967 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2957,6 +2957,12 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/middleware-no-user-middleware: + dependencies: + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/middleware-ssg: dependencies: astro: