Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(remix-dev/vite): fix unstable file read during handleHotUpdate #8479

Merged
5 changes: 5 additions & 0 deletions .changeset/olive-vans-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Vite: Fix HMR race condition when reading changed file contents
17 changes: 11 additions & 6 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ const getRouteManifestModuleExports = async (
const getRouteModuleExports = async (
viteChildCompiler: Vite.ViteDevServer | null,
pluginConfig: ResolvedRemixVitePluginConfig,
routeFile: string
routeFile: string,
readRouteFile: () => string | Promise<string> = () =>
fse.readFile(routeFile, "utf-8")
): Promise<string[]> => {
if (!viteChildCompiler) {
throw new Error("Vite child compiler not found");
Expand All @@ -318,7 +320,7 @@ const getRouteModuleExports = async (

let [id, code] = await Promise.all([
resolveId(),
fse.readFile(routePath, "utf-8"),
readRouteFile(),
// pluginContainer.transform(...) fails if we don't do this first:
moduleGraph.ensureEntryFromUrl(url, ssr),
]);
Expand Down Expand Up @@ -1325,7 +1327,7 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
},
{
name: "remix-hmr-updates",
async handleHotUpdate({ server, file, modules }) {
async handleHotUpdate({ server, file, modules, read }) {
let pluginConfig = await resolvePluginConfig();
// Update the config cache any time there is a file change
cachedPluginConfig = pluginConfig;
Expand All @@ -1344,7 +1346,8 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
let newRouteMetadata = await getRouteMetadata(
pluginConfig,
viteChildCompiler,
route
route,
read
);

hmrEventData.route = newRouteMetadata;
Expand Down Expand Up @@ -1457,12 +1460,14 @@ function getRoute(
async function getRouteMetadata(
pluginConfig: ResolvedRemixVitePluginConfig,
viteChildCompiler: Vite.ViteDevServer | null,
route: ConfigRoute
route: ConfigRoute,
readRouteFile?: () => string | Promise<string>
) {
let sourceExports = await getRouteModuleExports(
viteChildCompiler,
pluginConfig,
route.file
route.file,
readRouteFile
);

let info = {
Expand Down
Loading