Skip to content

fix(core): guard empty template in shadow rendering (issue #1111) - #1116

Closed
lnuxe wants to merge 1 commit into
Tencent:masterfrom
lnuxe:fix/empty-template-shadow-render
Closed

lnuxe wants to merge 1 commit into
Tencent:masterfrom
lnuxe:fix/empty-template-shadow-render

Conversation

@lnuxe

@lnuxe lnuxe commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor
Checklist
  • 提交符合commit规范
  • 文档更改
  • 测试用例添加
  • npm run test通过
详细描述

背景

同一子应用并发触发两次 startApp(典型:React 18/19 StrictMode 在 dev 下 effect 挂载→卸载→再挂载,两次间隔极短),第二次调用在首次 importHTML 尚未完成时命中「已初始化,快速渲染」分支(sandbox 已存在且 alive)。该分支调用 sandbox.active(...) 时未传 template,active 内 this.template = template ?? this.template 回退为 undefined,随后 renderTemplateToShadowRoot / renderTemplateToIframe 对空模板执行:

  • renderTemplateToHtml 中 parser.parseFromString(undefined, "text/html") 把参数强转成字符串 "undefined",解析出的 body 含一个文本节点 undefined;
  • html.innerHTML = template 同样写入字面量 undefined。

结果:子应用容器闪现 "undefined" 文本,直到真实模板到达后才消失。

修复

在 renderTemplateToShadowRoot 和 renderTemplateToIframe 两个渲染入口对空模板(undefined / 空字符串)提前 return,等真实模板到达后再渲染。

验证

  • 新增单元测试 empty-template-guard.test.ts:断言空模板时两个入口直接返回,不向 shadowRoot / 渲染文档注入任何 "undefined" 文本节点。
  • jest 全套 96 用例通过(含新增 2 条);eslint / commitlint 通过。

Concurrent startApp (e.g. React StrictMode double-mount) can hit the
"already initialized" fast path before the first importHTML resolves:
sandbox.active is called without a template, this.template falls back to
undefined, and renderTemplateToShadowRoot / renderTemplateToIframe feed
undefined into parseFromString which stringifies it to "undefined" --
rendering a literal undefined text node into the sub-app container.

Add an early return for empty templates in both render entrypoints so the
real template renders instead of a flashing "undefined".

fix Tencent#1111
@lnuxe

lnuxe commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

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

Both add the same if (!template) return; guard to renderTemplateToShadowRoot and renderTemplateToIframe. #1112 covers the same two entry points, so there is nothing here worth keeping separate.

I did not check for an existing PR before picking up the issue. 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