Conversation
Skip inserting a second style when the same href was already inlined from the entry HTML or a prior dynamic placeholder. Still dispatch load on the original link so Vite preload can settle. close Tencent#1108
Dispatching load synchronously inside the hijacked appendChild loses the event
when the consumer registers its listener only after appendChild returns. Vite's
preload helper appends the link first and calls addEventListener("load") second,
so its promise never settled and the lazily imported chunk never executed.
Dispatch in a macrotask instead. Covered by two unit cases: a listener attached
after the dispatch still receives load, and nothing is dispatched in the same tick.
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通过说明:已添加
packages/wujie-core/__test__/unit/css-href-dedupe.test.ts(5 项,其中「命中去重后的 load 派发时机」2 项为本次补充);本地jest -c __test__/unit/jest.config.js21 suites / 99 tests 通过。未跑 puppeteer 集成测试。未改文档站。详细描述
<style>时写入data-wujie-css-href。loadStyleSheet插入前按该属性和注释/* href */查找;命中则不再插入第二份 style,改为异步(setTimeout(..., 0))对原link派发load。消费方是在appendChild返回之后才addEventListener("load"),在调用栈内同步派发会丢事件,导致 Vite preload 的 promise 不落定、动态 import 的 chunk 不执行。data-wujie-css-href。<link>中,又被__vite__mapDepspreload 时,避免两份 style 导致后插入的默认宽高盖住原子类。最小复现:https://github.com/KeyToLove/wujie-vite8-css-dup-demo