Skip to content

fix(solid-router): prevent HeadContent hydration warnings #7510

Merged
birkskyum merged 4 commits into
solid-router-v2-prefrom
fix/solid-router-headcontent-hydration-fouc
May 30, 2026
Merged

fix(solid-router): prevent HeadContent hydration warnings #7510
birkskyum merged 4 commits into
solid-router-v2-prefrom
fix/solid-router-headcontent-hydration-fouc

Conversation

@brenelz
Copy link
Copy Markdown
Contributor

@brenelz brenelz commented May 30, 2026

Fix two related issues with HeadContent in solid-router:

  1. Hydration: render bare element templates with one getNextElement per invocation (switch instead of if/return), call hydration-affecting hooks unconditionally, and capture the relocated node via the JSX return value instead of a ref attribute so SSR attributes on void meta/link elements are preserved. Eliminates "unclaimed server-rendered node" warnings.

  2. FOUC: stabilize head tag object identity per-key across renders so (which keys by reference) reconciles unchanged tags in place instead of remounting every head node on navigation. Previously a single changed tag (e.g. title) invalidated the whole array, remounting the app stylesheet link and briefly detaching it, causing a flash of unstyled content in dark mode.

…ion FOUC

Fix two related issues with HeadContent in solid-router:

1. Hydration: render bare element templates with one getNextElement per
   invocation (switch instead of if/return), call hydration-affecting
   hooks unconditionally, and capture the relocated node via the JSX
   return value instead of a ref attribute so SSR attributes on void
   meta/link elements are preserved. Eliminates "unclaimed
   server-rendered node" warnings.

2. FOUC: stabilize head tag object identity per-key across renders so
   <For> (which keys by reference) reconciles unchanged tags in place
   instead of remounting every head node on navigation. Previously a
   single changed tag (e.g. title) invalidated the whole array,
   remounting the app stylesheet link and briefly detaching it, causing
   a flash of unstyled content in dark mode.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 30, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2afd6d93-0040-4b6d-bab7-6f8773dde4bb

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/solid-router-headcontent-hydration-fouc

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link
Copy Markdown
Contributor

nx-cloud Bot commented May 30, 2026

View your CI Pipeline Execution ↗ for commit bef6a9d

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 6m 26s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 37s View ↗

☁️ Nx Cloud last updated this comment at 2026-05-30 14:02:41 UTC

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 30, 2026

🚀 Changeset Version Preview

6 package(s) bumped directly, 0 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/solid-router 2.0.0-beta.18 → 2.0.0-beta.19 Changeset
@tanstack/solid-router-devtools 2.0.0-beta.14 → 2.0.0-beta.15 Changeset
@tanstack/solid-router-ssr-query 2.0.0-beta.19 → 2.0.0-beta.20 Changeset
@tanstack/solid-start 2.0.0-beta.19 → 2.0.0-beta.20 Changeset
@tanstack/solid-start-client 2.0.0-beta.18 → 2.0.0-beta.19 Changeset
@tanstack/solid-start-server 2.0.0-beta.18 → 2.0.0-beta.19 Changeset

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 30, 2026

Bundle Size Benchmarks

  • Commit: 8b2499a27935
  • Measured at: 2026-05-30T13:57:19.215Z
  • Baseline source: history:9a6c12596ff6
  • Dashboard: bundle-size history
Scenario Current (gzip) Delta vs baseline Raw Brotli Trend
react-router.minimal 87.48 KiB +238 B (+0.27%) 275.76 KiB 75.97 KiB ▃▃▃▃▃▃▃▁▁▁▁█
react-router.full 90.78 KiB +89 B (+0.10%) 286.95 KiB 78.95 KiB ▆▆▆▆▆▆▆▁▁▁▁█
solid-router.minimal 42.01 KiB +6.54 KiB (+18.42%) 123.63 KiB 37.88 KiB ▁▁▁▁▁▁▁▁▁▁▁█
solid-router.full 46.46 KiB +6.29 KiB (+15.65%) 137.32 KiB 41.85 KiB ▁▁▁▁▁▁▁▁▁▁▁█
vue-router.minimal 53.38 KiB +423 B (+0.78%) 153.07 KiB 47.94 KiB ▂▂▂▂▂▂▂▁▁▁▁█
vue-router.full 58.25 KiB -359 B (-0.60%) 168.53 KiB 52.18 KiB ███████▇▇▇▇▁
react-start.minimal 102.01 KiB +135 B (+0.13%) 324.00 KiB 88.21 KiB ▄▄▄▄▄▄▄▁▁▁▁█
react-start.full 105.38 KiB +128 B (+0.12%) 334.35 KiB 91.14 KiB ▄▄▄▄▄▄▄▁▁▁▁█
solid-start.minimal 63.23 KiB +13.65 KiB (+27.53%) 192.52 KiB 55.93 KiB ▁▁▁▁▁▁▁▁▁▁▁█
solid-start.full 67.13 KiB +11.76 KiB (+21.25%) 204.06 KiB 59.31 KiB ▁▁▁▁▁▁▁▁▁▁▁█

Trend sparkline is historical gzip bytes ending with this PR measurement; lower is better.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 30, 2026

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/@tanstack/arktype-adapter@7510

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@7510

@tanstack/history

npm i https://pkg.pr.new/@tanstack/history@7510

@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/@tanstack/nitro-v2-vite-plugin@7510

@tanstack/react-router

npm i https://pkg.pr.new/@tanstack/react-router@7510

@tanstack/react-router-devtools

npm i https://pkg.pr.new/@tanstack/react-router-devtools@7510

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/@tanstack/react-router-ssr-query@7510

@tanstack/react-start

npm i https://pkg.pr.new/@tanstack/react-start@7510

@tanstack/react-start-client

npm i https://pkg.pr.new/@tanstack/react-start-client@7510

@tanstack/react-start-server

npm i https://pkg.pr.new/@tanstack/react-start-server@7510

@tanstack/router-cli

npm i https://pkg.pr.new/@tanstack/router-cli@7510

@tanstack/router-core

npm i https://pkg.pr.new/@tanstack/router-core@7510

@tanstack/router-devtools

npm i https://pkg.pr.new/@tanstack/router-devtools@7510

@tanstack/router-devtools-core

npm i https://pkg.pr.new/@tanstack/router-devtools-core@7510

@tanstack/router-generator

npm i https://pkg.pr.new/@tanstack/router-generator@7510

@tanstack/router-plugin

npm i https://pkg.pr.new/@tanstack/router-plugin@7510

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/@tanstack/router-ssr-query-core@7510

@tanstack/router-utils

npm i https://pkg.pr.new/@tanstack/router-utils@7510

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/@tanstack/router-vite-plugin@7510

@tanstack/solid-router

npm i https://pkg.pr.new/@tanstack/solid-router@7510

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/@tanstack/solid-router-devtools@7510

@tanstack/solid-router-ssr-query

npm i https://pkg.pr.new/@tanstack/solid-router-ssr-query@7510

@tanstack/solid-start

npm i https://pkg.pr.new/@tanstack/solid-start@7510

@tanstack/solid-start-client

npm i https://pkg.pr.new/@tanstack/solid-start-client@7510

@tanstack/solid-start-server

npm i https://pkg.pr.new/@tanstack/solid-start-server@7510

@tanstack/start-client-core

npm i https://pkg.pr.new/@tanstack/start-client-core@7510

@tanstack/start-fn-stubs

npm i https://pkg.pr.new/@tanstack/start-fn-stubs@7510

@tanstack/start-plugin-core

npm i https://pkg.pr.new/@tanstack/start-plugin-core@7510

@tanstack/start-server-core

npm i https://pkg.pr.new/@tanstack/start-server-core@7510

@tanstack/start-static-server-functions

npm i https://pkg.pr.new/@tanstack/start-static-server-functions@7510

@tanstack/start-storage-context

npm i https://pkg.pr.new/@tanstack/start-storage-context@7510

@tanstack/valibot-adapter

npm i https://pkg.pr.new/@tanstack/valibot-adapter@7510

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/@tanstack/virtual-file-routes@7510

@tanstack/vue-router

npm i https://pkg.pr.new/@tanstack/vue-router@7510

@tanstack/vue-router-devtools

npm i https://pkg.pr.new/@tanstack/vue-router-devtools@7510

@tanstack/vue-router-ssr-query

npm i https://pkg.pr.new/@tanstack/vue-router-ssr-query@7510

@tanstack/vue-start

npm i https://pkg.pr.new/@tanstack/vue-start@7510

@tanstack/vue-start-client

npm i https://pkg.pr.new/@tanstack/vue-start-client@7510

@tanstack/vue-start-server

npm i https://pkg.pr.new/@tanstack/vue-start-server@7510

@tanstack/zod-adapter

npm i https://pkg.pr.new/@tanstack/zod-adapter@7510

commit: 7207ebd

Copy link
Copy Markdown
Contributor

@nx-cloud nx-cloud Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

At least one additional CI pipeline execution has run since the conclusion below was written and it may no longer be applicable.

Nx Cloud is proposing a fix for your failed CI:

We introduced a nonceAttr helper in useTags() that only includes the nonce key in element attrs when the nonce is defined (server-side), preventing { nonce: undefined } from being spread onto hydrated elements. Previously, Solid would treat nonce={undefined} during hydration as an instruction to remove the attribute, stripping the server-rendered nonce from every <style>, <link>, <script>, and <meta> element and causing all CSP tests to fail. This change preserves the server-rendered nonce through hydration and restores passing CSP compliance.

Warning

We could not verify this fix.

Suggested Fix changes
diff --git a/packages/solid-router/src/headContentUtils.tsx b/packages/solid-router/src/headContentUtils.tsx
index beddfe3d..b5f3f06a 100644
--- a/packages/solid-router/src/headContentUtils.tsx
+++ b/packages/solid-router/src/headContentUtils.tsx
@@ -17,6 +17,10 @@ import type {
 export const useTags = (assetCrossOrigin?: AssetCrossOriginConfig) => {
   const router = useRouter()
   const nonce = router.options.ssr?.nonce
+  // Only include nonce in element attrs when it is defined. Spreading
+  // `{ nonce: undefined }` onto a hydrated element causes Solid to *remove*
+  // the nonce attribute that was rendered by the server, which breaks CSP.
+  const nonceAttr = nonce != null ? { nonce } : {}
   const getTagKey = (tag: RouterManagedTag) => JSON.stringify(tag)
   const activeMatches = Solid.createMemo(
     () => router.stores.activeMatchesSnapshot.state,
@@ -74,7 +78,7 @@ export const useTags = (assetCrossOrigin?: AssetCrossOriginConfig) => {
             tag: 'meta',
             attrs: {
               ...m,
-              nonce,
+              ...nonceAttr,
             },
           })
         }
@@ -109,7 +113,7 @@ export const useTags = (assetCrossOrigin?: AssetCrossOriginConfig) => {
         tag: 'link',
         attrs: {
           ...link,
-          nonce,
+          ...nonceAttr,
         },
       })) satisfies Array<RouterManagedTag>
 
@@ -129,7 +133,7 @@ export const useTags = (assetCrossOrigin?: AssetCrossOriginConfig) => {
               crossOrigin:
                 getAssetCrossOrigin(assetCrossOrigin, 'stylesheet') ??
                 asset.attrs?.crossOrigin,
-              nonce,
+              ...nonceAttr,
             },
           }) satisfies RouterManagedTag,
       )
@@ -156,7 +160,7 @@ export const useTags = (assetCrossOrigin?: AssetCrossOriginConfig) => {
                 crossOrigin:
                   getAssetCrossOrigin(assetCrossOrigin, 'modulepreload') ??
                   preloadLink.crossOrigin,
-                nonce,
+                ...nonceAttr,
               },
             })
           }),
@@ -175,7 +179,7 @@ export const useTags = (assetCrossOrigin?: AssetCrossOriginConfig) => {
       tag: 'style',
       attrs: {
         ...style,
-        nonce,
+        ...nonceAttr,
       },
       children,
     })),
@@ -191,7 +195,7 @@ export const useTags = (assetCrossOrigin?: AssetCrossOriginConfig) => {
       tag: 'script',
       attrs: {
         ...script,
-        nonce,
+        ...nonceAttr,
       },
       children,
     })),

Apply fix via Nx Cloud  Reject fix via Nx Cloud


Or Apply changes locally with:

npx nx-cloud apply-locally mXtR-Jg7l

Apply fix locally with your editor ↗   View interactive diff ↗



🎓 Learn more about Self-Healing CI on nx.dev

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 30, 2026

Merging this PR will improve performance by 12.25%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 5 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
client-side navigation loop (solid) 72.6 ms 64.7 ms +12.25%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix/solid-router-headcontent-hydration-fouc (7207ebd) with solid-router-v2-pre (67a9040)1

Open in CodSpeed

Footnotes

  1. No successful run was found on solid-router-v2-pre (2edb597) during the generation of this report, so 67a9040 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@brenelz brenelz requested a review from a team as a code owner May 30, 2026 13:53
@schiller-manuel schiller-manuel requested a review from birkskyum May 30, 2026 14:31
@birkskyum birkskyum merged commit 129d043 into solid-router-v2-pre May 30, 2026
20 checks passed
@birkskyum birkskyum deleted the fix/solid-router-headcontent-hydration-fouc branch May 30, 2026 22:41
// When <HeadContent /> is placed in <head> (the SSR/hydration case), the node
// is already in document.head, so this is a no-op and the element is left
// exactly where Solid hydrated it.
function useRelocateToHead(getEl: () => Node | undefined) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could consider being strict here like react and just requiring the HeadContent to be in head

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants