diff --git a/src/app_test.tsx b/src/app_test.tsx index 7539a465..86c43b64 100644 --- a/src/app_test.tsx +++ b/src/app_test.tsx @@ -617,6 +617,26 @@ Deno.test("App - .route() with basePath", async () => { expect(res.status).toEqual(200); }); +Deno.test("App - .all() with *", async () => { + const app = new App() + .all("*", () => new Response("ok")); + + const server = new FakeServer(app.handler()); + + const res = await server.get("/"); + expect(await res.text()).toEqual("ok"); +}); + +Deno.test("App - .route() with *", async () => { + const app = new App() + .route("*", { handler: () => new Response("ok") }); + + const server = new FakeServer(app.handler()); + + const res = await server.get("/"); + expect(await res.text()).toEqual("ok"); +}); + Deno.test("App - .use() - lazy", async () => { const app = new App<{ text: string }>() // deno-lint-ignore require-await diff --git a/src/commands.ts b/src/commands.ts index ff7e2a2d..a8356e68 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,7 +1,7 @@ import { HttpError } from "./error.ts"; import { isHandlerByMethod, type PageResponse } from "./handlers.ts"; import type { MaybeLazyMiddleware, Middleware } from "./middlewares/mod.ts"; -import { mergePath, type Method, type Router } from "./router.ts"; +import { mergePath, type Method, type Router, toRoutePath } from "./router.ts"; import { getOrCreateSegment, newSegment, @@ -301,10 +301,10 @@ function applyCommandsInner( } else { fns.push((ctx) => renderRoute(ctx, route)); - const routePath = mergePath( + const routePath = toRoutePath(mergePath( basePath, route.config?.routeOverride ?? pattern, - ); + )); if (typeof route.handler === "function") { router.add("GET", routePath, fns); @@ -333,7 +333,7 @@ function applyCommandsInner( result.push(...fns); - const resPath = mergePath(basePath, pattern); + const resPath = toRoutePath(mergePath(basePath, pattern)); if (method === "ALL") { router.add("GET", resPath, result); router.add("DELETE", resPath, result); diff --git a/src/dev/builder.ts b/src/dev/builder.ts index ec351798..08bc3634 100644 --- a/src/dev/builder.ts +++ b/src/dev/builder.ts @@ -167,6 +167,12 @@ export class Builder { await buildCache.prepare(); + app.config.root = this.config.root; + app.config.mode = "development"; + setBuildCache(app, buildCache); + + const appHandler = app.handler(); + const devApp = new App(app.config) .use(liveReload()) .use(devErrorOverlay()) @@ -176,7 +182,7 @@ export class Builder { await this.#ready.promise; return ctx.next(); }) - .mountApp("/*", app); + .all("*", (ctx) => appHandler(ctx.req, ctx.info)); devApp.config.root = this.config.root; devApp.config.mode = "development"; diff --git a/src/dev/builder_test.ts b/src/dev/builder_test.ts index 1f586fc2..bb1784ff 100644 --- a/src/dev/builder_test.ts +++ b/src/dev/builder_test.ts @@ -353,6 +353,41 @@ export const app = new App().fsRoutes()`, sanitizeResources: false, }); +Deno.test({ + name: "Builder - dev server doesn't overtake _error handler", + 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() + .onError("*", () => new Response("it works")) + .get("/", () => new Response("no")); + + const builder = new Builder({ + root: tmp, + outDir: path.join(tmp, "dist"), + }); + + const controller = new AbortController(); + await builder.listen(() => Promise.resolve>(app), { + signal: controller.signal, + async onListen(addr) { + const res = await fetch(`http://localhost:${addr.port}/invalid`); + + const text = await res.text(); + expect(text).toEqual("it works"); + + controller.abort(); + }, + }); + + // expect(text).toContain("

ok

"); + }, + sanitizeOps: false, + sanitizeResources: false, +}); + Deno.test({ // Currently waiting on bug fixes in @deno/loader // to support resolving without passing entry points diff --git a/src/router.ts b/src/router.ts index 59251e0c..69acaf71 100644 --- a/src/router.ts +++ b/src/router.ts @@ -294,3 +294,8 @@ export function mergePath(basePath: string, path: string): string { const s = (basePath !== "" && path === "/") ? "" : path; return basePath + s; } + +export function toRoutePath(path: string): string { + if (path === "") return "*"; + return path; +}