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:
parent
f6c4214042
commit
2aaed2d2a9
8 changed files with 56 additions and 5 deletions
5
.changeset/bright-crabs-kick.md
Normal file
5
.changeset/bright-crabs-kick.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes a bug where the middleware was incorrectly imported during the build
|
|
@ -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 () {
|
||||
? 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) {
|
||||
|
|
8
packages/astro/test/fixtures/middleware-full-ssr/package.json
vendored
Normal file
8
packages/astro/test/fixtures/middleware-full-ssr/package.json
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "@test/middleware-full-ssr",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*"
|
||||
}
|
||||
}
|
1
packages/astro/test/fixtures/middleware-full-ssr/src/error.js
vendored
Normal file
1
packages/astro/test/fixtures/middleware-full-ssr/src/error.js
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
throw new Error("Shoud not error at build time")
|
2
packages/astro/test/fixtures/middleware-full-ssr/src/middleware.js
vendored
Normal file
2
packages/astro/test/fixtures/middleware-full-ssr/src/middleware.js
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
import "./error.js"
|
||||
export const onRequest = (_ , next) => next();
|
14
packages/astro/test/fixtures/middleware-full-ssr/src/pages/index.astro
vendored
Normal file
14
packages/astro/test/fixtures/middleware-full-ssr/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>
|
|
@ -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;
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue