diff --git a/packages/fresh/src/app.ts b/packages/fresh/src/app.ts index 08f90dbd..a7a936a5 100644 --- a/packages/fresh/src/app.ts +++ b/packages/fresh/src/app.ts @@ -328,9 +328,15 @@ export class App { const cmd = app.#commands[i]; if (cmd.type !== CommandType.App && cmd.type !== CommandType.NotFound) { + // Apply the inner app's basePath if it exists + let effectivePattern = cmd.pattern; + if (app.config.basePath) { + effectivePattern = mergePath(app.config.basePath, cmd.pattern, false); + } + const clone = { ...cmd, - pattern: mergePath(path, cmd.pattern, true), + pattern: mergePath(path, effectivePattern, true), includeLastSegment: cmd.pattern === "/" || cmd.includeLastSegment, }; this.#commands.push(clone); diff --git a/packages/fresh/src/app_test.tsx b/packages/fresh/src/app_test.tsx index f2c6570c..d5111af2 100644 --- a/packages/fresh/src/app_test.tsx +++ b/packages/fresh/src/app_test.tsx @@ -777,3 +777,75 @@ Deno.test("App - ctx.render() without app wrapper or layout", async () => { const res = await server.get("/"); expect(await res.text()).toContain("index<"); }); + +Deno.test("App - .mountApp() with basePath", async () => { + const innerApp = new App({ basePath: "/api" }) + .get("/users", () => new Response("users")); + + const app = new App() + .get("/", () => new Response("home")) + .mountApp("/v1", innerApp); + + const server = new FakeServer(app.handler()); + + let res = await server.get("/"); + expect(await res.text()).toEqual("home"); + + // Fixed behavior: inner app's basePath is now applied + res = await server.get("/v1/api/users"); + expect(await res.text()).toEqual("users"); + + // Routes without the full basePath should not work + res = await server.get("/v1/users"); + expect(res.status).toEqual(404); +}); + +Deno.test("App - .mountApp() with main app basePath", async () => { + const innerApp = new App() + .get("/data", () => new Response("data")); + + const app = new App({ basePath: "/main" }) + .get("/", () => new Response("home")) + .mountApp("/sub", innerApp); + + const server = new FakeServer(app.handler()); + + let res = await server.get("/main"); + expect(await res.text()).toEqual("home"); + + res = await server.get("/main/sub/data"); + expect(await res.text()).toEqual("data"); + + // Should not work without main basePath + res = await server.get("/sub/data"); + expect(res.status).toEqual(404); +}); + +Deno.test("App - .mountApp() with both main and inner basePath", async () => { + const innerApp = new App({ basePath: "/api/v2" }) + .get("/users", () => new Response("users")) + .get("/posts", () => new Response("posts")); + + const app = new App({ basePath: "/main" }) + .get("/", () => new Response("home")) + .mountApp("/services", innerApp); + + const server = new FakeServer(app.handler()); + + let res = await server.get("/main"); + expect(await res.text()).toEqual("home"); + + // Both basePaths should be applied: main basePath + mount path + inner basePath + res = await server.get("/main/services/api/v2/users"); + expect(await res.text()).toEqual("users"); + + res = await server.get("/main/services/api/v2/posts"); + expect(await res.text()).toEqual("posts"); + + // Partial paths should not work + res = await server.get("/services/api/v2/users"); + expect(res.status).toEqual(404); + + res = await server.get("/main/services/users"); + expect(res.status).toEqual(404); +});