Skip to content

Commit 7eb2144

Browse files
yangshunclaude
andauthored
fix(react): serve a workspace-linked void as source in dev (#720)
## What changed Locally, React Pages apps in this repo could stop hydrating after `vp run build`. The browser threw: ``` SyntaxError: The requested module '/node_modules/.vite/deps/void_pages-client.js?v=…' does not provide an export named 'isVoidActionError' ``` The client entry never loaded, so `#app` was never marked hydrated. `websockets-playground.test.ts` timed out on `/chat` and `/notifications` (noted in #719). CI passed because it starts with no Vite cache. ### Cause In dev, the React adapter listed `void/pages-client` and `void/pages-prefetch` in the client `optimizeDeps.include` (#620). It listed `void/live/client` in the worker include. Here `void` is a linked workspace package. Vite serves a linked package as source unless it is listed in `include`, and a listed one is pre-bundled into `node_modules/.vite`. That cache is keyed by the lockfile, config and `NODE_ENV`, not by the linked package's files, so rebuilding Void left the old copy in place. #715 added `isVoidActionError` to `void/pages-client` and re-exported it from `@void/react`. Any cache written before #715 then broke the client. ### Fix `installedDependencies()` keeps a Void entry in `include` only when it resolves into `node_modules`: | `void` install | Before | After | | --- | --- | --- | | From the registry (`node_modules`) | pre-bundled | pre-bundled (keeps #620's cold-start fix) | | Linked workspace package | pre-bundled, stale after a rebuild | served as source, always the current build | This only changes dev behavior for linked installs. Apps that install `void` from npm see no difference. ## For reviewers - **Served as source:** in a linked install, these entries pull in only `@void/edge`, which is linked too and has no imports. Serving them as source therefore can't trigger a late dependency discovery or a full reload. - **`void/live/client`:** the worker include had the same stale-copy hazard, so the same rule applies there. - **Resolution:** `import.meta.resolve` from `@void/react`. `void` is a peer dependency, so this finds the same `void` the app uses. ## Verification - Replayed the failure: built `void` at #715's parent and ran the websockets suite to fill the cache. Then I rebuilt on this branch and ran it again. Before this change: 2 failed. After: 3/3 passed on two runs. - `packages/react/test/unit/plugin-pages.test.ts`: 34 passed. A new unit test covers installed paths (pnpm, npm, Windows) and a linked path. The dev-config test now checks that this workspace's linked `void` is not pre-bundled. - `vp run check`, lint and format are clean. - Full `vp test run`: 774 of 777 files passed (1 skipped). The other 2 files were the Vue and Svelte `*-pages-client` preview suites. They reuse an existing playground `dist`, and this worktree's predated their newest fixtures. After deleting those `dist` folders, both passed (94 tests). To clear a stale cache without this change: ```sh rm -rf playground/<app>/node_modules/.vite ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2680b99 commit 7eb2144

2 files changed

Lines changed: 56 additions & 11 deletions

File tree

‎packages/react/src/plugin-pages.ts‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { readFileSync } from 'node:fs';
2+
import { fileURLToPath } from 'node:url';
23
import { dirname, relative, resolve } from 'pathslash';
34
import type { Plugin } from 'vite';
45
import {
@@ -22,7 +23,8 @@ const WORKER_ENVIRONMENT_NAME = 'void_worker';
2223
const REACT_DEDUPE = ['react', 'react-dom'];
2324
const REACT_OPTIMIZE_DEPS = ['react', 'react/jsx-runtime', 'react/jsx-dev-runtime', 'react-dom'];
2425
const CLIENT_OPTIMIZE_DEPS = [...REACT_OPTIMIZE_DEPS, 'react-dom/client'];
25-
const WORKER_OPTIMIZE_DEPS = [...REACT_OPTIMIZE_DEPS, 'react-dom/server', 'void/live/client'];
26+
const WORKER_OPTIMIZE_DEPS = [...REACT_OPTIMIZE_DEPS, 'react-dom/server'];
27+
const NODE_MODULES_SEGMENT = /[\\/]node_modules[\\/]/;
2628

2729
// SSR HTML bypasses transformIndexHtml, so @vitejs/plugin-react cannot inject
2830
// its Fast Refresh preamble. Without it, the dev client entry fails to load.
@@ -73,7 +75,10 @@ export function pagesPlugin(pagesDir: string, options?: PagesPluginOptions): Plu
7375
// from Vite's dep optimizer. void/pages-client and
7476
// void/pages-prefetch are reached only through virtual entries and
7577
// the excluded @void/react, so the scanner cannot see them.
76-
include: [...CLIENT_OPTIMIZE_DEPS, 'void/pages-client', 'void/pages-prefetch'],
78+
include: [
79+
...CLIENT_OPTIMIZE_DEPS,
80+
...installedDependencies(['void/pages-client', 'void/pages-prefetch']),
81+
],
7782
},
7883
ssr: {
7984
optimizeDeps: {
@@ -89,7 +94,7 @@ export function pagesPlugin(pagesDir: string, options?: PagesPluginOptions): Plu
8994
// SSR is already rendering, the program reload can mix React
9095
// and react-dom/server from different optimized-dep hashes.
9196
entries: [pagesOptimizeEntries],
92-
include: WORKER_OPTIMIZE_DEPS,
97+
include: [...WORKER_OPTIMIZE_DEPS, ...installedDependencies(['void/live/client'])],
9398
},
9499
},
95100
},
@@ -117,6 +122,22 @@ export function pagesPlugin(pagesDir: string, options?: PagesPluginOptions): Plu
117122
});
118123
}
119124

125+
/**
126+
* Keeps the specifiers that resolve into `node_modules`. Vite serves a linked
127+
* package, such as `void` in a workspace, as source unless
128+
* `optimizeDeps.include` lists it. A listed linked package is pre-bundled into
129+
* a cache keyed by the lockfile and config rather than the package's files, so
130+
* dev would keep serving that copy after the package is rebuilt. A client built
131+
* against the new exports then fails to load and never hydrates.
132+
*/
133+
export function installedDependencies(
134+
specifiers: ReadonlyArray<string>,
135+
resolveFile: (specifier: string) => string = (specifier) =>
136+
fileURLToPath(import.meta.resolve(specifier)),
137+
): Array<string> {
138+
return specifiers.filter((specifier) => NODE_MODULES_SEGMENT.test(resolveFile(specifier)));
139+
}
140+
120141
function clientOptimizeEntries(
121142
scan: PageScanResult | null,
122143
pagesDir: string,

‎packages/react/test/unit/plugin-pages.test.ts‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
22
import { join } from 'node:path';
33
import { afterEach, describe, expect, it, vi } from 'vite-plus/test';
4-
import { pagesPlugin } from '../../src/plugin-pages.ts';
4+
import { installedDependencies, pagesPlugin } from '../../src/plugin-pages.ts';
55

66
const TMP = join(import.meta.dirname, '__tmp_pages_plugin__');
77
const PAGES_DIR = join(TMP, 'pages');
@@ -197,6 +197,22 @@ describe('pagesPlugin build config', () => {
197197
});
198198
});
199199

200+
describe('installedDependencies', () => {
201+
it('keeps entries installed in node_modules and drops linked workspace entries', () => {
202+
const files: Record<string, string> = {
203+
'void/pages-client':
204+
'/app/node_modules/.pnpm/void@1.6.2/node_modules/void/dist/pages/client.mjs',
205+
'void/pages-prefetch': 'C:\\app\\node_modules\\void\\dist\\pages\\prefetch.mjs',
206+
'void/live/client': '/repo/packages/void/dist/runtime/live-client.mjs',
207+
};
208+
209+
expect(installedDependencies(Object.keys(files), (specifier) => files[specifier]!)).toEqual([
210+
'void/pages-client',
211+
'void/pages-prefetch',
212+
]);
213+
});
214+
});
215+
200216
describe('pagesPlugin dev config', () => {
201217
afterEach(() => {
202218
rmSync(TMP, { recursive: true, force: true });
@@ -209,19 +225,14 @@ describe('pagesPlugin dev config', () => {
209225
expect(config).toMatchObject({
210226
optimizeDeps: {
211227
entries: ['pages/index.tsx'],
212-
include: expect.arrayContaining(['void/pages-client', 'void/pages-prefetch']),
228+
include: expect.arrayContaining(['react', 'react-dom/client']),
213229
},
214230
environments: {
215231
void_worker: {
216232
optimizeDeps: {
217233
exclude: ['@void/react'],
218234
entries: ['pages/**/*.{js,jsx,ts,tsx}'],
219-
include: expect.arrayContaining([
220-
'react',
221-
'react-dom',
222-
'react-dom/server',
223-
'void/live/client',
224-
]),
235+
include: expect.arrayContaining(['react', 'react-dom', 'react-dom/server']),
225236
},
226237
},
227238
},
@@ -287,6 +298,19 @@ describe('pagesPlugin dev config', () => {
287298
expect((config.optimizeDeps as { entries: Array<string> }).entries).toHaveLength(2);
288299
});
289300

301+
it('serves the workspace-linked void as source instead of pre-bundling it', async () => {
302+
// This repository links void from packages/void, so a pre-bundled copy would
303+
// outlive the next void build.
304+
const config = (await loadServeConfig()) as {
305+
optimizeDeps: { include: Array<string> };
306+
environments: { void_worker: { optimizeDeps: { include: Array<string> } } };
307+
};
308+
309+
expect(config.optimizeDeps.include).not.toContain('void/pages-client');
310+
expect(config.optimizeDeps.include).not.toContain('void/pages-prefetch');
311+
expect(config.environments.void_worker.optimizeDeps.include).not.toContain('void/live/client');
312+
});
313+
290314
it('includes only the React entry points needed by each environment', async () => {
291315
const config = (await loadServeConfig()) as {
292316
optimizeDeps: { include: Array<string> };

0 commit comments

Comments
 (0)