Repository navigation
Fix middleware HMR not reloading when imported modules change - #17597
astrobot-houston wants to merge 1 commit into
Conversation
…17590) Add a hotUpdate hook to the middleware Vite plugin that walks the importers chain of updated modules. When a changed module is a transitive dependency of the middleware virtual module, the plugin now invalidates the virtual module and sends the astro:middleware-updated HMR event to clear the pipeline's cached middleware function. Previously only direct edits to middleware.ts triggered a reload; edits to files imported by the middleware were silently ignored.
🦋 Changeset detectedLatest commit: 2d42757 The changes in this PR will be included in the next version bump. This PR includes changesets to release 402 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Hmm... looking at the codebase, the only other use of the |
|
Yeah, just checked out this branch locally, changed the hotUpdate to the below code, built and ran hotUpdate: {
handler() {
if (!isAstroServerEnvironment(this.environment)) return;
const middlewareVirtualMod =
this.environment.moduleGraph.getModuleById(MIDDLEWARE_RESOLVED_MODULE_ID);
if (!middlewareVirtualMod) return;
this.environment.moduleGraph.invalidateModule(middlewareVirtualMod);
this.environment.hot.send('astro:middleware-updated', {});
},
}I tested it by changing import { defineMiddleware } from "astro:middleware";
import { MiscUtils } from "./utils/misc";
import { OtherUtils } from "./utils/other";
let executed = 0;
export const onRequest = defineMiddleware(async (context, next) => {
context.locals.utils = new MiscUtils();
new OtherUtils("middleware");
executed += 1;
console.log("Executed:", executed);
return next();
});and then added a newline in Then I added a newline in So, yeah. No need to manually traverse the imports, it seems |
|
Also, |
Changes
middleware.tsitself is edited.hotUpdatehook to the middleware Vite plugin that walks each updated module'simporterschain. When a changed module is a transitive dependency of the middleware virtual module, the hook invalidates it and sends theastro:middleware-updatedevent to clear the pipeline cache.Testing
packages/astro/test/units/middleware/middleware-hmr-imports.test.tswith 6 unit tests covering the newisTransitiveImporterOfhelper: direct imports, multi-level transitive chains, unrelated modules, circular references, empty importer lists, and deeply nested chains.Docs
Closes #17590