diff --git a/src/app.ts b/src/app.ts index 93e541f0190..f0ff28f5b5a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -30,7 +30,6 @@ import { segmentToMiddlewares, } from "./segments.ts"; import { isHandlerByMethod, type PageResponse } from "./handlers.ts"; -import { staticFiles } from "./middlewares/static_files.ts"; // TODO: Completed type clashes in older Deno versions // deno-lint-ignore no-explicit-any @@ -408,21 +407,16 @@ export class App { return missingBuildHandler; } + const rootMiddlewares = this.#root.middlewares; + // Fallthrough - this.#addMiddleware( - "ALL", - "*", - [...this.#root.middlewares, staticFiles()], - true, - ); + this.#addMiddleware("ALL", "*", rootMiddlewares, true); for (let i = 0; i < this.#routeDefs.length; i++) { const route = this.#routeDefs[i]; this.#router.add(route.method, route.pattern, route.fns, route.unshift); } - const rootMiddlewares = this.#root.middlewares; - return async ( req: Request, conn: Deno.ServeHandlerInfo = DEFAULT_CONN_INFO, diff --git a/src/middlewares/static_files_test.ts b/src/middlewares/static_files_test.ts index bb9c1c1df04..fdc0af705c1 100644 --- a/src/middlewares/static_files_test.ts +++ b/src/middlewares/static_files_test.ts @@ -1,9 +1,10 @@ import { staticFiles } from "./static_files.ts"; -import { serveMiddleware } from "../test_utils.ts"; +import { FakeServer, serveMiddleware } from "../test_utils.ts"; import type { BuildCache, StaticFile } from "../build_cache.ts"; import { expect } from "@std/expect"; import { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx"; import { BUILD_ID } from "../runtime/build_id.ts"; +import { App, setBuildCache } from "../app.ts"; class MockBuildCache implements BuildCache { buildId = "MockId"; @@ -211,16 +212,34 @@ Deno.test("static files - fallthrough", async () => { "foo.css": { content: "body {}", hash: null }, }); - const server = serveMiddleware( - staticFiles(), - { buildCache, next: () => Promise.resolve(new Response("it works")) }, - ); + let called: string[] = []; + const app = new App<{ text: string }>() + .use((ctx) => { + ctx.state.text = "it"; + called.push("before"); + return ctx.next(); + }) + .use(staticFiles()) + .use((ctx) => { + ctx.state.text += " works"; + called.push("after"); + return ctx.next(); + }) + .get("/", (ctx) => new Response(ctx.state.text)); + + setBuildCache(app, buildCache); + + const server = new FakeServer(app.handler()); let res = await server.get("foo.css"); let text = await res.text(); expect(text).toEqual("body {}"); + expect(called).toEqual(["before"]); + + called = []; res = await server.get("/"); text = await res.text(); expect(text).toEqual("it works"); + expect(called).toEqual(["before", "after"]); });