Repository navigation
Conversation
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
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 I did not check for an existing PR before picking up the issue. Sorry for the duplicate. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist
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通过。