Skip to content

fix(core): 并发 startApp 时容器渲染出字面量 undefined - #1114

Closed
lnuxe wants to merge 1 commit into
Tencent:masterfrom
lnuxe:fix/concurrent-start-app-undefined
Closed

lnuxe wants to merge 1 commit into
Tencent:masterfrom
lnuxe:fix/concurrent-start-app-undefined

Conversation

@lnuxe

@lnuxe lnuxe commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Closes #1111

Root cause

startApp registers the sandbox in the instance cache synchronously (the WuJie constructor runs before the first await importHTML(...)), so a second startApp for the same name — e.g. React 18/19 <StrictMode> running the effect twice in dev, which is the report — takes the "already initialized, fast render" branch while the first call still has no template:

// packages/wujie-core/src/sandbox.ts
this.template = template ?? this.template;   // both undefined on the first load
await renderTemplateToShadowRoot(this.shadowRoot, iframeWindow, this.template);

renderTemplateToHtml then stringifies the missing template twice (parseFromString(undefined, "text/html") and html.innerHTML = template), which puts the literal text node undefined into the container. Worse, the keep-alive branch of active returns early because this.shadowRoot now exists, so the real template of the first call is never rendered: the container stays at undefined and the child app cannot mount (getElementById(...) is null).

#840 / #976 fixed the same race only in the wujie-react / wujie-vue2 / wujie-vue3 wrappers (each keeps its own window.__WUJIE_QUEUE). Anyone calling wujie-core's startApp directly is still exposed, and core has no guard for an empty template.

Fix

  1. Serialize startApp per app name in core (packages/wujie-core/src/index.ts): concurrent calls for the same name are chained, so the second call always runs after the first one finished rendering. A failed start does not block the next one, and the queue entry is removed as soon as the chain settles (no retention of finished promises).
  2. Guard the render entry points (packages/wujie-core/src/shadow.ts): renderTemplateToShadowRoot and renderTemplateToIframe return early on an empty template instead of writing the literal undefined, as suggested in the issue.

No API change: startApp still returns Promise<Function | void> with the destroy fn.

Reproducible case

Minimal page (no framework needed) that mimics StrictMode double mount — two startApp calls in the same tick, keep-alive, and a sub-app whose entry HTML is delayed 500 ms so the two calls overlap:

window.wujie.startApp({ name: 'app1', url: '/sub/index.html', el, alive: true });
window.wujie.startApp({ name: 'app1', url: '/sub/index.html', el, alive: true });

A requestAnimationFrame sampler records every distinct shadowRoot.textContent of the container.

Verification

# wujie-core unit tests (21 suites / 98 tests)
npx jest -c __test__/unit/jest.config.js --coverage=false
# eslint + declaration typecheck
npx eslint --ext .ts ./src
npx tsc --emitDeclarationOnly -p tsconfig.json

New regression file __test__/unit/concurrent-start-app.test.ts (4 cases) — 3 of them fail on the unpatched source and all 4 pass with the patch:

# before (git stash push -- packages/wujie-core/src)
  ✕ 同名子应用并发 startApp 应串行执行 sandbox.active,不能交错 (40 ms)
  ✓ 前一次启动失败不应阻塞后一次启动 (32 ms)
  ✕ template 为空时不应把字面量 undefined 渲染进 shadowRoot (2 ms)
  ✕ template 为空时不应渲染降级 iframe 文档 (16 ms)
Tests:       3 failed, 1 passed, 4 total

# after
  ✓ 同名子应用并发 startApp 应串行执行 sandbox.active,不能交错 (67 ms)
  ✓ 前一次启动失败不应阻塞后一次启动 (32 ms)
  ✓ template 为空时不应把字面量 undefined 渲染进 shadowRoot (4 ms)
  ✓ template 为空时不应渲染降级 iframe 文档 (19 ms)
Tests:       4 passed, 4 total

Browser reproduction (built lib/index.js, Chrome 1000x780 @2x, rAF sampling of the container's shadow root):

// BEFORE — the container literally renders "undefined", child app never mounts
{
  "sawUndefined": true,
  "ready": false,
  "pageErrors": [
    "Cannot set properties of null (setting 'textContent')",
    "Cannot set properties of null (setting 'textContent')"
  ],
  "samples": [{ "ms": 88, "text": "html {view-transition-name: none;}undefined" }]
}

// AFTER — template renders, one mount, no page error
{
  "sawUndefined": false,
  "ready": true,
  "pageErrors": [],
  "samples": [
    { "ms": 563, "text": "... /* http://127.0.0.1:5199/sub/style.css */#app { color: #0a7d32; ... } initial" },
    { "ms": 614, "text": "... /* http://127.0.0.1:5199/sub/style.css */#app { color: #0a7d32; ... } SUB APP READY" }
  ]
}
beforeafter

同名子应用并发调用 startApp(React StrictMode 双挂载)时,第二次调用会命中「已初始化快速渲染」
分支,该分支没有 template,renderTemplateToHtml 把 undefined 强转成字符串写进容器;同时保活
分支因为 shadowRoot 已存在而不再重渲染,容器停在 undefined,子应用挂载时 getElementById 为 null。

- startApp 按 name 串行:后一次调用等前一次渲染完成,前一次失败不阻塞后一次。
- renderTemplateToShadowRoot / renderTemplateToIframe 对空 template 提前 return 兜底。
- 新增 __test__/unit/concurrent-start-app.test.ts(4 个用例)。
@lnuxe

lnuxe commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Closing this in favour of #1112 by @fadeaway1680, which addresses the same issue #1111 and was opened on 2026-09-17 — before this PR.

I did not check for an existing PR before picking up the issue. #1112 covers the same code path, so keeping both would just split the review attention.

Sorry for the duplicate.

@lnuxe lnuxe closed this Nov 13, 2026
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.

bug: 并发 startApp(如 React StrictMode 双挂载)时子应用容器闪现 "undefined" 文本

1 participant