fix: 修复百度小程序 onLoad onTabItemTap执行时序问题#18682
Conversation
Walkthrough在 Taro 运行时的页面生命周期处理器中新增对 Swan(百度)小程序环境的检测:当识别为 Swan 时,生命周期处理函数通过专用执行路径立即执行,绕过原有的延迟/分发执行逻辑。 Changes
Sequence Diagram(s)(此次改动为单一文件的环境判断与执行分支更改,不生成序列图) Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 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'// 百度小程序 |
There was a problem hiding this comment.
🧩 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 -60Repository: 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.tsRepository: 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 -10Repository: 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 -5Repository: 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 -10Repository: 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 -10Repository: 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 swanRepository: 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 -80Repository: 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 -20Repository: 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 -100Repository: 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 -30Repository: 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 -100Repository: 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 -5Repository: 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 15Repository: 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 -100Repository: 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.
f287941 to
b171d24
Compare
There was a problem hiding this comment.
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
📒 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
| if (isSWAN) { | ||
| return exec() | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
# Search for LIFECYCLES definition in the file
rg -n "LIFECYCLES" packages/taro-runtime/src/dsl/common.ts -A 30 -B 5Repository: NervJS/taro
Length of output: 3434
🏁 Script executed:
# Search for defer: usage across the codebase
rg -n "defer:" --type ts -C 2Repository: 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.tsRepository: NervJS/taro
Length of output: 948
🏁 Script executed:
# Search for onTabItemTap references
rg -n "onTabItemTap|TabItemTap" --type ts -C 3Repository: 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 Report❌ Patch coverage is
❌ 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@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
这个 PR 做了什么? (简要描述所做更改)
修复百度小程序 onLoad onTabItemTap执行时序问题
这个 PR 是什么类型? (至少选择一个)
这个 PR 涉及以下平台:
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.