-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
fix: encode URLs correctly (fix #15298) #15311
Conversation
Run & review this pull request in StackBlitz Codeflow. |
d4dbd3c
to
c667958
Compare
Thanks for the PR! I mentioned we had this problem here #15246 (review). I think it is a good idea to do this, but I don't know if we should merge this one in a patch. We always had this issue and it would be good to see how all the encoding affects performance. I added it to the 5.1 milestone for now. |
Yes, I forgot that 🙈Am 2023. 12. 12. um 18:49 schrieb Ben McCann ***@***.***>:
@benmccann commented on this pull request.
In packages/vite/src/node/plugins/importAnalysis.ts:
@@ -401,7 +401,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
url = joinUrlSegments(base, url)
}
- return [url, resolved.id]
+ // only encode if the url can't be parsed
is this comment left over from your earlier changes?
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
|
3918548
to
f45775c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how much we should worry about performance when deciding to merge this or not. Correctness seems more important. Of course if we can optimize the implementation we should certainly do so
I agree that correctness is more important than performance here. I still think we should merge this one as part of the next minor. We discussed with the team and decided to start the beta for 5.1 after the holidays. This change could introduce subtle bugs, for example, I think this changed line isn't right https://github.com/vitejs/vite/pull/15311/files#diff-f2f744fef86a2c562dd5142240912f7a2d28404fac536740a2424daf628aa609R409. That normalized URL is later used in both warmup and to update the module graph. In both cases, the URL should be decoded. So we need to move that later on, and only encode when we write a URL into the code. |
You mean the line in |
I think the encoding may need to be moved here instead https://github.com/pzerelles/vite/blob/f45775c9515cf9a3d3c9ffc448b871dfc78efe84/packages/vite/src/node/plugins/importAnalysis.ts#L602C17-L602C17 |
0593e21
to
6174878
Compare
Thanks so much for providing the right location. I probably didn't understand the code good enough. The original problem is also fixed when moving the encode to that location. |
To set expectations, we may start work in 5.1 beta in January. My take is that we should merge it then. I'll be off for a few weeks, then do a review. Other maintainers may get to this one sooner though and I'm fine with them deciding to move forward if performance and test cases look enough to cover this one. We should have a test covering an id that wouldn't work with the encoding as it was before the last commit. I think a file called |
Yes. There is also another thing. Right now I encode only placeholders |
Instead of adding a new test I extended the existing unicode url test to include a % character. |
If performance is a concern, I did some tests and I found that browsers can generally handle un-encoded imports too. It'll encode them automatically when requesting. Even if the import string has a mix of encoded and unencoded strings, e.g. |
@pzerelles we discussed about this in today's team meeting and decided to move forward with the change, but using a helper for performance reasons that will only transform |
But this will not fix urls in general and libraries like |
@pzerelles would you create a minimal repro for the issue you describe with image-tools? |
Of course: https://github.com/pzerelles/vite-bug-repro The second test logo will not be loaded, because vite-imagetools (currently) writes the transformed image to disk with url-encoded filename (which is also wrong), but the web server will look for a file with the url decoded. The main reason for vite-imagetools doing this is because vite does not url-encode asset urls when they are written to html or js output (I assume). I already have a PR in the vite-imagetools repository to remove this behavior and output files unencoded, and maintainers have signaled to me that it will be accepted if vite can provide the url-encoding, which in their opinion is also the correct place to do it. |
Based on the repro, is your main concern of this inconsistency on this HTML during <img src="/assets/logo 10-rYk2SNri.png" class="logo" alt="Test logo">
<img src="/assets/logo%2010-4FQfBhVH.webp" class="logo" alt="Test logo"> The strings come from this source code: import testLogo from "./logo 10.png";
import testLogoScaled from "./logo 10.png?w=64&format=webp";
document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
<img src="${testLogo}" class="logo" alt="Test logo" />
<img src="${testLogoScaled}" class="logo" alt="Test logo" />
` So from what I can tell, we only need to fix the URL string from asset imports. We don't have to handle the encoding of import specifiers to fix But for others, it looks like |
It is not only for That is correct, |
I think this PR is doing more than expected to fix the issue for
Which I don't think are needed for I guess what I'm proposing is that, we can scope this PR down by
|
But isn't |
The consumer who uses |
To be honest, I wasn't thinking about encoding when designing the What if we keep |
But isn't that the distinction, filename or url. I agree that the filename should not be url-encoded, but the url should be if it is represented as string. From a developer standpoint, I import an image and get back a string to use as And |
We have two definitions for URL internally:
I think we need to properly document this in the docs, but it is tricky. We should first find good names for these.
I'm proposing moving the encoding from |
Hmm, maybe I'm also looking at the issue wrongly. @patak-dev I think I agree that perhaps we should leave If we do these encoding/decoding at a low-level (e.g. the
ExamplesExamples of no2: vite/packages/vite/src/node/plugins/css.ts Lines 327 to 328 in 6c4bf26
vite/packages/vite/src/node/server/middlewares/static.ts Lines 121 to 122 in 6c4bf26
Examples of no3: vite/packages/vite/src/client/overlay.ts Lines 238 to 243 in 6c4bf26
vite/packages/vite/src/node/server/middlewares/static.ts Lines 196 to 198 in 6c4bf26
If the internals keep flipping between encoded and decoded, I think it'll be hard to maintain over-time. So I guess my new suggestion now is to update this part (which is the end of the logic) and encode it 🤔 vite/packages/vite/src/node/plugins/asset.ts Line 209 in 6c4bf26
@patak-dev I think for this case, we might need to do a full encode since we don't know how it's going to be used. We could only do the partial encode for import specifiers I believe since we have control over that. |
Moving this to 5.2 since we're releasing 5.1 soon and the PR needs an update to finish up. |
be4739a
to
c07d5f9
Compare
@bluwy I changed the PR to this and it works for dev server. But in build mode, the URLs are unencoded and the original Repro from #15298 fails with preview. I updated the test html and removed and invalid unencoded image URL from there and added the problematic % to an encoded image URL in index.html. The tests are fine still, but during the tests I see messages of |
I added the URL encode again in |
I went ahead and rebased this, and fixed the URI malformed issue. The issue long exists in Vite, only when you reference an asset with I also removed the encoding in
Hoping this change doesn't blow everything up and should be more accurate. |
@bluwy Thank you very much. I wouldn't have found all those places probably. Will try if it solves all issues I had. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
/ecosystem-ci run |
📝 Ran ecosystem CI on
✅ astro, histoire, ladle, laravel, marko, nuxt, previewjs, qwik, rakkas, sveltekit, unocss, vike, vite-plugin-pwa, vite-plugin-react, vite-plugin-react-pages, vite-plugin-react-swc, vite-plugin-svelte, vite-plugin-vue, vite-setup-catalogue, vitepress, vitest |
Description
Fixes #15298. The problem was that special characters were not encoded correctly in URLs created for assets. This was also a problem for vite-imagetools when there were spaces or other special characters in the filename.
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).