mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-07 12:57:47 +00:00
[cli] Fix dev query string merging (#8183)
Since `vercel dev` performs query string merging with `vercel.json` routes and the requested url, there is sometimes a slight difference between the request that the browser sends and the request that the upstream dev server receives.
Most servers treat the query string key with "empty" value the same as undefined value. Meaning, these two URLs should be equivalent because each has empty values:
- `http://example.com/src/App.vue?vue&type=style&index=0&lang.css`
- `http://example.com/src/App.vue?vue=&type=style&index=0&lang.css=`
However, `vite dev` handles these two URLs differently because of [string comparisons](2289d04af5/packages/plugin-vue/src/handleHotUpdate.ts (L98-L99)) instead of URL parsing, which causes requests from `vercel dev` to fail. Thus, this PR changes from Node.js native URL parsing to a custom implementation in order to handle this corner case and preserve the difference between `?a=` and `?a`.
Ideally this would be [fixed in vite](https://github.com/vitejs/vite/pull/9589), however we might run into other dev servers that also fail in the same way in the future.
- Fixes https://github.com/vercel/vercel/issues/7283
- Closes https://github.com/vitejs/vite/pull/9589
- Related to https://github.com/nodejs/node/issues/9500
This commit is contained in:
46
packages/cli/test/unit/util/dev/parse-query-string.test.ts
Normal file
46
packages/cli/test/unit/util/dev/parse-query-string.test.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import {
|
||||
parseQueryString,
|
||||
formatQueryString,
|
||||
} from '../../../../src/util/dev/parse-query-string';
|
||||
|
||||
describe('parseQueryString', () => {
|
||||
it('should parse to Map and format back to original String', async () => {
|
||||
const querystring =
|
||||
'?a&a=&a&b=1&c=2&c=3&d=&d&d=&space%20bar=4&html=%3Ch1%3E';
|
||||
const parsed = parseQueryString(querystring);
|
||||
expect(parsed).toEqual({
|
||||
a: [undefined, '', undefined],
|
||||
b: ['1'],
|
||||
c: ['2', '3'],
|
||||
d: ['', undefined, ''],
|
||||
'space bar': ['4'],
|
||||
html: ['<h1>'],
|
||||
});
|
||||
const format = formatQueryString(parsed);
|
||||
expect(format).toEqual(querystring);
|
||||
});
|
||||
it('should work with empty string', async () => {
|
||||
const parsed = parseQueryString('');
|
||||
expect(parsed).toEqual({});
|
||||
const format = formatQueryString(parsed);
|
||||
expect(format).toEqual(undefined);
|
||||
});
|
||||
it('should work with question mark', async () => {
|
||||
const parsed = parseQueryString('?');
|
||||
expect(parsed).toEqual({});
|
||||
const format = formatQueryString(parsed);
|
||||
expect(format).toEqual(undefined);
|
||||
});
|
||||
it('should work without question mark', async () => {
|
||||
const parsed = parseQueryString('blarg');
|
||||
expect(parsed).toEqual({});
|
||||
const format = formatQueryString(parsed);
|
||||
expect(format).toEqual(undefined);
|
||||
});
|
||||
it('should work with undefined', async () => {
|
||||
const parsed = parseQueryString(undefined);
|
||||
expect(parsed).toEqual({});
|
||||
const format = formatQueryString(parsed);
|
||||
expect(format).toEqual(undefined);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user