mirror of
https://github.com/withastro/astro.git
synced 2025-01-20 22:12:38 -05:00
fix(middleware): do not import user middleware if not preset (#9057)
This commit is contained in:
parent
1e97708cda
commit
1bc3319686
13 changed files with 123 additions and 10 deletions
5
.changeset/chilled-swans-appear.md
Normal file
5
.changeset/chilled-swans-appear.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Correctly infer the presence of an user middleware
|
|
@ -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();
|
||||
|
|
|
@ -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',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
|
23
packages/astro/test/fixtures/middleware-no-user-middleware/astro.config.mjs
vendored
Normal file
23
packages/astro/test/fixtures/middleware-no-user-middleware/astro.config.mjs
vendored
Normal file
|
@ -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'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
13
packages/astro/test/fixtures/middleware-no-user-middleware/integration-middleware-post.js
vendored
Normal file
13
packages/astro/test/fixtures/middleware-no-user-middleware/integration-middleware-post.js
vendored
Normal file
|
@ -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();
|
||||
});
|
13
packages/astro/test/fixtures/middleware-no-user-middleware/integration-middleware-pre.js
vendored
Normal file
13
packages/astro/test/fixtures/middleware-no-user-middleware/integration-middleware-pre.js
vendored
Normal file
|
@ -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();
|
||||
});
|
8
packages/astro/test/fixtures/middleware-no-user-middleware/package.json
vendored
Normal file
8
packages/astro/test/fixtures/middleware-no-user-middleware/package.json
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "@test/middleware-no-user-middlewaqre",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*"
|
||||
}
|
||||
}
|
14
packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/index.astro
vendored
Normal file
14
packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/index.astro
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
---
|
||||
const data = Astro.locals;
|
||||
---
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Testing</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<span>Index</span>
|
||||
<p>{data?.name}</p>
|
||||
</body>
|
||||
</html>
|
0
packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/post.astro
vendored
Normal file
0
packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/post.astro
vendored
Normal file
0
packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/pre.astro
vendored
Normal file
0
packages/astro/test/fixtures/middleware-no-user-middleware/src/pages/pre.astro
vendored
Normal file
|
@ -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', () => {
|
||||
|
|
6
pnpm-lock.yaml
generated
6
pnpm-lock.yaml
generated
|
@ -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:
|
||||
|
|
Loading…
Add table
Reference in a new issue