fix: __VITE_PUBLIC_ASSET__hash__ in HTML#9247
Conversation
publicDir
42a2fbb to
2c3d85a
Compare
| // TODO: To investigate why `await getBg('.inline-style-public') === "url("http://localhost:5173/icon.png")"` | ||
| // It supposes to be `url("http://localhost:5173/foo/icon.png")` | ||
| // (I built the playground to verify) | ||
| expect(await getBg('.inline-style-public')).toContain(iconMatch) |
There was a problem hiding this comment.
This test is failing because during dev await getBg('.inline-style-public') is "url("http://localhost:5173/icon.png")".
Vite's test runs with dev and then runs with build+preview.
I think there is a different bug here. await getBg('.inline-style-public') should return url("http://localhost:5173/foo/icon.png") and not url("http://localhost:5173/icon.png")" during dev too. (because src="/raw.js"(at assets/index.html line 18) is rewrote to src="/foo/raw.js" during dev.
There was a problem hiding this comment.
Yeah. I guess there is a bug in Vitest configuration here but I'm not familiar so I haven't catched it yet. I will investigate more. But if you spot the bug, please let me know, or just push the change directly to my branch. Much appreciated.
There was a problem hiding this comment.
The weird thing is that if it's class style, it works fine. The test fail with inline style only. So I am nor sure it's a bug in Vitest config or the code implementation 😂
There was a problem hiding this comment.
I guess there is a bug in Vitest configuration here but I'm not familiar so I haven't catched it yet.
No, I meant there is a bug in Vite.
The weird thing is that if it's class style, it works fine.
Good finding 👍 I think this is a different bug.
I haven't checked closely yet but I suppose style attributes needs to be rewrote here during dev.
vite/packages/vite/src/node/server/middlewares/indexHtml.ts
Lines 185 to 253 in 2c3d85a
If you prefer, I think we could go with commenting out this line for now since it's a different bug. (I'll create a issue or PR later)
There was a problem hiding this comment.
Thank for your help. I will separate it to a different test case then skip it for now. I will push it soon.
There was a problem hiding this comment.
I just re-ran the code again in DEV mode and BUILD mode. I confirm that the url() in inline style is missing base url.
This is what I get when I visit http://127.0.0.1:5173/icon.png directly.

But I don't understand why it works fine as a background image in a CSS rule

So I guess we just discovered another bug.
I also pushed the commit to skip the failed test for DEV mode.
…tyle in DEV mode
publicDir



Description
index.html, if we refer to a file inpublicDirinsideurl()(e.g:style="background: url(/icon.png)"), it becomes__VITE_PUBLIC_ASSET__hash__after building (e.g:style="background: url(__VITE_PUBLIC_ASSET__018698dc__)").public/fonts/font.woffinindex.htmlcontains__VITE_PUBLIC_ASSET__#9174playground/assetsto reproduce this bug__VITE_PUBLIC_ASSET__hash__with the actual file inpublicDir. (packages/vite/src/node/plugins/html.ts)playground/assets/__tests__/assets.spec.tsto cover changes. (not passed all yet)Related PR
Additional context
publicDirdoes not load Upgrade to vite 3 bestofjs/bestofjs#148 (comment)You can see the preview at https://bestofjs-68pbasy3a-bestofjs.vercel.app/
There is no file such as
https://bestofjs-68pbasy3a-bestofjs.vercel.app/__VITE_PUBLIC_ASSET__03d6a1cf__Questions
https://github.com/vitejs/vite/pull/9247/files#diff-be0c593011deabf61343720611c7586df51ddc269f187678cdbf85ff72a7973bR370-R373
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).