0
Fork 0
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:
Emanuele Stoppa 2023-11-10 16:11:15 +00:00 committed by GitHub
parent 1e97708cda
commit 1bc3319686
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 123 additions and 10 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Correctly infer the presence of an user middleware

View file

@ -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();

View file

@ -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',
},
});
}

View file

@ -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',
},
});
}

View 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'
});
}
}
}
]
});

View 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();
});

View 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();
});

View file

@ -0,0 +1,8 @@
{
"name": "@test/middleware-no-user-middlewaqre",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View 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>

View 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
View file

@ -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: