Inline scripts are duplicated after hydration when the CSP nonce is sent in a header
维护者通常 1 天内回复
评估
- 难度
- 1/5
- 预计耗时
- 1 小时以内
- 新手友好度
- 90/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- react, typescript
- 领域
- frontend
调研方向
从 packages/react-router/src/Asset.tsx 中第 204 行附近的 inline script 查找逻辑开始。检查 hydration 期间如何读取现有 script 的 nonce,然后使用链接的最小 reproducer 或在 response header 中使用 CSP nonce 的 Start 应用验证行为。完成标准是匹配到服务端渲染的 script,且不会追加重复的 script。
由索引模型根据 Issue 内容生成。
描述
Which project does this relate to?
Router
Describe the bug
After hydration, Script looks for an existing inline script with the same text, type and nonce before it creates a new one:
When the CSP arrives in a response header, browsers apply nonce hiding. On a connected element, getAttribute('nonce') returns '', and only the .nonce property keeps the value. So the comparison is '' vs. the real nonce, and the server-rendered script never matches. Script then creates a new <script type="module" nonce>import("/assets/index-….js")</script>, sets its textContent, and appends it to <head>.
Complete minimal reproducer
https://github.com/gabriel-le/tanstack-nonce-inline-scripts-duplication
Steps to Reproduce the Bug
This works in any Start app:
- Set
ssr.nonceon the router and sendContent-Security-Policy: script-src 'nonce-<n>'as a response header. - Load a page.
document.head.querySelectorAll('script[type=module]')includes a re-addedimport("/assets/…")script. ItsgetAttribute('nonce')is'', and its.noncehas the value. - Control: send the same policy in a
<meta http-equiv>, or send no policy. Nonces aren't hidden, the lookup matches, and nothing is added. - Add
; require-trusted-types-for 'script'to the header. Hydration fails with the error above.
I tested this using react-router, but the issue is likely the same in solid and vue
Expected behavior
The lookup matches the server-rendered script, and nothing is created or appended.
Proposed fix
- const sNonce = el.getAttribute('nonce') ?? undefined
+ const sNonce = el.nonce || undefined
The fix uses || rather than ?? because .nonce is '' when an element has no nonce. With ??, apps that don't use a nonce would start creating duplicates.
Screenshots or Videos
No response
Platform
- Router / Start Version: 1.170.40
- OS: macOS
- Browser: Tested in chrome, safari and firefox
- Browser Version: Safari Version 27.2 (22625.2.5.11.1)
- Bundler: vite
- Bundler Version: 8.0.0
Additional context
No response
- 主要语言
- TypeScript
- 星标
- 15.2k
- 派生
- 1.9k
- 平均合并
- 1 天 13 小时
- 30 天内合并 PR
- 76
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/router 的其他 Issue
-
router.startViewTransition leaves the ready/finished promises unhandled, so a skipped transition throws an unhandled AbortError可能已有人在做 @unjica 于 2 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 74/100
TanStack/router#8776 · 3 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
Router devtools crash when search params contain a BigInt (JSON.stringify in history tracking)可能已有人在做 @A-Raphie 于 6 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
docs: `createLink` has no API reference page; 3 how-to guides link to a missing `#createlink` anchor可能已有人在做 @A-Raphie 于 6 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复
-
start-plugin-core: sitemap lastmod and pages.json lastBuilt are stamped with the build's wall clock (no SOURCE_DATE_EPOCH support)可能已有人在做 @treadnot 于 28 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
Empty .ts route file is scaffolded with JSX, breaking all route generation可能已有人在做 @nathan-logan 于 32 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
相似的 Issue
-
[Bug]: Device screenshot fails with 500 on iPhone Duo (Xcode 27.2): serve-sim calls simctl without --display可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 66/100
pingdotgg/t3code#24900 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
inu-appcenter/UNIDorm-web#411 ·
维护者通常 1 天内回复
-
🐛 bug
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
ranxianglei/billion-context#4720 ·
维护者通常 1 天内回复
-
area:arcade game p3-medium ready-for-agent type:bug
难度 2/5 1-3 小时 新手友好度 82/100
fderuiter/deruiter.dev#2840 ·
维护者通常 1 天内回复