Skip to content

Commit e6eca00

Browse files
cursoragentjherr
andcommitted
fix(ui-react): one React in the production SSR bundle under pnpm (#44)
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>
1 parent 1a4f94c commit e6eca00

21 files changed

Lines changed: 370 additions & 48 deletions

File tree

‎.changeset/ssr-single-react.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@tanstack/cms-ui-react': patch
3+
---
4+
5+
Fix the production server returning 500 on every server-rendered page with "Invalid hook call" (`Cannot read properties of null (reading 'useRef')`) in apps installed with pnpm.
6+
7+
`@tanstack/react-store`, which the admin's auth hooks and `@tanstack/react-form` use, imports the CommonJS `use-sync-external-store` shim. A production SSR build resolves bare imports from the app root. Under pnpm the app can't resolve react-store, so Vite inlined it with the shim, and the shim's `require('react')` loaded a second React at runtime beside the one Nitro bundles. In SSR, `tanstackCms()` now serves the shim's entry points as ESM on React's built-in `useSyncExternalStore`, so they share the app's React.
8+
9+
Apps that worked around this with `ssr: { external: ['@tanstack/react-store'] }` and a direct `@tanstack/react-store` dependency can drop both. An app needs the `tanstackCms()` plugin to get the fix.

‎docs/architecture/routes-in-a-package.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default defineConfig({
4646
})
4747
```
4848

49-
- **`tanstackCms()`** (from `@tanstack/cms-ui-react/vite`) keeps the CMS packages (`@tanstack/cms-core`, `@tanstack/cms-api`, `@tanstack/cms-ui-react`, `@tanstack/cms-client`) and `@tanstack/ai*` out of Vite's SSR externals and dependency pre-bundling. The packages ship TanStack Start server functions and route files that your app's Start compiler must transform, so the plugin is required when you consume the published packages. Pass `tanstackCms({ packages: [...] })` to treat additional packages the same way.
49+
- **`tanstackCms()`** (from `@tanstack/cms-ui-react/vite`) keeps the CMS packages (`@tanstack/cms-core`, `@tanstack/cms-api`, `@tanstack/cms-ui-react`, `@tanstack/cms-client`) and `@tanstack/ai*` out of Vite's SSR externals and dependency pre-bundling. The packages ship TanStack Start server functions and route files that your app's Start compiler must transform, so the plugin is required when you consume the published packages. Pass `tanstackCms({ packages: [...] })` to treat additional packages the same way. In SSR it also serves `use-sync-external-store` (which `@tanstack/react-store` and `@tanstack/react-form` import) as ESM on React's own `useSyncExternalStore`. Otherwise, under pnpm, the production server bundle loads a second copy of React, and every server-rendered page fails with "Invalid hook call".
5050
- **`cmsVirtualRoutes()`** (from `@tanstack/cms-ui-react/virtual-routes`) returns a virtual route tree built with `@tanstack/virtual-file-routes`. The router generator turns it into your app's `src/routeTree.gen.ts`, with imports pointing into the package.
5151

5252
Your app's `src/router.tsx` imports `./cms.config` before `./routeTree.gen`, so `defineCms()` has registered the content model before any route loader or server function reads it:

‎examples/astro-starlight-after/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"dev": "vite dev --port 3013",
88
"build": "vite build",
9+
"test:build": "node ../../scripts/smoke-ssr.mjs",
910
"serve": "vite preview",
1011
"start": "node .output/server/index.mjs",
1112
"test:types": "tsc --noEmit",

‎examples/astro-starlight-after/vite.config.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { nitro } from 'nitro/vite'
44
import viteReact from '@vitejs/plugin-react'
55
import tailwindcss from '@tailwindcss/vite'
66
import { cmsVirtualRoutes } from '@tanstack/cms-ui-react/virtual-routes'
7+
import { tanstackCms } from '@tanstack/cms-ui-react/vite'
78

89
export default defineConfig({
910
plugins: [
11+
tanstackCms(),
1012
tailwindcss(),
1113
tanstackStart({
1214
router: {
@@ -26,14 +28,5 @@ export default defineConfig({
2628
build: {
2729
target: 'es2022',
2830
},
29-
ssr: {
30-
noExternal: [
31-
'@tanstack/cms-core',
32-
'@tanstack/ai',
33-
'@tanstack/ai-anthropic',
34-
'@tanstack/ai-gemini',
35-
'@tanstack/ai-mcp',
36-
],
37-
},
3831
nitro: {},
3932
})

‎examples/blog-voting/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"dev": "vite dev --port 3004",
88
"build": "vite build",
9+
"test:build": "node ../../scripts/smoke-ssr.mjs",
910
"serve": "vite preview",
1011
"start": "node .output/server/index.mjs",
1112
"test:types": "tsc --noEmit",

‎examples/blog-voting/vite.config.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import viteReact from '@vitejs/plugin-react'
55
import tailwindcss from '@tailwindcss/vite'
66
import { route } from '@tanstack/virtual-file-routes'
77
import { cmsVirtualRoutes } from '@tanstack/cms-ui-react/virtual-routes'
8+
import { tanstackCms } from '@tanstack/cms-ui-react/vite'
89

910
export default defineConfig({
1011
plugins: [
12+
tanstackCms(),
1113
tailwindcss(),
1214
tanstackStart({
1315
router: {
@@ -31,14 +33,5 @@ export default defineConfig({
3133
build: {
3234
target: 'es2022',
3335
},
34-
ssr: {
35-
noExternal: [
36-
'@tanstack/cms-core',
37-
'@tanstack/ai',
38-
'@tanstack/ai-anthropic',
39-
'@tanstack/ai-gemini',
40-
'@tanstack/ai-mcp',
41-
],
42-
},
4336
nitro: {},
4437
})

‎examples/blog/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"dev": "vite dev --port 3003",
88
"build": "vite build",
9+
"test:build": "node ../../scripts/smoke-ssr.mjs",
910
"serve": "vite preview",
1011
"start": "node .output/server/index.mjs",
1112
"test:types": "tsc --noEmit",

‎examples/db-federated/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"dev": "vite dev --port 3022",
88
"build": "vite build",
9+
"test:build": "node ../../scripts/smoke-ssr.mjs",
910
"serve": "vite preview",
1011
"start": "node .output/server/index.mjs",
1112
"test:types": "tsc --noEmit"

‎examples/db-sqlite/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"dev": "vite dev --port 3021",
88
"build": "vite build",
9+
"test:build": "node ../../scripts/smoke-ssr.mjs",
910
"serve": "vite preview",
1011
"start": "node .output/server/index.mjs",
1112
"test:types": "tsc --noEmit"

‎examples/emdash-after/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"dev": "vite dev --port 3015",
88
"build": "vite build",
9+
"test:build": "node ../../scripts/smoke-ssr.mjs",
910
"serve": "vite preview",
1011
"start": "node .output/server/index.mjs",
1112
"test:types": "tsc --noEmit",

0 commit comments

Comments
 (0)