fix: basePath not working in dev server (#3202)
Fixes https://github.com/denoland/fresh/issues/3201
This commit is contained in:
@@ -330,7 +330,7 @@ export class App<State> {
|
|||||||
if (cmd.type !== CommandType.App && cmd.type !== CommandType.NotFound) {
|
if (cmd.type !== CommandType.App && cmd.type !== CommandType.NotFound) {
|
||||||
const clone = {
|
const clone = {
|
||||||
...cmd,
|
...cmd,
|
||||||
pattern: mergePath(path, cmd.pattern),
|
pattern: mergePath(path, cmd.pattern, true),
|
||||||
includeLastSegment: cmd.pattern === "/" || cmd.includeLastSegment,
|
includeLastSegment: cmd.pattern === "/" || cmd.includeLastSegment,
|
||||||
};
|
};
|
||||||
this.#commands.push(clone);
|
this.#commands.push(clone);
|
||||||
|
|||||||
@@ -273,6 +273,7 @@ function applyCommandsInner<State>(
|
|||||||
const routePath = mergePath(
|
const routePath = mergePath(
|
||||||
basePath,
|
basePath,
|
||||||
config?.routeOverride ?? pattern,
|
config?.routeOverride ?? pattern,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
let def: Route<State>;
|
let def: Route<State>;
|
||||||
@@ -304,6 +305,7 @@ function applyCommandsInner<State>(
|
|||||||
const routePath = toRoutePath(mergePath(
|
const routePath = toRoutePath(mergePath(
|
||||||
basePath,
|
basePath,
|
||||||
route.config?.routeOverride ?? pattern,
|
route.config?.routeOverride ?? pattern,
|
||||||
|
false,
|
||||||
));
|
));
|
||||||
|
|
||||||
if (typeof route.handler === "function") {
|
if (typeof route.handler === "function") {
|
||||||
@@ -333,7 +335,7 @@ function applyCommandsInner<State>(
|
|||||||
|
|
||||||
result.push(...fns);
|
result.push(...fns);
|
||||||
|
|
||||||
const resPath = toRoutePath(mergePath(basePath, pattern));
|
const resPath = toRoutePath(mergePath(basePath, pattern, false));
|
||||||
if (method === "ALL") {
|
if (method === "ALL") {
|
||||||
router.add("GET", resPath, result);
|
router.add("GET", resPath, result);
|
||||||
router.add("DELETE", resPath, result);
|
router.add("DELETE", resPath, result);
|
||||||
|
|||||||
@@ -389,6 +389,42 @@ Deno.test({
|
|||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "Builder - dev server supports basePath",
|
||||||
|
fn: async () => {
|
||||||
|
const root = path.join(import.meta.dirname!, "..", "..");
|
||||||
|
await using _tmp = await withTmpDir({ dir: root, prefix: "tmp_builder_" });
|
||||||
|
const tmp = _tmp.dir;
|
||||||
|
|
||||||
|
const app = new App({ basePath: "/foo/bar" })
|
||||||
|
.get("/", () => new Response("ok"))
|
||||||
|
.get("/asdf", () => new Response("ok"));
|
||||||
|
|
||||||
|
const builder = new Builder({
|
||||||
|
root: tmp,
|
||||||
|
outDir: path.join(tmp, "dist"),
|
||||||
|
});
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
let address;
|
||||||
|
await builder.listen(() => Promise.resolve<App<unknown>>(app), {
|
||||||
|
signal: controller.signal,
|
||||||
|
onListen(addr) {
|
||||||
|
address = `http://localhost:${addr.port}`;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetch(`${address}/foo/bar/asdf`);
|
||||||
|
|
||||||
|
const text = await res.text();
|
||||||
|
expect(text).toEqual("ok");
|
||||||
|
|
||||||
|
controller.abort();
|
||||||
|
},
|
||||||
|
sanitizeOps: false,
|
||||||
|
sanitizeResources: false,
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "Builder - serves static files in subdir",
|
name: "Builder - serves static files in subdir",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
|
|||||||
@@ -284,11 +284,15 @@ export function patternToSegments(
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mergePath(basePath: string, path: string): string {
|
export function mergePath(
|
||||||
|
basePath: string,
|
||||||
|
path: string,
|
||||||
|
isMounting: boolean,
|
||||||
|
): string {
|
||||||
if (basePath.endsWith("*")) basePath = basePath.slice(0, -1);
|
if (basePath.endsWith("*")) basePath = basePath.slice(0, -1);
|
||||||
if (basePath === "/") basePath = "";
|
if (basePath === "/") basePath = "";
|
||||||
|
|
||||||
if (path === "*") path = "";
|
if (path === "*") path = isMounting ? "" : "/*";
|
||||||
else if (path === "/*") path = "/*";
|
else if (path === "/*") path = "/*";
|
||||||
|
|
||||||
const s = (basePath !== "" && path === "/") ? "" : path;
|
const s = (basePath !== "" && path === "/") ? "" : path;
|
||||||
|
|||||||
@@ -155,12 +155,22 @@ Deno.test("patternToSegments", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("mergePath", () => {
|
Deno.test("mergePath", () => {
|
||||||
expect(mergePath("", "/foo")).toEqual("/foo");
|
expect(mergePath("", "/foo", false)).toEqual("/foo");
|
||||||
expect(mergePath("/", "/foo")).toEqual("/foo");
|
expect(mergePath("/", "/foo", false)).toEqual("/foo");
|
||||||
expect(mergePath("/foo/bar", "/")).toEqual("/foo/bar");
|
expect(mergePath("/foo/bar", "/", false)).toEqual("/foo/bar");
|
||||||
expect(mergePath("/foo/bar", "/baz")).toEqual("/foo/bar/baz");
|
expect(mergePath("/foo/bar", "/baz", false)).toEqual("/foo/bar/baz");
|
||||||
expect(mergePath("*", "/baz")).toEqual("/baz");
|
expect(mergePath("*", "/baz", false)).toEqual("/baz");
|
||||||
expect(mergePath("/*", "/baz")).toEqual("/baz");
|
expect(mergePath("/*", "/baz", false)).toEqual("/baz");
|
||||||
expect(mergePath("/foo", "*")).toEqual("/foo");
|
expect(mergePath("/foo", "*", false)).toEqual("/foo/*");
|
||||||
expect(mergePath("/foo", "/*")).toEqual("/foo/*");
|
expect(mergePath("/foo", "/*", false)).toEqual("/foo/*");
|
||||||
|
|
||||||
|
// mounting
|
||||||
|
expect(mergePath("", "/foo", true)).toEqual("/foo");
|
||||||
|
expect(mergePath("/", "/foo", true)).toEqual("/foo");
|
||||||
|
expect(mergePath("/foo/bar", "/", true)).toEqual("/foo/bar");
|
||||||
|
expect(mergePath("/foo/bar", "/baz", true)).toEqual("/foo/bar/baz");
|
||||||
|
expect(mergePath("*", "/baz", true)).toEqual("/baz");
|
||||||
|
expect(mergePath("/*", "/baz", true)).toEqual("/baz");
|
||||||
|
expect(mergePath("/foo", "*", true)).toEqual("/foo");
|
||||||
|
expect(mergePath("/foo", "/*", true)).toEqual("/foo/*");
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user