Skip to content

Commit

Permalink
vite: reduce network calls for route modules during HMR (#8591)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed Jan 25, 2024
1 parent 4651e6a commit db45174
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
36 changes: 18 additions & 18 deletions packages/remix-dev/vite/plugin.ts
Expand Up @@ -1455,29 +1455,28 @@ function isEqualJson(v1: unknown, v2: unknown) {
}

function addRefreshWrapper(
pluginConfig: ResolvedVitePluginConfig,
remixConfig: ResolvedVitePluginConfig,
code: string,
id: string
): string {
let isRoute =
id.endsWith(CLIENT_ROUTE_QUERY_STRING) || getRoute(pluginConfig, id);
let acceptExports = isRoute
? [
"clientAction",
"clientLoader",
"handle",
"meta",
"links",
"shouldRevalidate",
]
: [];
let route = getRoute(remixConfig, id);
let acceptExports =
route || id.endsWith(CLIENT_ROUTE_QUERY_STRING)
? [
"clientAction",
"clientLoader",
"handle",
"meta",
"links",
"shouldRevalidate",
]
: [];
return (
REACT_REFRESH_HEADER.replace("__SOURCE__", JSON.stringify(id)) +
REACT_REFRESH_HEADER.replaceAll("__SOURCE__", JSON.stringify(id)) +
code +
REACT_REFRESH_FOOTER.replace("__SOURCE__", JSON.stringify(id)).replace(
"__ACCEPT_EXPORTS__",
JSON.stringify(acceptExports)
)
REACT_REFRESH_FOOTER.replaceAll("__SOURCE__", JSON.stringify(id))
.replaceAll("__ACCEPT_EXPORTS__", JSON.stringify(acceptExports))
.replaceAll("__ROUTE_ID__", JSON.stringify(route?.id))
);
}

Expand Down Expand Up @@ -1511,6 +1510,7 @@ if (import.meta.hot && !inWebWorker && window.__remixLiveReloadEnabled) {
RefreshRuntime.registerExportsForReactRefresh(__SOURCE__, currentExports);
import.meta.hot.accept((nextExports) => {
if (!nextExports) return;
__ROUTE_ID__ && window.__remixRouteModuleUpdates.set(__ROUTE_ID__, nextExports);
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports, __ACCEPT_EXPORTS__);
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
});
Expand Down
8 changes: 6 additions & 2 deletions packages/remix-dev/vite/static/refresh-utils.cjs
Expand Up @@ -17,8 +17,10 @@ const enqueueUpdate = debounce(async () => {

for (let route of routeUpdates.values()) {
manifest.routes[route.id] = route;

let imported = await __hmr_import(route.url + "?t=" + Date.now());
let imported = window.__remixRouteModuleUpdates.get(route.id);
if (!imported) {
throw Error(`[remix:hmr] No module update found for route ${route.id}`);
}
let routeModule = {
...imported,
// react-refresh takes care of updating these in-place,
Expand Down Expand Up @@ -53,6 +55,7 @@ const enqueueUpdate = debounce(async () => {
);
__remixRouter._internalSetRoutes(routes);
routeUpdates.clear();
window.__remixRouteModuleUpdates.clear();
}

await revalidate();
Expand Down Expand Up @@ -138,6 +141,7 @@ function __hmr_import(module) {
}

const routeUpdates = new Map();
window.__remixRouteModuleUpdates = new Map();

async function revalidate() {
let { promise, resolve } = channel();
Expand Down

0 comments on commit db45174

Please sign in to comment.