0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix(middleware): don't import via entrypoint (#12707)

* fix(middleware): don't import via entrypoint

* fix(middleware): don't import via entrypoint
This commit is contained in:
Emanuele Stoppa 2024-12-10 20:48:38 +00:00 committed by GitHub
parent f6c4214042
commit 2aaed2d2a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 56 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a bug where the middleware was incorrectly imported during the build

View file

@ -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) {

View file

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

View file

@ -0,0 +1 @@
throw new Error("Shoud not error at build time")

View file

@ -0,0 +1,2 @@
import "./error.js"
export const onRequest = (_ , next) => next();

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

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

View file

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