Repository navigation
Conversation
同名子应用并发调用 startApp(React StrictMode 双挂载)时,第二次调用会命中「已初始化快速渲染」 分支,该分支没有 template,renderTemplateToHtml 把 undefined 强转成字符串写进容器;同时保活 分支因为 shadowRoot 已存在而不再重渲染,容器停在 undefined,子应用挂载时 getElementById 为 null。 - startApp 按 name 串行:后一次调用等前一次渲染完成,前一次失败不阻塞后一次。 - renderTemplateToShadowRoot / renderTemplateToIframe 对空 template 提前 return 兜底。 - 新增 __test__/unit/concurrent-start-app.test.ts(4 个用例)。
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. |
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.
Closes #1111
Root cause
startAppregisters the sandbox in the instance cache synchronously (theWuJieconstructor runs before the firstawait importHTML(...)), so a secondstartAppfor 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:renderTemplateToHtmlthen stringifies the missing template twice (parseFromString(undefined, "text/html")andhtml.innerHTML = template), which puts the literal text node undefined into the container. Worse, the keep-alive branch ofactivereturns early becausethis.shadowRootnow exists, so the real template of the first call is never rendered: the container stays atundefinedand the child app cannot mount (getElementById(...)isnull).#840 / #976 fixed the same race only in the
wujie-react/wujie-vue2/wujie-vue3wrappers (each keeps its ownwindow.__WUJIE_QUEUE). Anyone callingwujie-core'sstartAppdirectly is still exposed, and core has no guard for an empty template.Fix
startAppper app name in core (packages/wujie-core/src/index.ts): concurrent calls for the samenameare 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).packages/wujie-core/src/shadow.ts):renderTemplateToShadowRootandrenderTemplateToIframereturn early on an empty template instead of writing the literalundefined, as suggested in the issue.No API change:
startAppstill returnsPromise<Function | void>with the destroy fn.Reproducible case
Minimal page (no framework needed) that mimics StrictMode double mount — two
startAppcalls in the same tick, keep-alive, and a sub-app whose entry HTML is delayed 500 ms so the two calls overlap:A
requestAnimationFramesampler records every distinctshadowRoot.textContentof the container.Verification
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:Browser reproduction (built
lib/index.js, Chrome 1000x780 @2x,rAFsampling of the container's shadow root):