Skip to content

fix(ui-react): one React in the production SSR bundle under pnpm - #44

Merged
jherr merged 1 commit into
mainfrom
cursor/blog-ssr-react-dedupe-d21e
Nov 6, 2026
Merged

jherr merged 1 commit into
mainfrom
cursor/blog-ssr-react-dedupe-d21e

Conversation

@jherr

@jherr jherr commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Merge chain: step 2 of 24. Base: main. Merge it right after #41. The branch is main 3b4a112, then one commit for each PR before this one in the merge order, then this PR's own commit 0bf5729 (tree 4e30daa).

What

Running node .output/server/index.mjs for examples/blog returned 500 on every server-rendered page: TypeError: Cannot read properties of null (reading 'useRef'), an invalid hook call. Eight other examples did the same. Dev and E2E were fine, because both run on the Vite dev server.

Root cause

The production server bundle loaded two copies of React.

  • Where the CommonJS shim comes from. @tanstack/react-store imports the CommonJS use-sync-external-store shim, which calls require('react'). ui-react's auth hooks use react-store, and so do @tanstack/react-form and @tanstack/search-ui-react.
  • Why the build inlines it. A Vite 8 SSR build resolves bare imports from the app root, while dev resolves them from the importer. Under pnpm the app can't see react-store, so the build inlined react-store and the shim while React stayed external.
  • Why that loads a second React. Rolldown emitted the shim's require('react') as a createRequire call, which Nitro can't trace. At runtime that call loaded a second React from node_modules/.pnpm.

When it started. I bisected with a production build and boot at each step. 81a5990 (the 1.0 alpha) is good. The first bad commit is b375842 (2026-11-03), which moved the admin's auth onto @tanstack/react-store. So the bug predates the refactor and #14.

Who was affected (production build, then boot, on main 3b4a112)

App Before After
examples/blog, blog-voting, db-federated, db-sqlite, picks 500 pass
examples/{wordpress,emdash,payload,astro-starlight}-after 500 pass
examples/db-markdown (doesn't use ui-react) OK OK
app/base, app/prm (already work around it with ssr.external and a direct react-store dependency) OK unchanged
starters/* no production build —

The fix

  • tanstackCms() (packages/ui-react/src/vite.ts). In SSR, it resolves the shim's entry points to two virtual modules:

    • the shim re-exports React's built-in useSyncExternalStore;
    • with-selector is an ESM port of React's own implementation (MIT, Meta).

    Both import React like any other module, so Nitro bundles exactly one React. Every app that uses the plugin gets the fix with no config change. Client builds are untouched.

  • Five examples now use the plugin. blog-voting and the four *-after examples listed ssr.noExternal by hand instead of using tanstackCms(), so the package fix couldn't reach them. The migration skills already say to use the plugin.

  • Changeset and docs. A @tanstack/cms-ui-react patch changeset, and one sentence in routes-in-a-package.md.

Regression check

  • scripts/smoke-ssr.mjs boots .output/server/index.mjs on a free port with a throwaway data directory. It requests /, /admin, /content and /admin/content, because most examples mount the admin at the root. It fails in four cases:
    • any 5xx;
    • "Invalid hook call" in the server log;
    • no path answering below 400;
    • the server exiting or not answering within 30s.
  • Wiring. The script runs as test:build in the nine ui-react examples. pnpm test:ci and test:pr already run that target after build. nx.json now caches .output and keys test:build on ^production. knip.json registers the script.
  • Unit tests. packages/ui-react/tests/vite-plugin.test.tsx adds 4 tests:
    • the SSR id mapping, and the ids it leaves alone;
    • the served with-selector: it renders under renderToString, re-renders on a store update, and skips a re-render when isEqual holds.
  • Negative control. With the vite.ts change reverted, blog:test:build fails with 500s and "Invalid hook call".

Breaking changes

None. In SSR, apps now get React's built-in useSyncExternalStore instead of the shim's. That hook ships in React 18 and later, and ui-react already requires React 19.

How verified

GitHub Actions is out of billing, so everything ran locally with --skip-nx-cache, on head e202a53 on top of main 3b4a112.

Check Result
pnpm install --frozen-lockfile passes
pnpm test:ci only the known @tanstack/cms-api:test:oxlint fails. ui-react 89, including the 4 new tests. Every starter and example passes. Kiira 124/124. Knip is clean.
The nine examples' test:build, inside test:ci all pass. / and /content (/admin/content for payload-after) return 200, and the rest return 404.
pnpm test:dts clean
pnpm test:scripts 59 pass
pnpm test:dialects exits 0. readDocument (test) at 248 is drift that's already on main.
E2E blog / base 28 / 12 pass

I also checked separately: blog:test:build passes, and the booted blog server returns 200 on /, where it returned 500 on main.

Assumptions

  • The fix belongs in the plugin. No react-store release drops the shim, and react-form and search-ui-react pull it in too.
  • Replacing the shim is safe. The shim exists only for React versions before 18, and the with-selector port follows React's source.
  • The bar is "not 500". /admin can be a 404 where the admin is mounted at the root. The script still requires at least one path below 400.
  • The app/base and app/prm workarounds stay for now. They're redundant after this PR; removing them is a follow-up.

Overlap with the 1d/R2 stack

1d (#28) also changes packages/ui-react/src/vite.ts: its imports, the doc comment and the tanstackCms() options. It also changes the cmsVirtualRoutes import line in the five example configs. Expect small, mechanical conflicts there. No routes/* files are touched.

This PR is independent of the refactor stacks.

Merge chain (local verification, CI down)

This PR's commits are squashed into one commit on the linear merge chain. That commit holds this PR's own changes, resolved against every PR before it, with the integration rehearsal's fixes for this PR applied. No conflicts.

The chain tip's tree is exactly the integration rehearsal plus #46 (c4e6d7c), and the tip passes the full local suite. I simulated merging every PR in order, with a merge commit or a fast-forward at each step, and every step is clean. A plain squash of the next PR would conflict, because the squash commit isn't an ancestor of the next branch. To squash instead, first rebase the branch's one commit onto the new main; that rebase is always clean.

Open in Web Open in Cursor 

@coderabbitai

coderabbitai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

TanStack is on CodeRabbit Free, which includes PR summaries. Ask your admin to upgrade for code reviews.

  • Ask an admin to upgrade

Open in CodeRabbit

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@changeset-bot

changeset-bot Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e6eca00

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@tanstack/cms-ui-react Patch
@tanstack/cms-api Patch
@tanstack/cms-cli Patch
@tanstack/cms-client Patch
@tanstack/cms-core Patch
@tanstack/cms-media-s3 Patch
@tanstack/cms-store-libsql Patch
@tanstack/cms-connect-atproto Patch
@tanstack/cms-migrate-wp Patch
@tanstack/cms-skills Patch
@tanstack/search-cms Patch
@tanstack/cms-store-postgres Patch
@tanstack/cms-ui-dom Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

cursor Bot pushed a commit that referenced this pull request Nov 6, 2026
…ckCms(); SSR smoke in test:build)

Co-authored-by: jherr <jherr@pobox.com>
cursor Bot pushed a commit that referenced this pull request Nov 6, 2026
…l URL

#44's vite-plugin test imports vite.ts under happy-dom, whose URL class
Node's fileURLToPath rejects; vite.ts now loads virtual-routes.ts (#28).

Co-authored-by: jherr <jherr@pobox.com>
cursor Bot pushed a commit that referenced this pull request Nov 6, 2026
Co-authored-by: jherr <jherr@pobox.com>
@cursor
cursor Bot force-pushed the cursor/blog-ssr-react-dedupe-d21e branch from e202a53 to 0bf5729 Compare November 6, 2026 14:50
Chain step 2 of the linear merge chain onto main 3b4a112: this PR's own changes, resolved against the PRs before it in the merge order.

Co-authored-by: jherr <jherr@pobox.com>
@jherr
jherr force-pushed the cursor/blog-ssr-react-dedupe-d21e branch from 0bf5729 to e6eca00 Compare November 6, 2026 15:05
@jherr
jherr merged commit 88e0150 into main Nov 6, 2026
@jherr
jherr deleted the cursor/blog-ssr-react-dedupe-d21e branch November 8, 2026 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants