Skip to content

Commit e2a112e

Browse files
authored
fix: improve client HMR (#1029)
#1021 was incomplete. This should fix it.
1 parent 8dae7a2 commit e2a112e

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

e2e/hot-reload.spec.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,9 @@ test.describe('hot reload', () => {
112112
await modifyFile('src/pages/index.tsx', 'Modified Page', 'Edited Page');
113113
await expect(page.getByText('Edited Page')).toBeVisible();
114114
await page.waitForTimeout(500); // need to wait not to full reload
115-
// FIXME The following should pass but not for now.
116-
// It's probably because Vite adds `?t=...` timestamp with HMR.
117-
// await expect(page.getByTestId('count')).toHaveText('3');
118-
// await page.getByTestId('increment').click();
119-
// await expect(page.getByTestId('count')).toHaveText('4');
115+
await expect(page.getByTestId('count')).toHaveText('3');
116+
await page.getByTestId('increment').click();
117+
await expect(page.getByTestId('count')).toHaveText('4');
120118
await terminate(pid!);
121119
});
122120
});

packages/waku/src/lib/plugins/vite-plugin-rsc-delegate.ts

+6
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,17 @@ export function rscDelegatePlugin(
7777
},
7878
async handleHotUpdate(ctx) {
7979
if (mode === 'development') {
80+
if (ctx.file.endsWith('/pages.gen.ts')) {
81+
// auto generated file by fsRouterTypegenPlugin
82+
return [];
83+
}
8084
await updateAllStyles(); // FIXME is this too aggressive?
8185
if (moduleImports.has(ctx.file)) {
8286
// re-inject
8387
const transformedResult = await server.transformRequest(ctx.file);
8488
if (transformedResult) {
8589
const { default: source } = await server.ssrLoadModule(ctx.file);
90+
console.log('[rsc] module import', ctx.file);
8691
callback({
8792
type: 'custom',
8893
event: 'module-import',
@@ -93,6 +98,7 @@ export function rscDelegatePlugin(
9398
ctx.modules.length &&
9499
!isClientEntry(ctx.file, await ctx.read())
95100
) {
101+
console.log('[rsc] hot reload', ctx.file);
96102
callback({ type: 'custom', event: 'rsc-reload' });
97103
}
98104
}

packages/waku/src/lib/plugins/vite-plugin-rsc-hmr.ts

+16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ import.meta.hot = __vite__createHotContext(import.meta.url);
2626
2727
if (import.meta.hot && !globalThis.__WAKU_HMR_CONFIGURED__) {
2828
globalThis.__WAKU_HMR_CONFIGURED__ = true;
29+
import.meta.hot.on('vite:afterUpdate', (data) => {
30+
if (data.type === 'update') {
31+
for (const update of data.updates) {
32+
if (
33+
update.type === 'js-update' &&
34+
globalThis.__WAKU_CLIENT_MODULE_LOADING__.has(update.path)
35+
) {
36+
globalThis.__WAKU_CLIENT_MODULE_LOADING__.set(update.path,
37+
globalThis.__WAKU_CLIENT_IMPORT__(update.path + '?t=' + update.timestamp).then((m) => {
38+
globalThis.__WAKU_CLIENT_MODULE_CACHE__.set(update.path, m);
39+
})
40+
);
41+
}
42+
}
43+
}
44+
});
2945
import.meta.hot.on('rsc-reload', () => {
3046
globalThis.__WAKU_RSC_RELOAD_LISTENERS__?.forEach((l) => l());
3147
});

0 commit comments

Comments
 (0)