Skip to content

Commit 895640d

Browse files
fix: basePath not working in dev server
1 parent 073bacf commit 895640d

5 files changed

Lines changed: 64 additions & 12 deletions

File tree

packages/fresh/src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ export class App<State> {
330330
if (cmd.type !== CommandType.App && cmd.type !== CommandType.NotFound) {
331331
const clone = {
332332
...cmd,
333-
pattern: mergePath(path, cmd.pattern),
333+
pattern: mergePath(path, cmd.pattern, true),
334334
includeLastSegment: cmd.pattern === "/" || cmd.includeLastSegment,
335335
};
336336
this.#commands.push(clone);

packages/fresh/src/commands.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ function applyCommandsInner<State>(
273273
const routePath = mergePath(
274274
basePath,
275275
config?.routeOverride ?? pattern,
276+
false,
276277
);
277278

278279
let def: Route<State>;
@@ -304,6 +305,7 @@ function applyCommandsInner<State>(
304305
const routePath = toRoutePath(mergePath(
305306
basePath,
306307
route.config?.routeOverride ?? pattern,
308+
false,
307309
));
308310

309311
if (typeof route.handler === "function") {
@@ -333,7 +335,7 @@ function applyCommandsInner<State>(
333335

334336
result.push(...fns);
335337

336-
const resPath = toRoutePath(mergePath(basePath, pattern));
338+
const resPath = toRoutePath(mergePath(basePath, pattern, false));
337339
if (method === "ALL") {
338340
router.add("GET", resPath, result);
339341
router.add("DELETE", resPath, result);

packages/fresh/src/dev/builder_test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,42 @@ Deno.test({
389389
sanitizeResources: false,
390390
});
391391

392+
Deno.test({
393+
name: "Builder - dev server supports basePath",
394+
fn: async () => {
395+
const root = path.join(import.meta.dirname!, "..", "..");
396+
await using _tmp = await withTmpDir({ dir: root, prefix: "tmp_builder_" });
397+
const tmp = _tmp.dir;
398+
399+
const app = new App({ basePath: "/foo/bar" })
400+
.get("/", () => new Response("ok"))
401+
.get("/asdf", () => new Response("ok"));
402+
403+
const builder = new Builder({
404+
root: tmp,
405+
outDir: path.join(tmp, "dist"),
406+
});
407+
408+
const controller = new AbortController();
409+
let address;
410+
await builder.listen(() => Promise.resolve<App<unknown>>(app), {
411+
signal: controller.signal,
412+
onListen(addr) {
413+
address = `http://localhost:${addr.port}`;
414+
},
415+
});
416+
417+
const res = await fetch(`${address}/foo/bar/asdf`);
418+
419+
const text = await res.text();
420+
expect(text).toEqual("ok");
421+
422+
controller.abort();
423+
},
424+
sanitizeOps: false,
425+
sanitizeResources: false,
426+
});
427+
392428
Deno.test({
393429
name: "Builder - serves static files in subdir",
394430
fn: async () => {

packages/fresh/src/router.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,15 @@ export function patternToSegments(
284284
return out;
285285
}
286286

287-
export function mergePath(basePath: string, path: string): string {
287+
export function mergePath(
288+
basePath: string,
289+
path: string,
290+
isMounting: boolean,
291+
): string {
288292
if (basePath.endsWith("*")) basePath = basePath.slice(0, -1);
289293
if (basePath === "/") basePath = "";
290294

291-
if (path === "*") path = "";
295+
if (path === "*") path = isMounting ? "" : "/*";
292296
else if (path === "/*") path = "/*";
293297

294298
const s = (basePath !== "" && path === "/") ? "" : path;

packages/fresh/src/router_test.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,22 @@ Deno.test("patternToSegments", () => {
155155
});
156156

157157
Deno.test("mergePath", () => {
158-
expect(mergePath("", "/foo")).toEqual("/foo");
159-
expect(mergePath("/", "/foo")).toEqual("/foo");
160-
expect(mergePath("/foo/bar", "/")).toEqual("/foo/bar");
161-
expect(mergePath("/foo/bar", "/baz")).toEqual("/foo/bar/baz");
162-
expect(mergePath("*", "/baz")).toEqual("/baz");
163-
expect(mergePath("/*", "/baz")).toEqual("/baz");
164-
expect(mergePath("/foo", "*")).toEqual("/foo");
165-
expect(mergePath("/foo", "/*")).toEqual("/foo/*");
158+
expect(mergePath("", "/foo", false)).toEqual("/foo");
159+
expect(mergePath("/", "/foo", false)).toEqual("/foo");
160+
expect(mergePath("/foo/bar", "/", false)).toEqual("/foo/bar");
161+
expect(mergePath("/foo/bar", "/baz", false)).toEqual("/foo/bar/baz");
162+
expect(mergePath("*", "/baz", false)).toEqual("/baz");
163+
expect(mergePath("/*", "/baz", false)).toEqual("/baz");
164+
expect(mergePath("/foo", "*", false)).toEqual("/foo/*");
165+
expect(mergePath("/foo", "/*", false)).toEqual("/foo/*");
166+
167+
// mounting
168+
expect(mergePath("", "/foo", true)).toEqual("/foo");
169+
expect(mergePath("/", "/foo", true)).toEqual("/foo");
170+
expect(mergePath("/foo/bar", "/", true)).toEqual("/foo/bar");
171+
expect(mergePath("/foo/bar", "/baz", true)).toEqual("/foo/bar/baz");
172+
expect(mergePath("*", "/baz", true)).toEqual("/baz");
173+
expect(mergePath("/*", "/baz", true)).toEqual("/baz");
174+
expect(mergePath("/foo", "*", true)).toEqual("/foo");
175+
expect(mergePath("/foo", "/*", true)).toEqual("/foo/*");
166176
});

0 commit comments

Comments
 (0)