Skip to content

Commit 08f2ec6

Browse files
committed
fix(vite): ignore query-string extensions when detecting asset requests
The ext extraction matched against the full URL, so `/api/foo?file=bar.png` picked up `.png` and — paired with `sec-fetch-dest: image` — got diverted to Vite as an asset load. Strip query/hash before matching so only the path's trailing extension counts.
1 parent 7d49dca commit 08f2ec6

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎src/build/vite/dev.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export async function configureViteDevServer(ctx: NitroPluginContext, server: Vi
244244
server.middlewares.use(function nitroDevMiddlewarePre(req, res, next) {
245245
const fetchDest = req.headers["sec-fetch-dest"];
246246
const accept = req.headers["accept"];
247-
const ext = req.url!.match(/\.([a-z0-9]+)(?:[?#]|$)/i)?.[1];
247+
const ext = req.url!.split(/[?#]/, 1)[0].match(/\.([a-z0-9]+)$/i)?.[1];
248248
const isNitroRoute = !!nitro.routing.routes.match(
249249
req.method || "",
250250
new URL(withBase(req.url!, nitro.options.baseURL), "http://localhost").pathname

‎test/vite/baseurl-dotted-param.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ describe("vite:baseURL dotted params", { sequential: true }, () => {
6767
expect(await response.text()).not.toBe("entry-client.ts");
6868
});
6969

70+
// The extension extraction must look at the path only — a `.png` in the query string (e.g. `?file=bar.png`) must not flag the request as an asset and divert it to Vite.
71+
test("ignores asset-like extensions inside the query string when routing to Nitro", async () => {
72+
const response = await fetch(`${serverURL}/subdir/api/proxy/data?file=bar.png`, {
73+
headers: { "sec-fetch-dest": "image", accept: "image/*" },
74+
redirect: "manual",
75+
});
76+
expect(response.status).toBe(200);
77+
expect(await response.text()).toBe("data");
78+
});
79+
7080
test("navigation without sec-fetch-dest still routes to Nitro (Accept: text/html)", async () => {
7181
const response = await fetch(`${serverURL}/subdir/api/proxy/page.html`, {
7282
headers: { accept: "text/html,application/xhtml+xml" },

0 commit comments

Comments
 (0)