Skip to content

fix: handle URL-normalized ?raw= and ?url= query params in asset plugin#22061

Open
remorses wants to merge 3 commits intovitejs:mainfrom
remorses:fix/raw-query-normalization
Open

fix: handle URL-normalized ?raw= and ?url= query params in asset plugin#22061
remorses wants to merge 3 commits intovitejs:mainfrom
remorses:fix/raw-query-normalization

Conversation

@remorses
Copy link
Copy Markdown
Contributor

rawRE and urlRE use /(\?|&)raw(?:&|$)/ which doesn't match ?raw=. URLSearchParams normalizes valueless params to ?raw= per the URL spec, so the asset plugin's load filter silently misses these imports and vite:import-analysis fails to parse raw file content as JS.

Expands the regex to /(\?|&)raw(?:=(?:&|$)|&|$)/ — accepts both ?raw and ?raw= while still rejecting partial matches like ?rawdata=1.

Closes #22060

Tests: 14 new unit tests for rawRE, urlRE, removeRawQuery, and removeUrlQuery covering both forms in various query positions. All 547 existing unit tests pass.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

?raw and ?url query params fail when URL-normalized to ?raw= / ?url=

1 participant