-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relative base
option + dynamic import leads to styles injected two times in production build
#9967
Comments
@bgoscinski great findings! Let us know if you would like to work on a PR to fix this 👍🏼 |
Sure, I can get my hands dirty, but I'd need some guidance about how should I fix it. I can see few ways it can be fixed:
|
I think something along the lines of 3 is the best, as even with two, people may add a link to a CSS by hand that we will miss with 2. For the moment, you could use
But after #9938, this function may already get a full URL. Maybe we could find a way to compare full URL with relative ones. It isn't clear at this point what is best (more after #9938) |
Yeah, I think it's safest to always (with both relative and absolute On it. |
Hi! I was noticing this on my work on PR #9920, and I did fixed it there also, by this code in the preload function: vite/packages/vite/src/node/plugins/importAnalysisBuild.ts Lines 70 to 92 in 8f68cdf
What do you think? |
Describe the bug
When
base
is set to relative (empty string or'./'
) then in prod build when dynamically importing a module that has some CSS dependencies these dependencies will be appended second time to<head>
leading to wrong styles. Invite serve
scenario this seem to work fine.I managed to find possible root cause:
When
base
is absolute then the generatedassetsUrl
returns absolute URLs without origin part which match the ones vite embeds into index.html 👍:However when
base
is relative thenassetsUrl
returns absolute URLs with origin part and vite embeds relative URLs into index.html 🐛:This mismatch in relative
base
case causes __vitePreload to think that given asset was not yet loaded becausedep
is an absolute URL with origin and index.html has relative URL to the file.To reproduce this bug
npm i && npx vite build && npx vite preview
link
to the same CSS file at the bottom of<head>
. This second one "wins" the style cascade now :(base
option fromvite.config.js
npx vite build && npx vite preview
Reproduction
https://github.com/bgoscinski/vite-relative-base-plus-dynamic-import-repro
System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: