Skip to content

Commit 19d3589

Browse files
authored
fix: apply basePath correctly in dev mode for root index (#3219)
## Summary - Fixed an issue where basePath was not correctly applied in dev mode for the root index route - The dev server now uses root path "/" while preserving the configured basePath for the main app
1 parent ee9d1d0 commit 19d3589

3 files changed

Lines changed: 31 additions & 8 deletions

File tree

packages/fresh/src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export type ListenOptions =
7272
& {
7373
remoteAddress?: string;
7474
};
75-
function createOnListen(
75+
export function createOnListen(
7676
basePath: string,
7777
options: ListenOptions,
7878
): (localAddr: Deno.NetAddr) => void {

packages/fresh/src/dev/builder.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { App, type ListenOptions, setBuildCache } from "../app.ts";
1+
import {
2+
App,
3+
createOnListen,
4+
type ListenOptions,
5+
setBuildCache,
6+
} from "../app.ts";
27
import { fsAdapter } from "../fs.ts";
38
import * as path from "@std/path";
49
import * as colors from "@std/fmt/colors";
@@ -181,7 +186,11 @@ export class Builder<State = any> {
181186

182187
const appHandler = app.handler();
183188

184-
const devApp = new App<State>(app.config)
189+
// Store original basePath for display purposes
190+
const originalBasePath = app.config.basePath;
191+
192+
const devConfig = { ...app.config, basePath: "" };
193+
const devApp = new App<State>(devConfig)
185194
.use(liveReload())
186195
.use(devErrorOverlay())
187196
.use(automaticWorkspaceFolders(this.config.root))
@@ -200,7 +209,11 @@ export class Builder<State = any> {
200209
// Boot in parallel to spin up the server quicker. We'll hold
201210
// requests until the required assets are processed.
202211
await Promise.all([
203-
devApp.listen(options),
212+
devApp.listen({
213+
...options,
214+
onListen: options.onListen ??
215+
createOnListen(originalBasePath, options),
216+
}),
204217
this.#build(buildCache, true),
205218
]);
206219
return;

packages/fresh/src/dev/builder_test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ Deno.test({
433433
const tmp = _tmp.dir;
434434

435435
const app = new App({ basePath: "/foo/bar" })
436-
.get("/", () => new Response("ok"))
436+
.get("/", () => new Response("root index"))
437437
.get("/asdf", () => new Response("ok"));
438438

439439
const builder = new Builder({
@@ -450,10 +450,20 @@ Deno.test({
450450
},
451451
});
452452

453-
const res = await fetch(`${address}/foo/bar/asdf`);
453+
// Test regular route
454+
const res1 = await fetch(`${address}/foo/bar/asdf`);
455+
const text1 = await res1.text();
456+
expect(text1).toEqual("ok");
454457

455-
const text = await res.text();
456-
expect(text).toEqual("ok");
458+
// Test root index with basePath (without trailing slash)
459+
// This was the main issue - accessing /foo/bar should work
460+
const res2 = await fetch(`${address}/foo/bar`);
461+
const text2 = await res2.text();
462+
expect(res2.status).toEqual(200);
463+
expect(text2).toEqual("root index");
464+
465+
// Verify original app config is not mutated by dev builder
466+
expect(app.config.basePath).toEqual("/foo/bar");
457467

458468
controller.abort();
459469
},

0 commit comments

Comments
 (0)