Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/build/vite/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { watch as chokidarWatch } from "chokidar";
import { watch as fsWatch } from "node:fs";
import { join } from "pathe";
import { debounce } from "perfect-debounce";
import { withBase } from "ufo";
import { scanHandlers } from "../../scan.ts";
import { getEnvRunner } from "./env.ts";

Expand Down Expand Up @@ -198,6 +199,12 @@ export async function configureViteDevServer(ctx: NitroPluginContext, server: Vi
return next();
}
nodeReq._nitroHandled = true;

const baseURL = nitro.options.baseURL || "/";
const originalURL = nodeReq.url;
if (baseURL !== "/") {
nodeReq.url = withBase(nodeReq.url, baseURL);
}
try {
// Create web API compat request
const req = new NodeRequest({ req: nodeReq, res: nodeRes });
Expand All @@ -219,19 +226,30 @@ export async function configureViteDevServer(ctx: NitroPluginContext, server: Vi
return await sendNodeResponse(nodeRes, envRes);
} catch (error) {
return next(error);
} finally {
if (baseURL !== "/") {
nodeReq.url = originalURL;
}
}
};

// Handle server routes first to avoid conflicts with static assets served by Vite from the root
// https://github.com/vitejs/vite/pull/20866
server.middlewares.use(function nitroDevMiddlewarePre(req, res, next) {
const fetchDest = req.headers["sec-fetch-dest"];
const ext = req.url!.match(/\.([a-z0-9]+)(?:[?#]|$)/i)?.[1];
const isNitroRoute = ext
? !!nitro.routing.routes.match(
req.method || "",
new URL(withBase(req.url!, nitro.options.baseURL), "http://localhost").pathname
)
: false;
res.setHeader("vary", "sec-fetch-dest");
if (
// Originating from browser tab or no fetch dest (curl, fetch, etc) and (not script, style, image, etc)
(!fetchDest || /^(document|iframe|frame|empty)$/.test(fetchDest)) &&
// No file extension (not /src/index.ts)
!req.url!.match(/\.([a-z0-9]+)(?:[?#]|$)/i)?.[1] &&
// No file extension (not /src/index.ts) unless it is an explicit Nitro route
(!ext || isNitroRoute) &&
// Special prefixes (/__vue-router/auto-routes, /@vite-plugin-layouts/, etc)
!/^\/(?:__|@)/.test(req.url!)
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (event: any) => event.context.params!.param;
11 changes: 11 additions & 0 deletions test/vite/baseurl-dotted-param-fixture/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Nitro Base URL Dotted Param Fixture</title>
</head>
<body>
<div id="app">fixture</div>
</body>
</html>
3 changes: 3 additions & 0 deletions test/vite/baseurl-dotted-param-fixture/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "nitro/tsconfig"
}
13 changes: 13 additions & 0 deletions test/vite/baseurl-dotted-param-fixture/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from "vite";
import { nitro } from "nitro/vite";

export default defineConfig({
base: "/subdir/",
plugins: [
nitro({
baseURL: "/subdir/",
serverDir: "./",
serveStatic: false,
}),
],
});
49 changes: 49 additions & 0 deletions test/vite/baseurl-dotted-param.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { fileURLToPath } from "node:url";
import type { ViteDevServer } from "vite";
import { beforeAll, afterAll, describe, expect, test } from "vitest";

const { createServer } = (await import(
process.env.NITRO_VITE_PKG || "vite"
)) as typeof import("vite");

describe("vite:baseURL dotted params", { sequential: true }, () => {
let server: ViteDevServer;
let serverURL: string;

const rootDir = fileURLToPath(new URL("./baseurl-dotted-param-fixture", import.meta.url));

beforeAll(async () => {
process.chdir(rootDir);
server = await createServer({ root: rootDir });
await server.listen("0" as unknown as number);
const addr = server.httpServer?.address() as {
port: number;
address: string;
family: string;
};
serverURL = `http://${addr.family === "IPv6" ? `[${addr.address}]` : addr.address}:${addr.port}`;
}, 30_000);

afterAll(async () => {
await server?.close();
});

test("serves Nitro API routes with dotted params under baseURL without redirecting", async () => {
for (const fetchDest of ["empty", "document", undefined]) {
const headers: Record<string, string> = {};
if (fetchDest) {
headers["sec-fetch-dest"] = fetchDest;
}
const response = await fetch(`${serverURL}/subdir/api/proxy/todos/Package.todos.Entity.3`, {
headers,
redirect: "manual",
});

expect(response.status, `sec-fetch-dest: ${fetchDest}`).toBe(200);
expect(response.headers.get("location"), `sec-fetch-dest: ${fetchDest}`).toBeNull();
expect(await response.text(), `sec-fetch-dest: ${fetchDest}`).toBe(
"todos/Package.todos.Entity.3"
);
}
});
});
Loading