Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Inline scripts are duplicated after hydration when the CSP nonce is sent in a header

未关闭 适合新手
#8,550 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@gabriel-le 已经在做这个了。

开始于 2026年9月28日。

  • #8551 来自 @gabriel-le —— 未关闭

评估

难度
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:

https://github.com/TanStack/router/blob/41ebd288677beacb8eb56953f5f8c26e9d33313e/packages/react-router/src/Asset.tsx#L204

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:

  1. Set ssr.nonce on the router and send Content-Security-Policy: script-src 'nonce-<n>' as a response header.
  2. Load a page. document.head.querySelectorAll('script[type=module]') includes a re-added import("/assets/…") script. Its getAttribute('nonce') is '', and its .nonce has the value.
  3. 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.
  4. 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

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/router 的其他 Issue

查看 TanStack/router 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。