Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/ssr-single-react.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@tanstack/cms-ui-react': patch
---

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.

`@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.

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.
2 changes: 1 addition & 1 deletion docs/architecture/routes-in-a-package.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default defineConfig({
})
```

- **`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.
- **`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".
- **`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.

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:
Expand Down
1 change: 1 addition & 0 deletions examples/astro-starlight-after/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"dev": "vite dev --port 3013",
"build": "vite build",
"test:build": "node ../../scripts/smoke-ssr.mjs",
"serve": "vite preview",
"start": "node .output/server/index.mjs",
"test:types": "tsc --noEmit",
Expand Down
11 changes: 2 additions & 9 deletions examples/astro-starlight-after/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { nitro } from 'nitro/vite'
import viteReact from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import { cmsVirtualRoutes } from '@tanstack/cms-ui-react/virtual-routes'
import { tanstackCms } from '@tanstack/cms-ui-react/vite'

export default defineConfig({
plugins: [
tanstackCms(),
tailwindcss(),
tanstackStart({
router: {
Expand All @@ -26,14 +28,5 @@ export default defineConfig({
build: {
target: 'es2022',
},
ssr: {
noExternal: [
'@tanstack/cms-core',
'@tanstack/ai',
'@tanstack/ai-anthropic',
'@tanstack/ai-gemini',
'@tanstack/ai-mcp',
],
},
nitro: {},
})
1 change: 1 addition & 0 deletions examples/blog-voting/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"dev": "vite dev --port 3004",
"build": "vite build",
"test:build": "node ../../scripts/smoke-ssr.mjs",
"serve": "vite preview",
"start": "node .output/server/index.mjs",
"test:types": "tsc --noEmit",
Expand Down
11 changes: 2 additions & 9 deletions examples/blog-voting/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import viteReact from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import { route } from '@tanstack/virtual-file-routes'
import { cmsVirtualRoutes } from '@tanstack/cms-ui-react/virtual-routes'
import { tanstackCms } from '@tanstack/cms-ui-react/vite'

export default defineConfig({
plugins: [
tanstackCms(),
tailwindcss(),
tanstackStart({
router: {
Expand All @@ -31,14 +33,5 @@ export default defineConfig({
build: {
target: 'es2022',
},
ssr: {
noExternal: [
'@tanstack/cms-core',
'@tanstack/ai',
'@tanstack/ai-anthropic',
'@tanstack/ai-gemini',
'@tanstack/ai-mcp',
],
},
nitro: {},
})
1 change: 1 addition & 0 deletions examples/blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"dev": "vite dev --port 3003",
"build": "vite build",
"test:build": "node ../../scripts/smoke-ssr.mjs",
"serve": "vite preview",
"start": "node .output/server/index.mjs",
"test:types": "tsc --noEmit",
Expand Down
1 change: 1 addition & 0 deletions examples/db-federated/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"dev": "vite dev --port 3022",
"build": "vite build",
"test:build": "node ../../scripts/smoke-ssr.mjs",
"serve": "vite preview",
"start": "node .output/server/index.mjs",
"test:types": "tsc --noEmit"
Expand Down
1 change: 1 addition & 0 deletions examples/db-sqlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"dev": "vite dev --port 3021",
"build": "vite build",
"test:build": "node ../../scripts/smoke-ssr.mjs",
"serve": "vite preview",
"start": "node .output/server/index.mjs",
"test:types": "tsc --noEmit"
Expand Down
1 change: 1 addition & 0 deletions examples/emdash-after/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"dev": "vite dev --port 3015",
"build": "vite build",
"test:build": "node ../../scripts/smoke-ssr.mjs",
"serve": "vite preview",
"start": "node .output/server/index.mjs",
"test:types": "tsc --noEmit",
Expand Down
11 changes: 2 additions & 9 deletions examples/emdash-after/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { nitro } from 'nitro/vite'
import viteReact from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import { cmsVirtualRoutes } from '@tanstack/cms-ui-react/virtual-routes'
import { tanstackCms } from '@tanstack/cms-ui-react/vite'

export default defineConfig({
plugins: [
tanstackCms(),
tailwindcss(),
tanstackStart({
router: {
Expand All @@ -26,14 +28,5 @@ export default defineConfig({
build: {
target: 'es2022',
},
ssr: {
noExternal: [
'@tanstack/cms-core',
'@tanstack/ai',
'@tanstack/ai-anthropic',
'@tanstack/ai-gemini',
'@tanstack/ai-mcp',
],
},
nitro: {},
})
1 change: 1 addition & 0 deletions examples/payload-after/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"dev": "vite dev --port 3011",
"build": "vite build",
"test:build": "node ../../scripts/smoke-ssr.mjs",
"serve": "vite preview",
"start": "node .output/server/index.mjs",
"test:types": "tsc --noEmit",
Expand Down
11 changes: 2 additions & 9 deletions examples/payload-after/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { nitro } from 'nitro/vite'
import viteReact from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import { cmsVirtualRoutes } from '@tanstack/cms-ui-react/virtual-routes'
import { tanstackCms } from '@tanstack/cms-ui-react/vite'

export default defineConfig({
plugins: [
tanstackCms(),
tailwindcss(),
tanstackStart({
router: {
Expand All @@ -27,14 +29,5 @@ export default defineConfig({
build: {
target: 'es2022',
},
ssr: {
noExternal: [
'@tanstack/cms-core',
'@tanstack/ai',
'@tanstack/ai-anthropic',
'@tanstack/ai-gemini',
'@tanstack/ai-mcp',
],
},
nitro: {},
})
1 change: 1 addition & 0 deletions examples/picks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"dev": "vite dev --port 3023",
"build": "vite build",
"test:build": "node ../../scripts/smoke-ssr.mjs",
"serve": "vite preview",
"start": "node .output/server/index.mjs",
"test:types": "tsc --noEmit",
Expand Down
1 change: 1 addition & 0 deletions examples/wordpress-after/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"dev": "vite dev --port 3016",
"build": "vite build",
"test:build": "node ../../scripts/smoke-ssr.mjs",
"serve": "vite preview",
"start": "node .output/server/index.mjs",
"test:types": "tsc --noEmit",
Expand Down
11 changes: 2 additions & 9 deletions examples/wordpress-after/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { nitro } from 'nitro/vite'
import viteReact from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import { cmsVirtualRoutes } from '@tanstack/cms-ui-react/virtual-routes'
import { tanstackCms } from '@tanstack/cms-ui-react/vite'

export default defineConfig({
plugins: [
tanstackCms(),
tailwindcss(),
tanstackStart({
router: {
Expand All @@ -26,14 +28,5 @@ export default defineConfig({
build: {
target: 'es2022',
},
ssr: {
noExternal: [
'@tanstack/cms-core',
'@tanstack/ai',
'@tanstack/ai-anthropic',
'@tanstack/ai-gemini',
'@tanstack/ai-mcp',
],
},
nitro: {},
})
3 changes: 3 additions & 0 deletions knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
],
"ignoreExportsUsedInFile": true,
"workspaces": {
".": {
"entry": ["scripts/smoke-ssr.mjs"]
},
"onboarding/packages/react": {
"entry": ["src/**/*.{ts,tsx}"]
},
Expand Down
12 changes: 10 additions & 2 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,21 @@
"test:build": {
"cache": true,
"dependsOn": ["build"],
"inputs": ["production"]
"inputs": [
"production",
"^production",
"{workspaceRoot}/scripts/smoke-ssr.mjs"
]
},
"build": {
"cache": true,
"dependsOn": ["^build"],
"inputs": ["production", "^production"],
"outputs": ["{projectRoot}/build", "{projectRoot}/dist"]
"outputs": [
"{projectRoot}/build",
"{projectRoot}/dist",
"{projectRoot}/.output"
]
},
"test:docs": {
"cache": true,
Expand Down
89 changes: 89 additions & 0 deletions packages/ui-react/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,78 @@ const CMS_PACKAGES = [
/** Bundled into SSR alongside the CMS (ESM-only, resolved via the CMS packages). */
const SSR_BUNDLED: Array<string | RegExp> = [/^@tanstack\/ai(-[a-z]+)?$/]

/*
* `@tanstack/react-store` (used here, by `@tanstack/react-form` and by
* `@tanstack/search-ui-react`) imports the CommonJS `use-sync-external-store`
* shim. A production SSR build resolves bare imports from the app root, so
* under pnpm, where the app doesn't depend on react-store, Vite inlines it
* with the shim while React stays external. The shim's `require('react')` then
* survives as a runtime `createRequire` call that Nitro can't bundle, and it
* loads a second React next to the one Nitro bundles: every server-rendered
* page fails with "Invalid hook call". In SSR the shim's entry points resolve
* to these ESM equivalents, which `import` React like any other module.
*/
const SHIM = /^use-sync-external-store(\/shim)?(\/index)?(\.js)?$/
const WITH_SELECTOR = /^use-sync-external-store(\/shim)?\/with-selector(\.js)?$/
const SHIM_ID = '\0tanstack-cms:use-sync-external-store'
const WITH_SELECTOR_ID = '\0tanstack-cms:use-sync-external-store/with-selector'

const SHIM_SOURCE = `import { useSyncExternalStore } from 'react'
export { useSyncExternalStore }
export default { useSyncExternalStore }
`

// React's use-sync-external-store/with-selector (MIT, Meta Platforms, Inc.)
// as ESM, on React's built-in useSyncExternalStore.
const WITH_SELECTOR_SOURCE = `import { useDebugValue, useEffect, useMemo, useRef, useSyncExternalStore } from 'react'
export function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnapshot, selector, isEqual) {
const instRef = useRef(null)
let inst
if (instRef.current === null) {
inst = { hasValue: false, value: null }
instRef.current = inst
} else inst = instRef.current
const [getSelection, getServerSelection] = useMemo(() => {
let hasMemo = false
let memoizedSnapshot
let memoizedSelection
const memoizedSelector = (nextSnapshot) => {
if (!hasMemo) {
hasMemo = true
memoizedSnapshot = nextSnapshot
const nextSelection = selector(nextSnapshot)
if (isEqual !== undefined && inst.hasValue) {
const currentSelection = inst.value
if (isEqual(currentSelection, nextSelection)) return (memoizedSelection = currentSelection)
}
return (memoizedSelection = nextSelection)
}
const currentSelection = memoizedSelection
if (Object.is(memoizedSnapshot, nextSnapshot)) return currentSelection
const nextSelection = selector(nextSnapshot)
if (isEqual !== undefined && isEqual(currentSelection, nextSelection)) {
memoizedSnapshot = nextSnapshot
return currentSelection
}
memoizedSnapshot = nextSnapshot
return (memoizedSelection = nextSelection)
}
return [
() => memoizedSelector(getSnapshot()),
getServerSnapshot === undefined ? undefined : () => memoizedSelector(getServerSnapshot()),
]
}, [getSnapshot, getServerSnapshot, selector, isEqual])
const value = useSyncExternalStore(subscribe, getSelection, getServerSelection)
useEffect(() => {
inst.hasValue = true
inst.value = value
}, [value])
useDebugValue(value)
return value
}
export default { useSyncExternalStoreWithSelector }
`

/** Structural subset of Vite's `Plugin`, so this file needs no `vite` import. */
export interface CmsVitePlugin {
name: string
Expand All @@ -29,6 +101,12 @@ export interface CmsVitePlugin {
ssr: { noExternal: Array<string | RegExp> }
optimizeDeps: { exclude: Array<string> }
}
resolveId: (
id: string,
importer: string | undefined,
options?: { ssr?: boolean },
) => string | undefined
load: (id: string) => string | undefined
}

export function tanstackCms(
Expand All @@ -42,5 +120,16 @@ export function tanstackCms(
ssr: { noExternal: [...packages, ...SSR_BUNDLED] },
optimizeDeps: { exclude: packages },
}),
resolveId: (id, _importer, resolveOptions) => {
if (!resolveOptions?.ssr) return undefined
if (WITH_SELECTOR.test(id)) return WITH_SELECTOR_ID
if (SHIM.test(id)) return SHIM_ID
return undefined
},
load: (id) => {
if (id === SHIM_ID) return SHIM_SOURCE
if (id === WITH_SELECTOR_ID) return WITH_SELECTOR_SOURCE
return undefined
},
}
}
Loading
Loading