diff --git a/src/app.ts b/src/app.ts index 4836fd0b..04507f40 100644 --- a/src/app.ts +++ b/src/app.ts @@ -30,7 +30,7 @@ import { segmentToMiddlewares, } from "./segments.ts"; import { isHandlerByMethod, type PageResponse } from "./handlers.ts"; -import { serveInternalStaticFiles } from "./middlewares/static_files.ts"; +import { staticFiles } from "./middlewares/static_files.ts"; // TODO: Completed type clashes in older Deno versions // deno-lint-ignore no-explicit-any @@ -408,17 +408,11 @@ export class App { return missingBuildHandler; } - const staticMids: MiddlewareFn[] = []; - if (this.#root.middlewares.length > 0) { - staticMids.push(...this.#root.middlewares); - } - - staticMids.push(serveInternalStaticFiles()); - + // Fallthrough this.#addMiddleware( "ALL", "*", - staticMids, + [...this.#root.middlewares, staticFiles()], true, ); diff --git a/src/middlewares/static_files.ts b/src/middlewares/static_files.ts index 9a2d7812..87974c33 100644 --- a/src/middlewares/static_files.ts +++ b/src/middlewares/static_files.ts @@ -7,20 +7,13 @@ import { tracer } from "../otel.ts"; import { getBuildCache } from "../context.ts"; /** - * Fresh middleware to enable file-system based routing. + * Fresh middleware to serve static files from the `static/` directory. * ```ts * // Enable Fresh static file serving * app.use(staticFiles()); * ``` - * @deprecated This is not needed anymore. Remove this middleware. */ export function staticFiles(): MiddlewareFn { - return async function freshStaticFiles(ctx) { - return await ctx.next(); - }; -} - -export function serveInternalStaticFiles(): MiddlewareFn { return async function freshServeStaticFiles(ctx) { const { req, url, config } = ctx; diff --git a/src/middlewares/static_files_test.ts b/src/middlewares/static_files_test.ts index 5e6b045d..bb9c1c1d 100644 --- a/src/middlewares/static_files_test.ts +++ b/src/middlewares/static_files_test.ts @@ -1,4 +1,4 @@ -import { serveInternalStaticFiles } from "./static_files.ts"; +import { staticFiles } from "./static_files.ts"; import { serveMiddleware } from "../test_utils.ts"; import type { BuildCache, StaticFile } from "../build_cache.ts"; import { expect } from "@std/expect"; @@ -39,7 +39,7 @@ Deno.test("static files - 200", async () => { "foo.css": { content: "body {}", hash: null }, }); const server = serveMiddleware( - serveInternalStaticFiles(), + staticFiles(), { buildCache }, ); @@ -59,7 +59,7 @@ Deno.test("static files - HEAD 200", async () => { "foo.css": { content: "body {}", hash: null }, }); const server = serveMiddleware( - serveInternalStaticFiles(), + staticFiles(), { buildCache }, ); @@ -76,7 +76,7 @@ Deno.test("static files - etag", async () => { "foo.css": { content: "body {}", hash: "123" }, }); const server = serveMiddleware( - serveInternalStaticFiles(), + staticFiles(), { buildCache }, ); @@ -101,7 +101,7 @@ Deno.test("static files - etag", async () => { Deno.test("static files - 404 on missing favicon.ico", async () => { const buildCache = new MockBuildCache({}); const server = serveMiddleware( - serveInternalStaticFiles(), + staticFiles(), { buildCache }, ); const res = await server.get("favicon.ico"); @@ -114,7 +114,7 @@ Deno.test("static files - 405 on wrong HTTP method", async () => { "foo.css": { content: "body {}", hash: null }, }); const server = serveMiddleware( - serveInternalStaticFiles(), + staticFiles(), { buildCache }, ); @@ -131,7 +131,7 @@ Deno.test("static files - disables caching in development", async () => { "foo.css": { content: "body {}", hash: null }, }); const server = serveMiddleware( - serveInternalStaticFiles(), + staticFiles(), { buildCache, config: { @@ -159,7 +159,7 @@ Deno.test("static files - enables caching in production", async () => { "foo.css": { content: "body {}", hash: null }, }); const server = serveMiddleware( - serveInternalStaticFiles(), + staticFiles(), { buildCache, config: { @@ -189,7 +189,7 @@ Deno.test("static files - decoded pathname", async () => { "인천.avif": { content: "body {}", hash: null }, }); const server = serveMiddleware( - serveInternalStaticFiles(), + staticFiles(), { buildCache }, ); @@ -205,3 +205,22 @@ Deno.test("static files - decoded pathname", async () => { expect(res.status).toEqual(200); } }); + +Deno.test("static files - fallthrough", async () => { + const buildCache = new MockBuildCache({ + "foo.css": { content: "body {}", hash: null }, + }); + + const server = serveMiddleware( + staticFiles(), + { buildCache, next: () => Promise.resolve(new Response("it works")) }, + ); + + let res = await server.get("foo.css"); + let text = await res.text(); + expect(text).toEqual("body {}"); + + res = await server.get("/"); + text = await res.text(); + expect(text).toEqual("it works"); +});