Skip to content

Commit

Permalink
fix(middleware): don't import via entrypoint (#12707)
Browse files Browse the repository at this point in the history
* fix(middleware): don't import via entrypoint

* fix(middleware): don't import via entrypoint
  • Loading branch information
ematipico authored Dec 10, 2024
1 parent f6c4214 commit 2aaed2d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-crabs-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug where the middleware was incorrectly imported during the build
10 changes: 5 additions & 5 deletions packages/astro/src/core/build/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/middleware-full-ssr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/middleware-full-ssr",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error("Shoud not error at build time")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "./error.js"
export const onRequest = (_ , next) => next();
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
const data = Astro.locals;
---

<html>
<head>
<title>Testing</title>
</head>
<body>

<span>Index</span>
<p>{data?.name}</p>
</body>
</html>
15 changes: 15 additions & 0 deletions packages/astro/test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2aaed2d

Please sign in to comment.