Skip to content

fix(runtime): align URL query serialization with browser behavior#19166

Open
bigbigDreamer wants to merge 1 commit intoNervJS:mainfrom
bigbigDreamer:fix/url-constructor-behavior
Open

fix(runtime): align URL query serialization with browser behavior#19166
bigbigDreamer wants to merge 1 commit intoNervJS:mainfrom
bigbigDreamer:fix/url-constructor-behavior

Conversation

@bigbigDreamer
Copy link
Copy Markdown
Contributor

@bigbigDreamer bigbigDreamer commented Apr 26, 2026

这个 PR 做了什么? (简要描述所做更改)

修复小程序端 URL 在构造后过早使用 URLSearchParams.toString() 重写 query 的问题。

此前 new URL('https://example.test/?name=|aaa') 后,url.search / url.href 会被序列化为 ?name=%7Caaa,与浏览器 WHATWG URL 行为不一致。按 Web 平台 URL 标准,URL query 序列化不应在构造阶段使用 application/x-www-form-urlencoded 规则;只有在修改 searchParams 后,才应将参数列表重新序列化并写回 URL。

标准依据:

本 PR 调整了 TaroURLURLSearchParams 的联动逻辑:

  • new URL() 后保留原始 search 表达,避免提前 encode query 参数。
  • searchParams.append/set/delete 后再触发 URL query 更新。
  • 补充 name=|aaa 场景测试,确保 url.search / url.hrefurl.searchParams.toString() 分别符合对应序列化规则。

这个 PR 是什么类型? (至少选择一个)

  • 错误修复 (Bugfix) issue: fix #
  • 新功能 (Feature)
  • 代码重构 (Refactor)
  • TypeScript 类型定义修改 (Types)
  • 文档修改 (Docs)
  • 代码风格更新 (Code style update)
  • 构建优化 (Chore)
  • 其他,请描述 (Other, please describe):

这个 PR 涉及以下平台:

  • 所有平台
  • Web 端(H5)
  • 移动端(React-Native)
  • 鸿蒙(Harmony)
  • 鸿蒙容器(Harmony Hybrid)
  • ASCF 元服务
  • 快应用(QuickApp)
  • 所有小程序
  • 微信小程序
  • 企业微信小程序
  • 京东小程序
  • 百度小程序
  • 支付宝小程序
  • 支付宝 IOT 小程序
  • 钉钉小程序
  • QQ 小程序
  • 飞书小程序
  • 快手小程序
  • 头条小程序

Summary by CodeRabbit

发布说明

  • Bug 修复

    • 优化URL查询参数处理机制,确保特殊字符正确保留和编码
  • 测试

    • 新增URL和查询参数特殊字符处理的测试覆盖,验证特殊字符的编码和解码行为

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 26, 2026

Walkthrough

重构URL和URLSearchParams实现以保留原始搜索字符串。URL类通过私有字段追踪搜索参数修改状态,未修改时返回原始值。URLSearchParams重构为命名导出类,支持注册变更回调以标记搜索已修改。添加测试验证管道符号处理。

Changes

Cohort / File(s) Summary
URL和URLSearchParams重构
packages/taro-runtime/src/bom/URL.ts, packages/taro-runtime/src/bom/URLSearchParams.ts
URL类新增私有字段跟踪搜索修改状态,未修改时返回原始搜索字符串,修改时返回序列化值。URLSearchParams重构为导出类TaroURLSearchParams,添加变更回调机制,在appenddeleteset操作后触发回调。
搜索参数测试
packages/taro-runtime/tests/location.spec.ts
新增测试覆盖URL中包含管道符号(`

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested reviewers

  • yoyo837

Poem

🐰 兔子跳跃欢欣忙,
搜索字符串保原样,
修改追踪真妙哉,
URLSearchParams换新装,
管道符号测试详~

🚥 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.
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.
Title check ✅ Passed 标题清晰准确地总结了主要变更:修复 URL 查询字符串序列化以符合浏览器行为标准。

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot requested a review from yoyo837 April 26, 2026 02:49
@bigbigDreamer
Copy link
Copy Markdown
Contributor Author

这个变更应该被明确标记为 break 更新,因为会出现别的小程序平台已经有人把错的行为标准化使用

Copy link
Copy Markdown

@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 (1)
packages/taro-runtime/src/bom/URLSearchParams.ts (1)

85-87: 可选优化:_setOnChange 当前是公开方法。

下划线只是命名约定,外部代码(甚至业务侧)通过 url.searchParams._setOnChange(undefined) 即可清除回调,导致后续 url.search / url.toString() 不再反映 searchParams 的变更,且静默失败、难以排查。如需更严格的隔离,可考虑用模块级 SymbolWeakMap 来注册回调,仅在 URL.ts 中可见。

♻️ 参考实现(Symbol 方案)
+export const SET_ON_CHANGE = Symbol('TaroURLSearchParams.setOnChange')
+
 export class TaroURLSearchParams {
   `#dict` = Object.create(null)
   `#onChange`?: () => void
   ...
-  _setOnChange (onChange?: () => void) {
+  [SET_ON_CHANGE] (onChange?: () => void) {
     this.#onChange = onChange
   }

并在 URL.ts#setSearch 中改用 this.#search[SET_ON_CHANGE](...)

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

In `@packages/taro-runtime/src/bom/URLSearchParams.ts` around lines 85 - 87, The
_setOnChange method on URLSearchParams is currently publicly callable which
allows external code to clear the update callback; change the implementation to
hide the setter by registering the onChange callback via a module-scoped symbol
or WeakMap so only URL.ts can access it (e.g. define a unique SET_ON_CHANGE
Symbol or a WeakMap in the module, export nothing, and replace calls to
url.searchParams._setOnChange(...) with this.[SET_ON_CHANGE](...) inside
URL.ts); update URLSearchParams.ts to remove or make _setOnChange internal and
expose the new module-scoped accessor, and update URL.ts to use the
symbol/WeakMap key when calling into the searchParams instance.
🤖 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/taro-runtime/src/bom/URLSearchParams.ts`:
- Around line 85-87: The _setOnChange method on URLSearchParams is currently
publicly callable which allows external code to clear the update callback;
change the implementation to hide the setter by registering the onChange
callback via a module-scoped symbol or WeakMap so only URL.ts can access it
(e.g. define a unique SET_ON_CHANGE Symbol or a WeakMap in the module, export
nothing, and replace calls to url.searchParams._setOnChange(...) with
this.[SET_ON_CHANGE](...) inside URL.ts); update URLSearchParams.ts to remove or
make _setOnChange internal and expose the new module-scoped accessor, and update
URL.ts to use the symbol/WeakMap key when calling into the searchParams
instance.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9eaf0d1d-cc55-4ff0-8704-afc83aad3c62

📥 Commits

Reviewing files that changed from the base of the PR and between 9c6c511 and 70fddb9.

📒 Files selected for processing (3)
  • packages/taro-runtime/src/bom/URL.ts
  • packages/taro-runtime/src/bom/URLSearchParams.ts
  • packages/taro-runtime/tests/location.spec.ts

@bigbigDreamer bigbigDreamer changed the title fix: URL constructor behavior fix(runtime): align URL query serialization with browser behavior Apr 26, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 56.09%. Comparing base (9c6c511) to head (70fddb9).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #19166      +/-   ##
==========================================
+ Coverage   56.07%   56.09%   +0.02%     
==========================================
  Files         447      447              
  Lines       23454    23469      +15     
  Branches     5780     5782       +2     
==========================================
+ Hits        13151    13166      +15     
+ Misses       8448     8442       -6     
- Partials     1855     1861       +6     
Flag Coverage Δ
taro-cli 72.85% <ø> (ø)
taro-runtime 58.53% <100.00%> (+0.19%) ⬆️
taro-web 53.12% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/taro-runtime/src/bom/URL.ts 95.80% <100.00%> (+0.31%) ⬆️
packages/taro-runtime/src/bom/URLSearchParams.ts 81.01% <100.00%> (+1.28%) ⬆️

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This was referenced May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant