Skip to content

Commit

Permalink
fix(remix-dev/vite): invalidate route manifest on route exports hot u…
Browse files Browse the repository at this point in the history
…pdate
  • Loading branch information
hi-ogawa committed Nov 28, 2023
1 parent c043a66 commit 500b839
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ const resolveRelativeRouteFilePath = (

let vmods = [serverEntryId, serverManifestId, browserManifestId];

const invalidateVirtualModules = (viteDevServer: Vite.ViteDevServer) => {
vmods.forEach((vmod) => {
let mod = viteDevServer.moduleGraph.getModuleById(
VirtualModule.resolve(vmod)
);
if (mod) {
viteDevServer.moduleGraph.invalidateModule(mod);
}
});
};

const getHash = (source: BinaryLike, maxLength?: number): string => {
let hash = createHash("sha256").update(source).digest("hex");
return typeof maxLength === "number" ? hash.slice(0, maxLength) : hash;
Expand Down Expand Up @@ -745,16 +756,7 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
) {
previousPluginConfig = pluginConfig;

// Invalidate all virtual modules
vmods.forEach((vmod) => {
let mod = viteDevServer.moduleGraph.getModuleById(
VirtualModule.resolve(vmod)
);

if (mod) {
viteDevServer.moduleGraph.invalidateModule(mod);
}
});
invalidateVirtualModules(viteDevServer);
}

next();
Expand Down Expand Up @@ -1092,15 +1094,31 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
// Update the config cache any time there is a file change
cachedPluginConfig = pluginConfig;
let route = getRoute(pluginConfig, file);
let hotData = { route: null as any };

if (route) {
// invalidate manifest on route exports change
let mod = await server.ssrLoadModule(serverManifestId);
let manifest = mod.default as Manifest;
let metadata = manifest.routes[route.id];
let newMetadata = await getRouteMetadata(
pluginConfig,
viteChildCompiler,
route
);
if (
(["hasLoader", "hasAction", "hasErrorBoundary"] as const).some(
(key) => metadata[key] !== newMetadata[key]
)
) {
invalidateVirtualModules(server);
}
}

server.ws.send({
type: "custom",
event: "remix:hmr",
data: {
route: route
? await getRouteMetadata(pluginConfig, viteChildCompiler, route)
: null,
},
data: hotData,
});

return modules;
Expand Down

0 comments on commit 500b839

Please sign in to comment.