Skip to content

fix(mini-runner): include sub-package pages in prerender page set - #19503

Merged
Single-Dancer merged 3 commits into
NervJS:mainfrom
anyone-developer:fix/subpackage-prerender
Sep 23, 2026
Merged

Single-Dancer merged 3 commits into
NervJS:mainfrom
anyone-developer:fix/subpackage-prerender

Conversation

@Edward-Roshan

@Edward-Roshan Edward-Roshan commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

[mini-runner] 分包页面开启预渲染后快照内容错误(构建无报错、无告警)

环境

  • Taro 4.2.x / 4.2.2(main 分支 2026-09-20 核实仍未修复)
  • 编译目标:weapp(微信小程序)
  • mini.prerender.match 包含分包路径,如 ['pages/**', 'sub/**']

现象

为分包页开启预渲染后,构建不会报错也不会告警,但每个分包页的预渲染快照(写入该页自身 .wxml 的 <block wx:else> 兜底块)内容是错误的:

  • 实测:13 个分包页的 index.wxml 字节数完全相同(48042 B),且内容都是主包最后一个页面(一个 push-landing 页)。即每个分包页首屏渲染的是「上一个编译页模块」的实例,而非自己的首屏。

根因

框架内两个子系统对「哪些页面可预渲染」口径不一致:

  1. 渲染/校验侧——packages/taro-webpack5-runner/src/prerender/prerender.js 的 Prerender.render():遍历 Object.keys(stat.entrypoints)——全部 webpack 入口,含分包——并调用 validatePrerenderPages(paths, prerenderConfig),后者只按 match / exclude 的 glob 过滤。所以 match: ['sub/**'] 的分包页能过校验、进入渲染队列。

  2. 注入侧——packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts 的 getPages():

    const appPages = this.appConfig.pages          // 仅主包
    this.prerenderPages = new Set(validatePrerenderPages(appPages, prerender).map(p => p.path))
    // ... 之后 ...
    this.getSubPackages(this.appConfig)            // 收集分包,但不更新 this.prerenderPages

    getSubPackages() 只往 this.pages 加、从不更新 this.prerenderPages。

下游页面 loader 判断 prerender: this.prerenderPages.has(module.name)。分包模块的 module.name 形如 sub/ai-chat/index,不在 this.prerenderPages 中 → prerender 为 false → taro-loader/lib/page.js 不注入 wx._prerender,但 writeScript() 仍给每页追加 module.exports = wx._prerender → 分包页 require 到上一个编译页模块的实例 → 所有分包快照塌缩成同一页。

框架静默产出了错误结果,没有在 validatePrerenderPages 处拒绝 / 告警分包路径。

复现步骤

  1. Taro 项目 app.config.ts 配置 subPackages。
  2. config/prod.ts(或 dev / index):mini.prerender = { match: ['sub/**'] }。
  3. taro build --type weapp。
  4. 检查 dist-weapp/sub/<任一>/index.wxml → 其 wx:else 兜底块内容错误;grep -rl 'push-landing' dist-weapp/sub 有命中。

期望:每个分包页携带自己的首屏快照;或者,若框架本就无意支持分包预渲染,应在构建时显式拒绝 / 告警 prerender.match 中的分包路径,而不是静默产出坏快照。

临时修复(已验证可用)

用 patch-package 改 getPages(),把分包页 union 进预渲染集合(见下方建议修法)。

建议修法(已提 PR)

在 getPages() 计算 this.prerenderPages 时,把分包页路径一并纳入:

const subPackages = this.appConfig.subPackages || this.appConfig.subpackages || []
const subPages: string[] = []
for (const sp of subPackages) {
  const root = sp.root || ''
  for (const pg of (sp.pages || [])) {
    subPages.push(`${root}/${pg}`.replace(/\/{2,}/g, '/'))
  }
}
this.prerenderPages = new Set(
  validatePrerenderPages([...appPages, ...subPages], prerender).map(p => p.path)
)

修复分支:anyone-developer/taro 的 fix/subpackage-prerender(待开 PR 到 NervJS/taro)。

Summary by CodeRabbit

  • 新功能
    • 预渲染配置现支持匹配分包页面,符合条件的分包页面会自动加入预渲染列表。
    • 页面预渲染处理现同时覆盖主包和分包页面。

Previously getPages() only fed main-package appPages into this.prerenderPages.
Prerender.render() iterates all webpack entrypoints (including sub-packages)
and validatePrerenderPages accepts any path matching the match glob, so
configuring prerender for sub-package pages passed validation but the page
loader never got prerender: true (this.prerenderPages.has(module.name) was
false for sub-package modules). As a result taro-loader did not inject
wx._prerender, and every sub-package page's prerender snapshot silently
fell back to the previous page's instance.

Fix by unioning sub-package page paths into the prerender page set, so
sub-package pages are eligible for prerender when their path matches the
config (same as main-package pages).
@coderabbitai

coderabbitai Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 3bdb7b0c-6b7d-4133-b3a5-ea21d7f132cc

📥 Commits

Reviewing files that changed from the base of the PR and between 5e983d3 and 4b6fdcd.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: e305beea-120f-4955-bddd-3a5717e89304

📥 Commits

Reviewing files that changed from the base of the PR and between 1b29770 and 73af676.

📒 Files selected for processing (1)
  • packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


Walkthrough

getPages 现在收集主包和分包页面,并将合并后的页面列表传给 validatePrerenderPages。匹配分包页面的预渲染配置现在会被处理。

Changes

预渲染页面覆盖

Layer / File(s) Summary
收集分包页面并校验预渲染配置
packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts
getPages 遍历 subPackages 和 subpackages,拼接并规范化分包页面路径。validatePrerenderPages 现在接收主包与分包页面的合并列表。

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: muyouz

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了主要变更:修复 mini-runner,使预渲染页面集合包含分包页面。标题简洁、具体,并与代码变更和 PR 目标一致。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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.

@codecov

codecov Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 54.63%. Comparing base (6205652) to head (4b6fdcd).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##             main   #19503    +/-   ##
========================================
  Coverage   54.63%   54.63%            
========================================
  Files         464      464            
  Lines       25391    25391            
  Branches     6364     6342    -22     
========================================
  Hits        13872    13872            
+ Misses       9640     9532   -108     
- Partials     1879     1987   +108     
Flag Coverage Δ
taro-cli 56.96% <ø> (ø)
taro-runtime 57.88% <ø> (ø)
taro-web 52.37% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.
see 53 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 added this to the 4.2.2 milestone Sep 22, 2026
@Single-Dancer
Single-Dancer removed the request for review from Muyouz September 22, 2026 02:50
@Single-Dancer

Copy link
Copy Markdown
Collaborator

你好,麻烦开启这个 PR 的 “Allow edits from maintainers”,方便同步 main 分支;或者 rebase 到最新 main 后更新一下 PR,谢谢。

@Edward-Roshan

Copy link
Copy Markdown
Contributor Author

你好,麻烦开启这个 PR 的 “Allow edits from maintainers”,方便同步 main 分支;或者 rebase 到最新 main 后更新一下 PR,谢谢。

点哪个开启?我没找到

@Single-Dancer
Single-Dancer merged commit 57a4427 into NervJS:main Sep 23, 2026
25 checks passed
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