fix(dev): dev server should not overtake app's root error handler (#3165)

This commit is contained in:
Marvin Hagemeister
2025-08-01 10:08:13 +02:00
committed by GitHub
parent a2de5154ec
commit 5f928d2cac
5 changed files with 71 additions and 5 deletions
+20
View File
@@ -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
+4 -4
View File
@@ -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<State>(
} 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<State>(
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);
+7 -1
View File
@@ -167,6 +167,12 @@ export class Builder<State = any> {
await buildCache.prepare();
app.config.root = this.config.root;
app.config.mode = "development";
setBuildCache(app, buildCache);
const appHandler = app.handler();
const devApp = new App<State>(app.config)
.use(liveReload())
.use(devErrorOverlay())
@@ -176,7 +182,7 @@ export class Builder<State = any> {
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";
+35
View File
@@ -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<unknown>>(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("<h1>ok</h1>");
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
// Currently waiting on bug fixes in @deno/loader
// to support resolving without passing entry points
+5
View File
@@ -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;
}