From a4d86d2f43b72c2d98e81435808aa69de3df592a Mon Sep 17 00:00:00 2001 From: Liang Date: Tue, 17 Nov 2026 23:06:46 +0800 Subject: [PATCH] fix(pages): hydrate islands added while the dev server runs --- packages/react/src/plugin-islands.ts | 1 + packages/solid/src/plugin-islands.ts | 1 + packages/svelte/src/plugin-islands.ts | 1 + packages/void/src/pages/islands-plugin.ts | 18 ++++++ packages/vue/src/plugin-islands.ts | 1 + .../island-dev-client-entry.test.ts | 63 +++++++++++++++++++ 6 files changed, 85 insertions(+) create mode 100644 packages/vue/test/integration/island-dev-client-entry.test.ts diff --git a/packages/react/src/plugin-islands.ts b/packages/react/src/plugin-islands.ts index c2e0ff429..298debf50 100644 --- a/packages/react/src/plugin-islands.ts +++ b/packages/react/src/plugin-islands.ts @@ -64,6 +64,7 @@ export function islandsPlugin( }, configureServer(server) { + islandManifest.configureServer(server); watchPagesForRescan(server, pagesDir, root, (scan: PageScanResult) => { pageScan = scan; }); diff --git a/packages/solid/src/plugin-islands.ts b/packages/solid/src/plugin-islands.ts index 566b1623c..6b6027335 100644 --- a/packages/solid/src/plugin-islands.ts +++ b/packages/solid/src/plugin-islands.ts @@ -85,6 +85,7 @@ export function islandsPlugin( }, configureServer(server) { + islandManifest.configureServer(server); watchPagesForRescan(server, pagesDir, root, (scan: PageScanResult) => { pageScan = scan; }); diff --git a/packages/svelte/src/plugin-islands.ts b/packages/svelte/src/plugin-islands.ts index 30a17ebec..ff616cc3d 100644 --- a/packages/svelte/src/plugin-islands.ts +++ b/packages/svelte/src/plugin-islands.ts @@ -79,6 +79,7 @@ export function islandsPlugin( }, configureServer(server) { + islandManifest.configureServer(server); watchPagesForRescan(server, pagesDir, root, (scan: PageScanResult) => { pageScan = scan; }); diff --git a/packages/void/src/pages/islands-plugin.ts b/packages/void/src/pages/islands-plugin.ts index a606305e1..863a5c04f 100644 --- a/packages/void/src/pages/islands-plugin.ts +++ b/packages/void/src/pages/islands-plugin.ts @@ -84,6 +84,11 @@ export type IslandManifest = { * build may load the islands client entry before their transforms run. */ registerPages(pageScan: PageScanResult, pagesDir: string): void; + /** + * Invalidate the dev islands client entry when a page transform records a + * new island, so the next page load hydrates it without a server restart. + */ + configureServer(server: ViteDevServer): void; }; /** `extensions` are tried, in order, for an island import without one. */ @@ -92,6 +97,7 @@ export function createIslandManifest( extensions: ReadonlyArray, ): IslandManifest { const entries = new Map(); + let server: ViteDevServer | null = null; function register( specifier: string, @@ -103,7 +109,16 @@ export function createIslandManifest( return { absPath, islandId: specifier }; } const islandId = normalizePath(relative(root, absPath)).replace(/\.[^.]+$/, ''); + const isNew = entries.get(islandId)?.componentPath !== absPath; entries.set(islandId, { componentPath: absPath, islandId, hydrate }); + if (isNew && server) { + for (const env of Object.values(server.environments)) { + const mod = env.moduleGraph.getModuleById('\0' + ISLANDS_CLIENT_ENTRY_ID); + if (mod) { + env.moduleGraph.invalidateModule(mod); + } + } + } return { absPath, islandId }; } @@ -127,6 +142,9 @@ export function createIslandManifest( } } }, + configureServer(devServer) { + server = devServer; + }, }; } diff --git a/packages/vue/src/plugin-islands.ts b/packages/vue/src/plugin-islands.ts index 659be890a..6ad123f4a 100644 --- a/packages/vue/src/plugin-islands.ts +++ b/packages/vue/src/plugin-islands.ts @@ -72,6 +72,7 @@ export function islandsPlugin( }, configureServer(server) { + islandManifest.configureServer(server); watchPagesForRescan(server, pagesDir, root, (scan: PageScanResult) => { pageScan = scan; }); diff --git a/packages/vue/test/integration/island-dev-client-entry.test.ts b/packages/vue/test/integration/island-dev-client-entry.test.ts new file mode 100644 index 000000000..b8d850f46 --- /dev/null +++ b/packages/vue/test/integration/island-dev-client-entry.test.ts @@ -0,0 +1,63 @@ +import { cpSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; +import vue from '@vitejs/plugin-vue'; +import { createServer } from 'vite'; +import { expect, it } from 'vite-plus/test'; +import { islandsPlugin } from '../../src/plugin-islands.ts'; + +const COUNTER = ` + +`; + +it('adds an island imported after the dev server loaded the islands client entry', async () => { + const root = mkdtempSync(join(import.meta.dirname, '__tmp_island_dev_client_entry_')); + const pages = join(root, 'pages'); + mkdirSync(pages); + mkdirSync(join(root, 'components')); + writeFileSync(join(root, 'components/Counter.vue'), COUNTER); + writeFileSync( + join(pages, 'index.island.vue'), + ` + +`, + ); + + const server = await createServer({ + root, + configFile: false, + cacheDir: join(root, '.vite'), + logLevel: 'silent', + plugins: [vue(), ...islandsPlugin(pages)], + server: { ws: false, watch: null }, + }); + try { + const { client, ssr } = server.environments; + const before = await client.transformRequest('virtual:void-islands-client'); + expect(before?.code).toContain('components/Counter'); + expect(before?.code).not.toContain('components/Second'); + + cpSync(join(root, 'components/Counter.vue'), join(root, 'components/Second.vue')); + writeFileSync( + join(pages, 'index.island.vue'), + ` + +`, + ); + // Rendering the edited page registers the new island. + await ssr.transformRequest('/pages/index.island.vue'); + + const after = await client.transformRequest('virtual:void-islands-client'); + expect(after?.code).toContain('components/Second'); + } finally { + await server.close(); + rmSync(root, { recursive: true, force: true }); + } +});