Skip to content

Commit 9b6fb92

Browse files
committed
fix: dev server now correctly handles basePath root index
- Set devApp basePath to "/" for consistent routing - Add displayBasePath option for correct startup message - Simplify middleware URL checks - Add regression test for basePath root index access
1 parent df851ef commit 9b6fb92

6 files changed

Lines changed: 25 additions & 67 deletions

File tree

packages/fresh/src/app.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ export type ListenOptions =
6969
>
7070
& {
7171
remoteAddress?: string;
72+
/** Base path to display in startup message (defaults to config.basePath) */
73+
displayBasePath?: string;
7274
};
7375
function createOnListen(
7476
basePath: string,
@@ -446,7 +448,8 @@ export class App<State> {
446448
*/
447449
async listen(options: ListenOptions = {}): Promise<void> {
448450
if (!options.onListen) {
449-
options.onListen = createOnListen(this.config.basePath, options);
451+
const displayBasePath = options.displayBasePath ?? this.config.basePath;
452+
options.onListen = createOnListen(displayBasePath, options);
450453
}
451454

452455
const handler = this.handler();

packages/fresh/src/app_basepath_index_test.tsx

Lines changed: 0 additions & 55 deletions
This file was deleted.

packages/fresh/src/dev/builder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ export class Builder<State = any> {
183183

184184
const appHandler = app.handler();
185185

186+
// Store original basePath for display purposes
187+
const originalBasePath = app.config.basePath;
188+
186189
const devConfig = { ...app.config, basePath: "/" };
187190
const devApp = new App<State>(devConfig)
188191
.use(liveReload())
@@ -203,7 +206,7 @@ export class Builder<State = any> {
203206
// Boot in parallel to spin up the server quicker. We'll hold
204207
// requests until the required assets are processed.
205208
await Promise.all([
206-
devApp.listen(options),
209+
devApp.listen({ ...options, displayBasePath: originalBasePath }),
207210
this.#build(buildCache, true),
208211
]);
209212
return;

packages/fresh/src/dev/builder_test.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ Deno.test({
397397
const tmp = _tmp.dir;
398398

399399
const app = new App({ basePath: "/foo/bar" })
400-
.get("/", () => new Response("ok"))
400+
.get("/", () => new Response("root index"))
401401
.get("/asdf", () => new Response("ok"));
402402

403403
const builder = new Builder({
@@ -414,10 +414,17 @@ Deno.test({
414414
},
415415
});
416416

417-
const res = await fetch(`${address}/foo/bar/asdf`);
417+
// Test regular route
418+
const res1 = await fetch(`${address}/foo/bar/asdf`);
419+
const text1 = await res1.text();
420+
expect(text1).toEqual("ok");
418421

419-
const text = await res.text();
420-
expect(text).toEqual("ok");
422+
// Test root index with basePath (without trailing slash)
423+
// This was the main issue - accessing /foo/bar should work
424+
const res2 = await fetch(`${address}/foo/bar`);
425+
const text2 = await res2.text();
426+
expect(res2.status).toEqual(200);
427+
expect(text2).toEqual("root index");
421428

422429
controller.abort();
423430
},

packages/fresh/src/dev/middlewares/error_overlay/middleware.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import { ErrorOverlay } from "./overlay.tsx";
66

77
export function devErrorOverlay<T>(): Middleware<T> {
88
return async (ctx) => {
9-
const { config, url } = ctx;
10-
if (url.pathname === config.basePath + DEV_ERROR_OVERLAY_URL) {
9+
const { url } = ctx;
10+
// In dev mode, basePath is always "/" so we check the URL directly
11+
if (url.pathname === DEV_ERROR_OVERLAY_URL) {
1112
return ctx.render(<ErrorOverlay url={url} />);
1213
}
1314

packages/fresh/src/dev/middlewares/live_reload.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ export function liveReload<T>(): Middleware<T> {
66
const revision = Date.now();
77

88
return (ctx) => {
9-
const { config, req, url } = ctx;
9+
const { req, url } = ctx;
1010

11-
const aliveUrl = config.basePath + ALIVE_URL;
12-
13-
if (url.pathname === aliveUrl) {
11+
// In dev mode, basePath is always "/" so we check the URL directly
12+
if (url.pathname === ALIVE_URL) {
1413
if (req.headers.get("upgrade") !== "websocket") {
1514
return new Response(null, { status: 501 });
1615
}

0 commit comments

Comments
 (0)