fix: support non-fallthrough static files like before (#3099)

This commit is contained in:
Marvin Hagemeister
2025-07-16 14:34:39 +02:00
committed by GitHub
parent 776a275b5d
commit 599973bb5c
3 changed files with 32 additions and 26 deletions
+3 -9
View File
@@ -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<State> {
return missingBuildHandler;
}
const staticMids: MiddlewareFn<State>[] = [];
if (this.#root.middlewares.length > 0) {
staticMids.push(...this.#root.middlewares);
}
staticMids.push(serveInternalStaticFiles());
// Fallthrough
this.#addMiddleware(
"ALL",
"*",
staticMids,
[...this.#root.middlewares, staticFiles()],
true,
);
+1 -8
View File
@@ -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<T>(): MiddlewareFn<T> {
return async function freshStaticFiles(ctx) {
return await ctx.next();
};
}
export function serveInternalStaticFiles<T>(): MiddlewareFn<T> {
return async function freshServeStaticFiles(ctx) {
const { req, url, config } = ctx;
+28 -9
View File
@@ -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");
});