Skip to content

Conversation

@zhengyanan1
Copy link
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

coderabbitai bot commented Dec 17, 2025

Walkthrough

在 Taro 运行时中为 Swan(百度)小程序环境引入新的环保卫,修改页面生命周期处理器工厂以检测 Swan 环境,当识别出 Swan 时直接执行生命周期处理函数,而不遵循延迟执行逻辑。

Changes

融合 / 文件 变更摘要
Swan 环境支持
packages/taro-runtime/src/dsl/common.ts
新增 isSWAN 环境守卫,在页面生命周期处理器工厂中添加条件判断:当 isSWAN 为真时,生命周期处理函数立即执行,绕过延迟执行机制

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • 关注新增的 isSWAN 环境判断逻辑是否正确识别 Swan 运行时环境
  • 验证条件分支中立即执行路径(exec())是否与现有的延迟执行逻辑保持一致
  • 确认该改动对其他小程序环境(微信、支付宝等)的生命周期执行无影响

Suggested reviewers

  • Single-Dancer
  • tutuxxx

Poem

🐰 小兔快乐跳啊跳,
Swan 环境有新招,
生命周期不再等待,
立即执行速度快,
Taro 运行更精妙!✨

Pre-merge checks and finishing touches

❌ 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%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题清晰准确地概括了主要变更:修复百度小程序中onLoad和onTabItemTap的执行时序问题,与代码摘要中引入Swan环境守卫和修改生命周期执行流程的变更相符。
✨ 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

@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

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.

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

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:

# Examine the lifecycle DSL file to understand the structure and the fix
cat -n packages/taro-runtime/src/dsl/common.ts | head -300 | tail -100

Repository: 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 20

Repository: NervJS/taro

Length of output: 37


🏁 Script executed:

# Search for LIFECYCLES definition across the runtime
rg -n "LIFECYCLES\s*=" packages/taro-runtime/src/ -A 15

Repository: 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 10

Repository: NervJS/taro

Length of output: 1704


🏁 Script executed:

# Search for getMiniLifecycleImpl hook implementation
rg -n "getMiniLifecycleImpl" packages/taro-runtime/src/ -A 30 | head -100

Repository: 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 -150

Repository: 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 -5

Repository: 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 -20

Repository: NervJS/taro

Length of output: 133


🏁 Script executed:

# Find swan platform integration files
fd -e ts "swan" packages/ | head -20

Repository: 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 -100

Repository: 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 -10

Repository: 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 -100

Repository: 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 -50

Repository: 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 -80

Repository: 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 50

Repository: 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 -150

Repository: NervJS/taro

Length of output: 9143


修复应仅限于 onTabItemTap,当前实现对所有生命周期的影响过于宽泛。

当前代码在 LIFECYCLES.forEach 循环中无条件地对所有 Swan 环境下的生命周期应用立即执行逻辑,绕过了 isDefer 机制。虽然这可能解决了 onTabItemTap 的时序问题,但影响范围包括 onLoadonReadyonShowonHide 等所有生命周期,可能导致依赖 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.

Suggested change
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.

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.

3 participants