Skip to content

Commit ac02012

Browse files
committed
fix: strip query strings before matching exclude patterns
Vite appends cache-busting query strings (e.g. ?t=1707350000) to module URLs during HMR. The exclude patterns use $ anchors that expect the URL to end with the file extension (e.g. /.*\.tsx?$/), which fails to match URLs with query parameters. This causes HMR module requests to leak through to the app server, which returns 404 with text/plain MIME type. The browser rejects the module and the page goes blank. Fix by also testing exclude patterns against the URL path (without query string), so /frontend/App.tsx?t=123 correctly matches /.*\.tsx?$/ via the path portion /frontend/App.tsx.
1 parent 05b898a commit ac02012

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/dev-server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,16 @@ function createMiddleware(server: ViteDevServer, options: DevServerOptions) {
6363
}
6464

6565
const exclude = options.exclude ?? defaultOptions.exclude ?? [];
66+
const urlPath = req.url?.split("?")[0];
6667

6768
for (const pattern of exclude) {
6869
if (req.url) {
6970
if (pattern instanceof RegExp) {
70-
if (pattern.test(req.url)) {
71+
if (pattern.test(req.url) || (urlPath && pattern.test(urlPath))) {
7172
return next();
7273
}
7374
} else if (typeof pattern === "string") {
74-
if (req.url.startsWith(pattern)) {
75+
if (req.url.startsWith(pattern) || (urlPath && urlPath.startsWith(pattern))) {
7576
return next();
7677
}
7778
}

0 commit comments

Comments
 (0)