Skip to content

fix: issue 7240 causing fouc#7250

Open
brenelz wants to merge 2 commits intomainfrom
fix-issue-7240
Open

fix: issue 7240 causing fouc#7250
brenelz wants to merge 2 commits intomainfrom
fix-issue-7240

Conversation

@brenelz
Copy link
Copy Markdown
Contributor

@brenelz brenelz commented Apr 24, 2026

Closes issue #7240

Summary by CodeRabbit

  • Refactor

    • Improved tag comparison to preserve existing tag references, reducing unnecessary DOM updates and ensuring stylesheet elements persist across route navigations.
  • Tests

    • Added test coverage confirming stylesheet link elements remain mounted and are not duplicated when navigating between routes with differing preload configurations.
  • Chores

    • Recorded a patch release entry describing the change.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 24, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9d044c1f-474e-428d-8947-f62d59ef61ea

📥 Commits

Reviewing files that changed from the base of the PR and between afe9e77 and 5ff1616.

📒 Files selected for processing (1)
  • .changeset/twenty-tools-lose.md
✅ Files skipped from review due to trivial changes (1)
  • .changeset/twenty-tools-lose.md

📝 Walkthrough

Walkthrough

useTags was changed to use a new local replaceEqualTags that compares RouterManagedTag entries via JSON.stringify and reuses matching previous object references. A test was added to ensure SSR stylesheet link elements remain mounted and not duplicated when navigating between routes with differing preload arrays.

Changes

Cohort / File(s) Summary
Tag Comparison Refactoring
packages/solid-router/src/headContentUtils.tsx
Replaced external replaceEqualDeep usage with a local replaceEqualTags that keys tags by JSON.stringify, reuses prev object references when keys match, and returns prev when arrays are equal under that rule.
SSR Stylesheet Persistence Test
packages/solid-router/tests/Scripts.test.tsx
Added a test that verifies a stylesheet <link rel="stylesheet" href="/main.css"> from the SSR manifest remains mounted (not duplicated or replaced) when navigating between routes whose manifests have different preloads.
Release Notes
.changeset/twenty-tools-lose.md
Added a changeset noting the tag comparison change and resulting stylesheet persistence behavior; bump type: patch.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped through tags both old and new,
I stringify to find the true,
I keep the old when keys align,
So stylesheets survive each line —
A happy hop from /a to /b. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: issue 7240 causing fouc' directly addresses the specific bug fix being implemented, referencing the issue number and the FOUC (flash of unstyled content) problem that was being fixed.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-issue-7240

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 Apr 24, 2026

View your CI Pipeline Execution ↗ for commit 5ff1616

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

☁️ Nx Cloud last updated this comment at 2026-04-24 21:09:54 UTC

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 24, 2026

🚀 Changeset Version Preview

1 package(s) bumped directly, 3 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/solid-router 1.168.20 → 1.168.21 Changeset
@tanstack/solid-start 1.167.40 → 1.167.41 Dependent
@tanstack/solid-start-client 1.166.36 → 1.166.37 Dependent
@tanstack/solid-start-server 1.166.37 → 1.166.38 Dependent

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 24, 2026

More templates

@tanstack/arktype-adapter

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

@tanstack/eslint-plugin-router

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

@tanstack/eslint-plugin-start

npm i https://pkg.pr.new/@tanstack/eslint-plugin-start@7250

@tanstack/history

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

@tanstack/nitro-v2-vite-plugin

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

@tanstack/react-router

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

@tanstack/react-router-devtools

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

@tanstack/react-router-ssr-query

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

@tanstack/react-start

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

@tanstack/react-start-client

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

@tanstack/react-start-rsc

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

@tanstack/react-start-server

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

@tanstack/router-cli

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

@tanstack/router-core

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

@tanstack/router-devtools

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

@tanstack/router-devtools-core

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

@tanstack/router-generator

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

@tanstack/router-plugin

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

@tanstack/router-ssr-query-core

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

@tanstack/router-utils

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

@tanstack/router-vite-plugin

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

@tanstack/solid-router

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

@tanstack/solid-router-devtools

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

@tanstack/solid-router-ssr-query

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

@tanstack/solid-start

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

@tanstack/solid-start-client

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

@tanstack/solid-start-server

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

@tanstack/start-client-core

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

@tanstack/start-fn-stubs

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

@tanstack/start-plugin-core

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

@tanstack/start-server-core

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

@tanstack/start-static-server-functions

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

@tanstack/start-storage-context

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

@tanstack/valibot-adapter

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

@tanstack/virtual-file-routes

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

@tanstack/vue-router

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

@tanstack/vue-router-devtools

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

@tanstack/vue-router-ssr-query

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

@tanstack/vue-start

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

@tanstack/vue-start-client

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

@tanstack/vue-start-server

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

@tanstack/zod-adapter

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

commit: 5ff1616

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 24, 2026

Bundle Size Benchmarks

  • Commit: a5385dbd90a8
  • Measured at: 2026-04-24T21:04:49.742Z
  • Baseline source: history:a5385dbd90a8
  • Dashboard: bundle-size history
Scenario Current (gzip) Delta vs baseline Raw Brotli Trend
react-router.minimal 87.35 KiB 0 B (0.00%) 274.60 KiB 75.97 KiB ▅▅▅▅▅▅▅▅▅▅▅
react-router.full 90.63 KiB 0 B (0.00%) 285.74 KiB 78.87 KiB ▁██████████
solid-router.minimal 35.55 KiB 0 B (0.00%) 106.71 KiB 31.96 KiB ▁▁▁▅███████
solid-router.full 40.09 KiB +71 B (+0.17%) 120.41 KiB 36.00 KiB ▁▁▁▃▃▃▃▃▃▃▃█
vue-router.minimal 53.30 KiB 0 B (0.00%) 152.01 KiB 47.88 KiB ▅▅▅▅▅▅▅▅▅▅▅
vue-router.full 58.20 KiB 0 B (0.00%) 167.43 KiB 52.06 KiB ▅▅▅▅▅▅▅▅▅▅▅
react-start.minimal 101.77 KiB 0 B (0.00%) 322.39 KiB 88.05 KiB ▁██████████
react-start.full 105.21 KiB 0 B (0.00%) 332.72 KiB 90.89 KiB ▁██████████
solid-start.minimal 49.53 KiB 0 B (0.00%) 152.52 KiB 43.68 KiB ▁▁▁█▇▇▇▇▇▇▇
solid-start.full 55.13 KiB +61 B (+0.11%) 168.93 KiB 48.54 KiB ▁▁▁▂▄▄▄▄▄▄▄█

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

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
packages/solid-router/tests/Scripts.test.tsx (1)

226-315: Test targets the bug scenario well.

The asymmetric preload arrays (['/a.js'] vs ['/b.js', '/b-child.js']) are exactly what shifts the stylesheet tag's index across the combined tags array, which was the scenario where the old replaceEqualDeep lost the reference. Asserting element identity (Line 309) plus dedup count (Lines 310–314) is the right pair of assertions.

Optional: consider adding a /b → /a click in the same test (or a second iteration) to also assert the reference survives navigating back, similar to the 5× loop in the existing test at Lines 200–216. Not required for this fix.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/solid-router/tests/Scripts.test.tsx` around lines 226 - 315, Add a
back-navigation check to the test "keeps manifest stylesheet links mounted when
preload counts change": after asserting the stylesheet link identity and dedupe
count, simulate clicking the "Go to A" link (use screen.getByRole to find the
link and fireEvent.click), wait for router.state.location.pathname to be '/a'
(or use screen.findByRole for the "Go to B" link), then re-check that
getStylesheetLink() returns the same initialLink and that only one link with
href '/main.css' exists; this mirrors the existing 5× loop behavior and ensures
the stylesheet reference survives navigating back.
packages/solid-router/src/headContentUtils.tsx (1)

197-241: Correct fix; optional perf refactor to dedupe JSON.stringify work.

Switching from the index-paired replaceEqualDeep to a Map keyed by JSON.stringify(tag) is the right fix: when preload counts change across routes, the stylesheet tag's position in the combined tags array shifts, and the old index-based diff would replace the reference even though the tag is identical — which is exactly what triggers the FOUC. Keying by content lookups preserves the reference regardless of index. Duplicate-key collisions in the Map aren't a concern because prev was already deduped by the same JSON key in the previous memo run via uniqBy (line 206–208).

One optional optimization: JSON.stringify runs once per tag in uniqBy on Line 207, then again for every prev tag on Line 223 and every next tag on Line 228. Head tags with large children (inline styles, JSON‑LD) make this noticeably hotter than it needs to be on every navigation. You can thread the keys through from the uniqBy step so each tag is stringified at most once:

♻️ Proposed optional refactor
   return Solid.createMemo((prev: Array<RouterManagedTag> | undefined) => {
-    const next = uniqBy(
-      [
+    const combined = [
         ...meta(),
         ...preloadLinks(),
         ...links(),
         ...styles(),
         ...headScripts(),
-      ] as Array<RouterManagedTag>,
-      (d) => {
-        return JSON.stringify(d)
-      },
-    )
+    ] as Array<RouterManagedTag>
+    const { items: next, keys: nextKeys } = uniqByWithKeys(combined, (d) =>
+      JSON.stringify(d),
+    )
     if (prev === undefined) {
       return next
     }
-    return replaceEqualTags(prev, next)
+    return replaceEqualTags(prev, next, nextKeys)
   })
 }

-function replaceEqualTags(
-  prev: Array<RouterManagedTag>,
-  next: Array<RouterManagedTag>,
-) {
-  const prevByKey = new Map<string, RouterManagedTag>()
-  for (const tag of prev) {
-    prevByKey.set(JSON.stringify(tag), tag)
-  }
-
-  let isEqual = prev.length === next.length
-  const result = next.map((tag, index) => {
-    const existing = prevByKey.get(JSON.stringify(tag))
+function replaceEqualTags(
+  prev: Array<RouterManagedTag>,
+  next: Array<RouterManagedTag>,
+  nextKeys: Array<string>,
+) {
+  const prevByKey = new Map<string, RouterManagedTag>()
+  for (const tag of prev) {
+    prevByKey.set(JSON.stringify(tag), tag)
+  }
+
+  let isEqual = prev.length === next.length
+  const result = next.map((tag, index) => {
+    const existing = prevByKey.get(nextKeys[index]!)
     if (existing) {
       if (existing !== prev[index]) {
         isEqual = false
       }
       return existing
     }
     isEqual = false
     return tag
   })
   return isEqual ? prev : result
 }

(With a small uniqByWithKeys helper that returns both the filtered items and their computed keys. Further: you could also cache the prev keys from the previous memo run to avoid re-stringifying prev as well.)

Not a blocker — the current implementation is correct and the fix itself is clean.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/solid-router/src/headContentUtils.tsx` around lines 197 - 241, The
current fix is correct but we should avoid redundant JSON.stringify calls by
threading computed keys through the memo: create a helper (e.g., uniqByWithKeys)
that returns both the deduped items and their computed keys, replace the
uniqBy(...) call in the Solid.createMemo callback with this helper to obtain
next and nextKeys, build prevByKey using the corresponding prevKeys (cache
prevKeys alongside prev in the memo closure), and in replaceEqualTags use the
precomputed keys (instead of calling JSON.stringify again) to look up existing
tags and to compare items when building result so each tag is stringified at
most once; update code that references prev, next, prevByKey, and result
accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/solid-router/src/headContentUtils.tsx`:
- Around line 197-241: The current fix is correct but we should avoid redundant
JSON.stringify calls by threading computed keys through the memo: create a
helper (e.g., uniqByWithKeys) that returns both the deduped items and their
computed keys, replace the uniqBy(...) call in the Solid.createMemo callback
with this helper to obtain next and nextKeys, build prevByKey using the
corresponding prevKeys (cache prevKeys alongside prev in the memo closure), and
in replaceEqualTags use the precomputed keys (instead of calling JSON.stringify
again) to look up existing tags and to compare items when building result so
each tag is stringified at most once; update code that references prev, next,
prevByKey, and result accordingly.

In `@packages/solid-router/tests/Scripts.test.tsx`:
- Around line 226-315: Add a back-navigation check to the test "keeps manifest
stylesheet links mounted when preload counts change": after asserting the
stylesheet link identity and dedupe count, simulate clicking the "Go to A" link
(use screen.getByRole to find the link and fireEvent.click), wait for
router.state.location.pathname to be '/a' (or use screen.findByRole for the "Go
to B" link), then re-check that getStylesheetLink() returns the same initialLink
and that only one link with href '/main.css' exists; this mirrors the existing
5× loop behavior and ensures the stylesheet reference survives navigating back.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e59af330-65c5-481e-96cf-f0c637eb1f9b

📥 Commits

Reviewing files that changed from the base of the PR and between 03ac20e and afe9e77.

📒 Files selected for processing (2)
  • packages/solid-router/src/headContentUtils.tsx
  • packages/solid-router/tests/Scripts.test.tsx

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 24, 2026

Merging this PR will not alter performance

✅ 5 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing fix-issue-7240 (5ff1616) with main (a5385db)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports.

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.

1 participant