fix: handle URL-normalized ?raw= and ?url= query params in asset plugin#22061
Open
remorses wants to merge 3 commits intovitejs:mainfrom
Open
fix: handle URL-normalized ?raw= and ?url= query params in asset plugin#22061remorses wants to merge 3 commits intovitejs:mainfrom
?raw= and ?url= query params in asset plugin#22061remorses wants to merge 3 commits intovitejs:mainfrom
Conversation
URLSearchParams normalizes valueless query params like ?raw to ?raw= (with trailing equals sign). This adds tests to verify that rawRE and urlRE handle both ?raw and ?raw= forms, including compound queries and the removeRawQuery/removeUrlQuery helper functions. Several of these tests currently fail because the existing regex /(\?|&)raw(?:&|$)/ does not match ?raw= — the = character is neither & nor end-of-string.
When a module ID passes through URLSearchParams (e.g. via new URL() followed by searchParams.delete()), valueless query params like ?raw get normalized to ?raw= per the URL spec. The existing rawRE and urlRE regexes /(\?|&)raw(?:&|$)/ only match ?raw followed by & or end-of-string, so ?raw= falls through the asset plugin's load filter. This causes the raw file content to reach vite:import-analysis, which fails to parse it as JavaScript. Expand the regex alternation to also accept = followed by & or end-of-string: /(\?|&)raw(?:=(?:&|$)|&|$)/. This handles both the original ?raw form and the URL-normalized ?raw= form, while still correctly rejecting partial matches like ?rawdata=1.
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.
rawREandurlREuse/(\?|&)raw(?:&|$)/which doesn't match?raw=.URLSearchParamsnormalizes valueless params to?raw=per the URL spec, so the asset plugin's load filter silently misses these imports andvite:import-analysisfails to parse raw file content as JS.Expands the regex to
/(\?|&)raw(?:=(?:&|$)|&|$)/— accepts both?rawand?raw=while still rejecting partial matches like?rawdata=1.Closes #22060
Tests: 14 new unit tests for
rawRE,urlRE,removeRawQuery, andremoveUrlQuerycovering both forms in various query positions. All 547 existing unit tests pass.