Skip to content

fix: 修复百度小程序 onLoad onTabItemTap执行时序问题#18682

Merged
Single-Dancer merged 1 commit intomainfrom
fix/swan-onTabItemTap
Jan 8, 2026
Merged

fix: 修复百度小程序 onLoad onTabItemTap执行时序问题#18682
Single-Dancer merged 1 commit intomainfrom
fix/swan-onTabItemTap

Conversation

@zhengyanan1
Copy link
Copy Markdown
Contributor

@zhengyanan1 zhengyanan1 commented Dec 17, 2025

这个 PR 做了什么? (简要描述所做更改)
修复百度小程序 onLoad onTabItemTap执行时序问题

这个 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 Fixes
    • 优化在 Swan (百度) 小程序环境下的页面生命周期处理:相关生命周期处理器将立即执行,避免延迟或被推迟执行,确保小程序行为更稳定、响应更及时。

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 17, 2025

Walkthrough

在 Taro 运行时的页面生命周期处理器中新增对 Swan(百度)小程序环境的检测:当识别为 Swan 时,生命周期处理函数通过专用执行路径立即执行,绕过原有的延迟/分发执行逻辑。

Changes

Cohort / File(s) 变更摘要
Swan 环境判断
packages/taro-runtime/src/dsl/common.ts
新增 isSWAN 环境守卫;在页面生命周期处理器工厂中加入条件分支:当为 Swan 环境时通过 exec() 立即执行生命周期处理函数,短路原有的延迟/直接执行分支。

Sequence Diagram(s)

(此次改动为单一文件的环境判断与执行分支更改,不生成序列图)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested reviewers

  • tutuxxx

Poem

🐰 小兔轻轻跳一跳,
Swan 环境早知道,
生命周期即刻跑,
不再等也不迟到,
代码里欢喜笑梢梢。

🚥 Pre-merge checks | ✅ 2 | ❌ 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 (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 拉取请求的标题清晰地总结了主要变更:修复百度小程序中onLoad和onTabItemTap的执行时序问题,与代码更改完全相关。

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/swan-onTabItemTap

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.

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.

Actionable comments posted: 2

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4e186a3 and f287941.

📒 Files selected for processing (1)
  • packages/taro-runtime/src/dsl/common.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
  • GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-gnu
  • GitHub Check: Build Rust WASM / stable - wasm32-wasi
  • GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc

}
}

const isSWAN = process.env.TARO_ENV === 'swan'// 百度小程序
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Get context around line 240 and lines 262-264 to understand the full changes

echo "=== Lines around 240 ==="
sed -n '235,245p' packages/taro-runtime/src/dsl/common.ts

echo -e "\n=== Lines around 262-264 ==="
sed -n '257,270p' packages/taro-runtime/src/dsl/common.ts

echo -e "\n=== Check LIFECYCLES definition ==="
rg -n "const LIFECYCLES" packages/taro-runtime/src/dsl/common.ts -A 30 | head -60

Repository: NervJS/taro

Length of output: 826


🏁 Script executed:

#!/bin/bash
# Get the LIFECYCLES definition more completely
echo "=== LIFECYCLES definition ==="
rg -n "const LIFECYCLES" packages/taro-runtime/src/dsl/common.ts -A 50 | head -80

echo -e "\n=== Search for onTabItemTap mentions ==="
rg -n "onTabItemTap" packages/taro-runtime/src/dsl/common.ts

Repository: NervJS/taro

Length of output: 126


🏁 Script executed:

#!/bin/bash
# Try different approaches to find LIFECYCLES
echo "=== Search for LIFECYCLES ==="
rg -n "LIFECYCLES" packages/taro-runtime/src/dsl/common.ts | head -20

echo -e "\n=== Get file content around line 240-280 ==="
sed -n '230,280p' packages/taro-runtime/src/dsl/common.ts

echo -e "\n=== Search for onTabItemTap in entire codebase ==="
rg -n "onTabItemTap" packages/taro-runtime/src/ | head -10

Repository: NervJS/taro

Length of output: 1857


🏁 Script executed:

#!/bin/bash
# Find where LIFECYCLES is imported from and what it contains
echo "=== Check imports at the top of the file ==="
head -120 packages/taro-runtime/src/dsl/common.ts | tail -30

echo -e "\n=== Search for LIFECYCLES definition in other files ==="
rg -n "export.*LIFECYCLES" packages/taro-runtime/src/ | head -10

echo -e "\n=== Get the constants file ==="
rg -l "LIFECYCLES.*=" packages/taro-runtime/src/ | head -5

Repository: NervJS/taro

Length of output: 1241


🏁 Script executed:

#!/bin/bash
# Find getMiniLifecycleImpl to see what LIFECYCLES contains
echo "=== Search for getMiniLifecycleImpl ==="
rg -n "getMiniLifecycleImpl" packages/taro-runtime/src/ -A 30 | head -100

echo -e "\n=== Look for lifecycle definitions in constants or config files ==="
fd -e ts "constant|config|lifecycle" packages/taro-runtime/src/ | head -10

Repository: NervJS/taro

Length of output: 7225


🏁 Script executed:

#!/bin/bash
# Search for where getMiniLifecycleImpl hook is registered/implemented
echo "=== Search for hook registration of getMiniLifecycleImpl ==="
rg -n "getMiniLifecycleImpl" packages/taro-runtime/ -B 5 -A 20 | grep -v "^--$" | head -150

echo -e "\n=== Search in platform-specific files ==="
find packages/taro-runtime -name "*.ts" -type f | xargs rg -l "page.*LIFECYCLES" | head -10

Repository: NervJS/taro

Length of output: 6261


🏁 Script executed:

#!/bin/bash
# Search for getMiniLifecycleImpl implementation in plugins
echo "=== Search for getMiniLifecycleImpl implementation ==="
find packages -name "*.ts" -type f | xargs rg -l "getMiniLifecycleImpl.*=" | head -10

echo -e "\n=== Look for swan-specific lifecycle definitions ==="
find packages -path "*swan*" -name "*.ts" | xargs rg -l "LIFECYCLES\|onTabItemTap" 2>/dev/null | head -10

echo -e "\n=== Check plugin-platform-swan directory ==="
ls -la packages/ | grep swan

Repository: NervJS/taro

Length of output: 315


🏁 Script executed:

#!/bin/bash
# Check the runtime-hooks implementation
echo "=== getMiniLifecycleImpl implementation ==="
rg -n "getMiniLifecycleImpl" packages/shared/src/runtime-hooks.ts -A 30 | head -60

echo -e "\n=== Check swan platform plugin ==="
find packages/taro-platform-swan -name "*.ts" | head -20

echo -e "\n=== Look for lifecycle definitions in swan plugin ==="
rg -n "LIFECYCLES\|onTabItemTap" packages/taro-platform-swan -A 5 | head -80

Repository: NervJS/taro

Length of output: 3118


🏁 Script executed:

#!/bin/bash
# Search for defaultMiniLifecycle and swan-specific definitions
echo "=== Search for defaultMiniLifecycle ==="
rg -n "defaultMiniLifecycle\|const.*LIFECYCLES" packages/shared/src/ -B 3 -A 20 | head -100

echo -e "\n=== Search for swan runtime implementation ==="
cat packages/taro-platform-swan/src/runtime.ts | head -100

echo -e "\n=== Search for onTabItemTap in entire codebase ==="
rg -n "onTabItemTap" packages/ --type ts | head -20

Repository: NervJS/taro

Length of output: 2769


🏁 Script executed:

#!/bin/bash
# Find the lifecycle definitions
echo "=== Search for lifecycle definitions in taro-with-weapp ==="
rg -n "onTabItemTap" packages/taro-with-weapp/src/lifecycle.ts -B 20 -A 5

echo -e "\n=== Search for defaultMiniLifecycle or LIFECYCLES definition ==="
rg -n "export.*LIFECYCLES.*=|const.*LIFECYCLES.*=" packages/ --type ts -A 3 | head -80

echo -e "\n=== Look at taro-with-weapp lifecycle exports ==="
cat packages/taro-with-weapp/src/lifecycle.ts | head -100

Repository: NervJS/taro

Length of output: 2944


🏁 Script executed:

#!/bin/bash
# Search for getMiniLifecycle (without Impl) which is called by getMiniLifecycleImpl
echo "=== Search for getMiniLifecycle hook implementation ==="
rg -n "getMiniLifecycle['\"].*:" packages/shared/src/runtime-hooks.ts -A 20 | head -100

echo -e "\n=== Search for defaultMiniLifecycle in shared ==="
find packages/shared -name "*.ts" | xargs rg -l "LIFECYCLES\|defaultMiniLifecycle" | head -10

echo -e "\n=== Check for lifecycle constants in shared ==="
rg -n "export.*=" packages/shared/src/ | grep -i lifecycle | head -30

Repository: NervJS/taro

Length of output: 210


🏁 Script executed:

#!/bin/bash
# Search more broadly for LIFECYCLES array definition
echo "=== Search for lifecycle arrays/constants ==="
rg -n "onLoad|onUnload|onShow|onHide|onReady" packages/taro-runtime/src/dsl/common.ts | head -20

echo -e "\n=== Look for the hooksMiniLifecycle or similar ==="
rg -n "hooksMiniLifecycle\|MiniLifecycle\|lifecycleImpl" packages/ --type ts | head -30

echo -e "\n=== Search specifically where LIFECYCLES contains ==="
rg -n "\['onLoad'\]|\['onShow'\]|\['onReady'\]" packages/shared/src/ -B 5 -A 5 | head -100

Repository: NervJS/taro

Length of output: 398


🏁 Script executed:

#!/bin/bash
# Search for platform-specific implementations
echo "=== Search for addHook or modifyHooks for getMiniLifecycle ==="
rg -n "addHook.*getMiniLifecycle\|'getMiniLifecycle'" packages/ --type ts -A 20 | head -150

echo -e "\n=== Check platform-weapp for lifecycle ==="
find packages/taro-platform-weapp -name "*.ts" | xargs rg -l "LIFECYCLES\|getMiniLifecycle" | head -5

echo -e "\n=== Check platform-swan for lifecycle ==="
find packages/taro-platform-swan -name "*.ts" | xargs rg -l "LIFECYCLES\|getMiniLifecycle" | head -5

Repository: NervJS/taro

Length of output: 204


🏁 Script executed:

#!/bin/bash
# Try to find where the lifecycle arrays are actually defined
echo "=== Search for 'ready' lifecycle marker ==="
rg -n "onReady|'ready'" packages/shared/src/ --type ts -B 3 -A 3 | head -100

echo -e "\n=== Search all TypeScript files for LIFECYCLES array pattern ==="
rg -n "LIFECYCLES\s*:\s*\[|LIFECYCLES\s*=\s*\[" packages/ --type ts -A 20 | head -100

echo -e "\n=== Check if LIFECYCLES comes from a hook implementation ==="
rg -n "call\('getMiniLifecycleImpl" packages/taro-runtime/src/dsl/common.ts -B 5 -A 15

Repository: NervJS/taro

Length of output: 3553


🏁 Script executed:

#!/bin/bash
# Now get the full defaultMiniLifecycle definition
echo "=== Get the full defaultMiniLifecycle definition ==="
sed -n '80,130p' packages/shared/src/runtime-hooks.ts

echo -e "\n=== Check if there are swan-specific modifications to lifecycles ==="
rg -n "modifyMiniLifecycles\|getMiniLifecycle" packages/taro-platform-swan/ -A 20 | head -100

echo -e "\n=== Look for swan lifecycle modifications ==="
find packages/taro-platform-swan -name "*.ts" | xargs cat | grep -A 50 -B 5 "lifecycle\|Lifecycle" | head -100

Repository: NervJS/taro

Length of output: 2765


环境检测方式正确,但应确认对所有生命周期的影响

使用 process.env.TARO_ENV === 'swan' 是 Taro 框架检测百度小程序环境的标准方式。

然而,第 262-264 行的修复会对 LIFECYCLES 数组中的所有生命周期立即执行,完全绕过 defer 机制。这包括 onPullDownRefresh、onReachBottom、onPageScroll 等。建议确认这是否为预期行为,以及是否可能对其他需要延迟执行的生命周期产生副作用。

🤖 Prompt for AI Agents
In packages/taro-runtime/src/dsl/common.ts around line 240 (isSWAN detection)
and lines 262-264, the change causes every lifecycle in LIFECYCLES to execute
immediately, bypassing the original defer mechanism; revert the unconditional
immediate execution and restore the defer behavior: only execute lifecycle
handlers immediately when explicitly allowed for swan (enumerate a whitelist
e.g. those safe to run immediately) or preserve the existing defer flag for each
lifecycle and respect it (if lifecycle.defer then push to deferred queue else
execute now); update the conditional to check lifecycle metadata (or whitelist)
before invoking and add a comment documenting why certain lifecycles are
immediate vs deferred.

@Single-Dancer Single-Dancer force-pushed the fix/swan-onTabItemTap branch from f287941 to b171d24 Compare January 8, 2026 08:01
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.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In @packages/taro-runtime/src/dsl/common.ts:
- Around line 262-264: Add an explanatory comment above the if (isSWAN) branch
in the exec/defer handling to state why Swan (isSWAN) lifecycles must bypass the
isDefer/hasLoaded check and run exec() immediately (e.g., platform-specific
timing/dispatch differences impacting onTabItemTap). Also consider narrowing the
exception: if the immediate run should only apply to a specific lifecycle,
change the condition to if (isSWAN && lifecycle === 'onTabItemTap') and document
that choice; update references to isDefer, exec, lifecycle and LIFECYCLES in the
comment so reviewers can see the rationale and the intended scope of the change.
🧹 Nitpick comments (1)
packages/taro-runtime/src/dsl/common.ts (1)

240-240: 常量定义合理。

常量命名清晰,注释到位。不过这个常量仅在下方的 LIFECYCLES.forEach 循环内使用,考虑将定义移到循环内部(第 241 行之后)以提高代码局部性,或者直接内联该检查。

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f287941 and b171d24.

📒 Files selected for processing (1)
  • packages/taro-runtime/src/dsl/common.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Build Rust Binding / stable - x86_64-apple-darwin
  • GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
  • GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-gnu
  • GitHub Check: Build Rust WASM / stable - wasm32-wasi
  • GitHub Check: Build Rust Binding / stable - aarch64-apple-darwin
  • GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
  • GitHub Check: Build Rust Binding / stable - x86_64-apple-darwin
  • GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-gnu

Comment on lines +262 to +264
if (isSWAN) {
return exec()
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# Search for LIFECYCLES definition in the file
rg -n "LIFECYCLES" packages/taro-runtime/src/dsl/common.ts -A 30 -B 5

Repository: NervJS/taro

Length of output: 3434


🏁 Script executed:

# Search for defer: usage across the codebase
rg -n "defer:" --type ts -C 2

Repository: NervJS/taro

Length of output: 2811


🏁 Script executed:

# Get context around lines 262-270 to see the full conditional logic
sed -n '250,280p' packages/taro-runtime/src/dsl/common.ts

Repository: NervJS/taro

Length of output: 948


🏁 Script executed:

# Search for onTabItemTap references
rg -n "onTabItemTap|TabItemTap" --type ts -C 3

Repository: NervJS/taro

Length of output: 23101


为 Swan 平台添加解释性注释,说明为何需要绕过 isDefer 检查。

当前实现确实使百度小程序环境下所有生命周期都立即执行。虽然 LIFECYCLES 数组中仅有 defer:onTabItemTap 一个被标记为延迟执行(packages/shared/src/runtime-hooks.ts 第 95 行),但代码结构上是为所有 Swan 生命周期添加了例外。这与 PR 描述"修复 onTabItemTap 时序问题"相比作用范围过宽。

建议:

  • if (isSWAN) 代码块上方添加注释,解释为何百度小程序平台需要所有生命周期立即执行而不等待 hasLoaded 完成
  • 如果此行为仅应应用于特定生命周期(如 onTabItemTap),考虑将条件改为 if (isSWAN && lifecycle === 'onTabItemTap'),使修改范围与需求相符
🤖 Prompt for AI Agents
In @packages/taro-runtime/src/dsl/common.ts around lines 262 - 264, Add an
explanatory comment above the if (isSWAN) branch in the exec/defer handling to
state why Swan (isSWAN) lifecycles must bypass the isDefer/hasLoaded check and
run exec() immediately (e.g., platform-specific timing/dispatch differences
impacting onTabItemTap). Also consider narrowing the exception: if the immediate
run should only apply to a specific lifecycle, change the condition to if
(isSWAN && lifecycle === 'onTabItemTap') and document that choice; update
references to isDefer, exec, lifecycle and LIFECYCLES in the comment so
reviewers can see the rationale and the intended scope of the change.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 8, 2026

Codecov Report

❌ Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 56.32%. Comparing base (2c7ee19) to head (b171d24).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
packages/taro-runtime/src/dsl/common.ts 0.00% 2 Missing and 1 partial ⚠️

❌ Your patch status has failed because the patch coverage (0.00%) is below the target coverage (75.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #18682      +/-   ##
==========================================
+ Coverage   52.66%   56.32%   +3.65%     
==========================================
  Files         252      447     +195     
  Lines       11679    23345   +11666     
  Branches     2755     5782    +3027     
==========================================
+ Hits         6151    13149    +6998     
- Misses       4562     8366    +3804     
- Partials      966     1830     +864     
Flag Coverage Δ
taro-cli 72.85% <ø> (+0.16%) ⬆️
taro-runtime 60.27% <0.00%> (-0.06%) ⬇️
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/dsl/common.ts 0.94% <0.00%> (-0.02%) ⬇️

... and 200 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.

@Single-Dancer Single-Dancer merged commit 9a985ce into main Jan 8, 2026
38 of 39 checks passed
@Single-Dancer Single-Dancer deleted the fix/swan-onTabItemTap branch January 8, 2026 08:29
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.

2 participants