Skip to content

Commit ba08e35

Browse files
authored
Stop reloading the middleware on hot updates that match no modules (#17944)
1 parent 0bc5715 commit ba08e35

3 files changed

Lines changed: 106 additions & 2 deletions

File tree

‎.changeset/odd-apples-cross.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a regression in `astro dev` where writes outside the module graph (for example, `@astrojs/cloudflare`'s `.wrangler/state` files) invalidated the middleware on every request, causing repeated SSR reloads. Such writes no longer invalidate the middleware.

‎packages/astro/src/core/middleware/vite-plugin.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,17 @@ export function vitePluginMiddleware({ settings }: { settings: AstroSettings }):
3939
);
4040
},
4141
hotUpdate: {
42-
handler() {
42+
handler(ctx) {
4343
if (!isAstroServerEnvironment(this.environment)) return;
4444

45+
// A change that matched no modules in the graph can't affect the
46+
// middleware or anything it imports, so there's nothing to invalidate.
47+
// Writes outside the graph (e.g. `@astrojs/cloudflare`'s `.wrangler/state`)
48+
// fire a hotUpdate per write and would otherwise reload the middleware
49+
// on every request.
50+
// https://github.com/withastro/astro/issues/17933
51+
if (ctx.modules.length === 0) return;
52+
4553
const middlewareVirtualMod = this.environment.moduleGraph.getModuleById(
4654
MIDDLEWARE_RESOLVED_MODULE_ID,
4755
);

‎packages/astro/test/units/middleware/middleware-hmr.test.ts‎

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import assert from 'node:assert/strict';
22
import { describe, it } from 'node:test';
3-
import { isMiddlewarePath } from '../../../dist/core/middleware/vite-plugin.js';
3+
import {
4+
isMiddlewarePath,
5+
vitePluginMiddleware,
6+
} from '../../../dist/core/middleware/vite-plugin.js';
7+
import { createBasicSettings } from '../test-utils.ts';
48

59
describe('middleware HMR path matching', () => {
610
it('matches middleware.ts (single-file pattern)', () => {
@@ -35,3 +39,90 @@ describe('middleware HMR path matching', () => {
3539
assert.ok(!isMiddlewarePath('components/Header.astro'));
3640
});
3741
});
42+
43+
describe('middleware HMR hotUpdate handler', () => {
44+
async function getHandler() {
45+
const settings = await createBasicSettings();
46+
const plugin = vitePluginMiddleware({ settings });
47+
const hook = plugin.hotUpdate;
48+
const handler = typeof hook === 'function' ? hook : hook?.handler;
49+
assert.ok(handler, 'plugin should expose a hotUpdate handler');
50+
return handler;
51+
}
52+
53+
function fakeEnvironment(name = 'ssr') {
54+
const virtualMod = { id: '\0virtual:astro:middleware' };
55+
const invalidated: unknown[] = [];
56+
const sent: string[] = [];
57+
return {
58+
environment: {
59+
name,
60+
moduleGraph: {
61+
getModuleById: () => virtualMod,
62+
invalidateModule: (mod: unknown) => invalidated.push(mod),
63+
},
64+
hot: { send: (event: string) => sent.push(event) },
65+
},
66+
invalidated,
67+
sent,
68+
};
69+
}
70+
71+
it('invalidates the middleware when the change matched modules', async () => {
72+
const handler = await getHandler();
73+
const { environment, invalidated, sent } = fakeEnvironment();
74+
75+
await handler.call(
76+
{ environment } as any,
77+
{
78+
modules: [{ id: '/src/middleware.ts' }],
79+
} as any,
80+
);
81+
82+
assert.equal(invalidated.length, 1);
83+
assert.deepEqual(sent, ['astro:middleware-updated']);
84+
});
85+
86+
it('invalidates the middleware for unrelated in-graph changes too', async () => {
87+
const handler = await getHandler();
88+
const { environment, invalidated, sent } = fakeEnvironment();
89+
90+
await handler.call(
91+
{ environment } as any,
92+
{
93+
// A change that matched modules in the graph is not filtered: it
94+
// could be a transitive import of the middleware, so the handler
95+
// invalidates unless the change matched no modules at all.
96+
modules: [{ id: '/src/pages/index.astro' }],
97+
} as any,
98+
);
99+
100+
assert.equal(invalidated.length, 1);
101+
assert.deepEqual(sent, ['astro:middleware-updated']);
102+
});
103+
104+
it("doesn't invalidate the middleware when the change matched no modules", async () => {
105+
const handler = await getHandler();
106+
const { environment, invalidated, sent } = fakeEnvironment();
107+
108+
await handler.call({ environment } as any, { modules: [] } as any);
109+
110+
assert.equal(invalidated.length, 0);
111+
assert.equal(sent.length, 0);
112+
});
113+
114+
it('ignores hotUpdates outside server environments', async () => {
115+
const handler = await getHandler();
116+
const { environment, invalidated, sent } = fakeEnvironment('client');
117+
118+
await handler.call(
119+
{ environment } as any,
120+
{
121+
modules: [{ id: '/src/middleware.ts' }],
122+
} as any,
123+
);
124+
125+
assert.equal(invalidated.length, 0);
126+
assert.equal(sent.length, 0);
127+
});
128+
});

0 commit comments

Comments
 (0)