-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix: 修复百度小程序 onLoad onTabItemTap执行时序问题 #18682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Walkthrough在 Taro 运行时中为 Swan(百度)小程序环境引入新的环保卫,修改页面生命周期处理器工厂以检测 Swan 环境,当识别出 Swan 时直接执行生命周期处理函数,而不遵循延迟执行逻辑。 Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ 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.
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
📒 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 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.
| if (isSWAN) { | ||
| return exec() | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
# Examine the lifecycle DSL file to understand the structure and the fix
cat -n packages/taro-runtime/src/dsl/common.ts | head -300 | tail -100Repository: NervJS/taro
Length of output: 3523
🏁 Script executed:
# Find the LIFECYCLES array definition and see what's in it
rg -n "LIFECYCLES\s*=" packages/taro-runtime/src/dsl/common.ts -A 20Repository: NervJS/taro
Length of output: 37
🏁 Script executed:
# Search for LIFECYCLES definition across the runtime
rg -n "LIFECYCLES\s*=" packages/taro-runtime/src/ -A 15Repository: NervJS/taro
Length of output: 37
🏁 Script executed:
# Search for LIFECYCLES import or definition
rg -n "LIFECYCLES" packages/taro-runtime/src/dsl/common.ts -B 5 -A 10Repository: NervJS/taro
Length of output: 1704
🏁 Script executed:
# Search for getMiniLifecycleImpl hook implementation
rg -n "getMiniLifecycleImpl" packages/taro-runtime/src/ -A 30 | head -100Repository: NervJS/taro
Length of output: 7116
🏁 Script executed:
# Search for where getMiniLifecycleImpl is registered/defined
rg -n "getMiniLifecycleImpl" packages/ --type ts -B 3 -A 40 | grep -A 40 "register\|page:" | head -150Repository: NervJS/taro
Length of output: 7332
🏁 Script executed:
# Search for platform-specific lifecycle implementations
fd -e ts packages/taro-plugin-platform-swan/src | xargs rg -l "getMiniLifecycleImpl" | head -5Repository: NervJS/taro
Length of output: 341
🏁 Script executed:
# Search in swan plugin directory for lifecycle implementation
rg -n "page.*:" packages/taro-plugin-platform-swan/src --type ts | grep -i lifecycle | head -20Repository: NervJS/taro
Length of output: 133
🏁 Script executed:
# Find swan platform integration files
fd -e ts "swan" packages/ | head -20Repository: NervJS/taro
Length of output: 194
🏁 Script executed:
# Search for hook registration and LIFECYCLES array definition
rg -n "register.*getMiniLifecycleImpl|page:\s*\[" packages/taro-platform-swan/src --type ts -A 30 | head -100Repository: NervJS/taro
Length of output: 37
🏁 Script executed:
# Search for lifecycle implementations in swan platform
find packages/taro-platform-swan/src -type f -name "*.ts" | head -10Repository: NervJS/taro
Length of output: 457
🏁 Script executed:
# Check swan runtime.ts for lifecycle definitions
cat -n packages/taro-platform-swan/src/runtime.ts | head -100Repository: NervJS/taro
Length of output: 480
🏁 Script executed:
# Search for LIFECYCLES constant definition across entire codebase
rg -n "const LIFECYCLES\s*=" packages/ --type ts -A 10 | head -50Repository: NervJS/taro
Length of output: 37
🏁 Script executed:
# Search for where LIFECYCLES comes from in the hooks system
rg -n "LIFECYCLES" packages/taro-runtime/src/ --type ts -B 3 | head -80Repository: NervJS/taro
Length of output: 933
🏁 Script executed:
# Search for getMiniLifecycleImpl hook registration in swan
rg -n "getMiniLifecycleImpl" packages/taro-platform-swan/ --type ts -B 5 -A 50Repository: NervJS/taro
Length of output: 37
🏁 Script executed:
# Search more broadly for getMiniLifecycleImpl hook definition
rg -n "getMiniLifecycleImpl" packages/ --type ts -B 5 -A 40 | grep -A 40 "register\|export" | head -150Repository: NervJS/taro
Length of output: 9143
修复应仅限于 onTabItemTap,当前实现对所有生命周期的影响过于宽泛。
当前代码在 LIFECYCLES.forEach 循环中无条件地对所有 Swan 环境下的生命周期应用立即执行逻辑,绕过了 isDefer 机制。虽然这可能解决了 onTabItemTap 的时序问题,但影响范围包括 onLoad、onReady、onShow、onHide 等所有生命周期,可能导致依赖 defer 行为的其他生命周期失效。
建议改为:
config[lifecycle] = function () {
const exec = () => safeExecute(this.$taroPath, lifecycle, ...arguments)
- if (isSWAN) {
+ if (isSWAN && lifecycle === 'onTabItemTap') {
return exec()
}
if (isDefer) {
hasLoaded.then(exec)
} else {
return exec()
}
}此修改将特殊处理限制在 onTabItemTap 生命周期上,避免对其他生命周期造成意外影响。
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (isSWAN) { | |
| return exec() | |
| } | |
| if (isSWAN && lifecycle === 'onTabItemTap') { | |
| return exec() | |
| } |
🤖 Prompt for AI Agents
packages/taro-runtime/src/dsl/common.ts around lines 262-264: the current check
unconditionally calls exec() for all lifecycles when running in Swan, which
bypasses the isDefer mechanism and affects lifecycles other than onTabItemTap;
change the condition so that exec() is invoked immediately only when isSWAN &&
lifecycle === 'onTabItemTap', leaving the existing defer handling intact for all
other lifecycle names.
这个 PR 做了什么? (简要描述所做更改)
修复百度小程序 onLoad onTabItemTap执行时序问题
这个 PR 是什么类型? (至少选择一个)
这个 PR 涉及以下平台:
Summary by CodeRabbit
Bug Fixes
✏️ Tip: You can customize this high-level summary in your review settings.