Skip to content

Commit cb5a0f3

Browse files
committed
fix: update clientReferenceMetaMap + eager transform during hotUpdate
1 parent fbbe43e commit cb5a0f3

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

packages/plugin-rsc/e2e/basic.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ function defineTest(f: Fixture) {
435435
test('hmr switch server to client', async ({ page }) => {
436436
await page.goto(f.url())
437437
await waitForHydration(page)
438-
// await using _ = await expectNoReload(page)
438+
await using _ = await expectNoReload(page)
439439

440440
await expect(page.getByTestId('test-hmr-switch-server')).toContainText(
441441
'(useState: false)',
@@ -446,32 +446,32 @@ function defineTest(f: Fixture) {
446446
'(useState: true)',
447447
)
448448

449+
await page.waitForTimeout(100)
449450
editor.reset()
450-
// TODO: currently reload is required
451-
await expect(async () => {
452-
await page.reload()
453-
await expect(page.getByTestId('test-hmr-switch-server')).toContainText(
454-
'(useState: false)',
455-
{ timeout: 0 },
456-
)
457-
}).toPass()
458-
// await expect(page.getByTestId('test-hmr-switch-server')).toContainText(
459-
// '(useState: false)',
460-
// )
451+
await expect(page.getByTestId('test-hmr-switch-server')).toContainText(
452+
'(useState: false)',
453+
)
461454
})
462455

463456
test('hmr switch client to server', async ({ page }) => {
464457
await page.goto(f.url())
465458
await waitForHydration(page)
459+
await using _ = await expectNoReload(page)
466460

467461
await expect(page.getByTestId('test-hmr-switch-client')).toContainText(
468462
'(useState: true)',
469463
)
470464
const editor = f.createEditor('src/routes/hmr-switch/client.tsx')
471465
editor.edit((s) => s.replace(`'use client'`, ''))
472-
// await expect(page.getByTestId('test-hmr-switch-client')).toContainText(
473-
// '(useState: false)',
474-
// )
466+
await expect(page.getByTestId('test-hmr-switch-client')).toContainText(
467+
'(useState: false)',
468+
)
469+
470+
await page.waitForTimeout(100)
471+
editor.reset()
472+
await expect(page.getByTestId('test-hmr-switch-client')).toContainText(
473+
'(useState: true)',
474+
)
475475
})
476476
})
477477

packages/plugin-rsc/src/plugin.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,22 @@ export default function vitePluginRsc(
453453
const ids = ctx.modules.map((mod) => mod.id).filter((v) => v !== null)
454454
if (ids.length === 0) return
455455

456-
// TODO: handle server<->client switch
456+
// handle client -> server switch (i.e. "use client" removal)
457+
// by eagerly transforming new module on "rsc" environment.
458+
if (this.environment.name === 'rsc') {
459+
for (const mod of ctx.modules) {
460+
if (
461+
mod.type === 'js' &&
462+
mod.id &&
463+
mod.id in manager.clientReferenceMetaMap
464+
) {
465+
try {
466+
await this.environment.transformRequest(mod.url)
467+
} catch {}
468+
}
469+
}
470+
}
471+
457472
// a shared component/module will have `isInsideClientBoundary = false` on `rsc` environment
458473
// and `isInsideClientBoundary = true` on `client` environment,
459474
// which means both server hmr and client hmr will be triggered.

0 commit comments

Comments
 (0)