Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -408,21 +407,16 @@ export class App<State> {
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,
Expand Down
29 changes: 24 additions & 5 deletions src/middlewares/static_files_test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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"]);
});
Loading