Skip to content

React loads twice in first load from cold started dev server #128

Description

@uhyo

Reproduction Steps

  1. cd packages/example-fs-routing
  2. Make sure that ./node_modules/.vite does not exist (delete it if exists)
  3. pnpm dev (runs Vite dev server)
  4. Open http://localhost:5173/

Console logs below error:

Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://react.dev/link/invalid-hook-call for tips about how to debug and fix this problem.

Note that then the page immediately reloads automatically. On second request this issue disappears.

To reproduce it again, you need to remove .vite and restart the Vite dev server.

Vite Log

Vite log:

4:21:07 PM [vite] (client) ✨ new dependencies optimized: @funstack/router
4:21:07 PM [vite] (client) ✨ optimized dependencies changed. reloading

Vite seems to detect the drift as 'optimized dependencies changed' and perform the reload.

Analysis

As the error message suggests, two copies of React are loaded. Chrome devtools revealed that there does exist two different request chains that load different copies of React.

1st copy:

Request initiator chain
http://localhost:5173/
http://localhost:5173/@id/_x00_virtual:vite-rsc/entry-browser
http://localhost:5173/@fs/home/uhyo/repos/funstack-static/packages/static/dist/entries/client.mjs
http://localhost:5173/@fs/home/uhyo/repos/funstack-static/packages/static/dist/client/entry.mjs
http://localhost:5173/node_modules/.vite/deps/react.js?v=edd0de91
http://localhost:5173/node_modules/.vite/deps/rolldown-runtime-BvCyGRYZ.js?v=a27e72b2

2nd copy:

Request initiator chain
http://localhost:5173/
http://localhost:5173/@id/_x00_virtual:vite-rsc/entry-browser
http://localhost:5173/@fs/home/uhyo/repos/funstack-static/packages/static/dist/entries/client.mjs
http://localhost:5173/@fs/home/uhyo/repos/funstack-static/packages/static/dist/client/entry.mjs
http://localhost:5173/@fs/home/uhyo/repos/funstack-static/node_modules/.pnpm/@vitejs+plugin-rsc@0.5.27_react-dom@19.2.7(...snip...)@vitejs/plugin-rsc/dist/browser.js?v=a27e72b2
http://localhost:5173/@id/_x00_virtual:vite-rsc/client-package-proxy/@funstack/router
http://localhost:5173/node_modules/.vite/deps/@funstack_router.js?v=6768cc48
http://localhost:5173/node_modules/.vite/deps/@funstack_static_n_react.js?v=4a349293

The 2nd copy appears as @funstack_static_n_react.js.

On the 2nd request (after deps has been optimized), http://localhost:5173/node_modules/.vite/deps/react.js is no longer a whole React module, but a re-export of @funstack_static_n_react.js:

Request initiator chain
http://localhost:5173/@vite/client
http://localhost:5173/
http://localhost:5173/@id/_x00_virtual:vite-rsc/entry-browser
http://localhost:5173/@fs/home/uhyo/repos/funstack-static/packages/static/dist/entries/client.mjs
http://localhost:5173/@fs/home/uhyo/repos/funstack-static/packages/static/dist/client/entry.mjs
http://localhost:5173/node_modules/.vite/deps/react.js?v=4a349293
http://localhost:5173/node_modules/.vite/deps/@funstack_static_n_react.js?v=4a349293

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions