Skip to content

Commit 599973b

Browse files
fix: support non-fallthrough static files like before (#3099)
1 parent 776a275 commit 599973b

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

src/app.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
segmentToMiddlewares,
3131
} from "./segments.ts";
3232
import { isHandlerByMethod, type PageResponse } from "./handlers.ts";
33-
import { serveInternalStaticFiles } from "./middlewares/static_files.ts";
33+
import { staticFiles } from "./middlewares/static_files.ts";
3434

3535
// TODO: Completed type clashes in older Deno versions
3636
// deno-lint-ignore no-explicit-any
@@ -408,17 +408,11 @@ export class App<State> {
408408
return missingBuildHandler;
409409
}
410410

411-
const staticMids: MiddlewareFn<State>[] = [];
412-
if (this.#root.middlewares.length > 0) {
413-
staticMids.push(...this.#root.middlewares);
414-
}
415-
416-
staticMids.push(serveInternalStaticFiles());
417-
411+
// Fallthrough
418412
this.#addMiddleware(
419413
"ALL",
420414
"*",
421-
staticMids,
415+
[...this.#root.middlewares, staticFiles()],
422416
true,
423417
);
424418

src/middlewares/static_files.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,13 @@ import { tracer } from "../otel.ts";
77
import { getBuildCache } from "../context.ts";
88

99
/**
10-
* Fresh middleware to enable file-system based routing.
10+
* Fresh middleware to serve static files from the `static/` directory.
1111
* ```ts
1212
* // Enable Fresh static file serving
1313
* app.use(staticFiles());
1414
* ```
15-
* @deprecated This is not needed anymore. Remove this middleware.
1615
*/
1716
export function staticFiles<T>(): MiddlewareFn<T> {
18-
return async function freshStaticFiles(ctx) {
19-
return await ctx.next();
20-
};
21-
}
22-
23-
export function serveInternalStaticFiles<T>(): MiddlewareFn<T> {
2417
return async function freshServeStaticFiles(ctx) {
2518
const { req, url, config } = ctx;
2619

src/middlewares/static_files_test.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { serveInternalStaticFiles } from "./static_files.ts";
1+
import { staticFiles } from "./static_files.ts";
22
import { serveMiddleware } from "../test_utils.ts";
33
import type { BuildCache, StaticFile } from "../build_cache.ts";
44
import { expect } from "@std/expect";
@@ -39,7 +39,7 @@ Deno.test("static files - 200", async () => {
3939
"foo.css": { content: "body {}", hash: null },
4040
});
4141
const server = serveMiddleware(
42-
serveInternalStaticFiles(),
42+
staticFiles(),
4343
{ buildCache },
4444
);
4545

@@ -59,7 +59,7 @@ Deno.test("static files - HEAD 200", async () => {
5959
"foo.css": { content: "body {}", hash: null },
6060
});
6161
const server = serveMiddleware(
62-
serveInternalStaticFiles(),
62+
staticFiles(),
6363
{ buildCache },
6464
);
6565

@@ -76,7 +76,7 @@ Deno.test("static files - etag", async () => {
7676
"foo.css": { content: "body {}", hash: "123" },
7777
});
7878
const server = serveMiddleware(
79-
serveInternalStaticFiles(),
79+
staticFiles(),
8080
{ buildCache },
8181
);
8282

@@ -101,7 +101,7 @@ Deno.test("static files - etag", async () => {
101101
Deno.test("static files - 404 on missing favicon.ico", async () => {
102102
const buildCache = new MockBuildCache({});
103103
const server = serveMiddleware(
104-
serveInternalStaticFiles(),
104+
staticFiles(),
105105
{ buildCache },
106106
);
107107
const res = await server.get("favicon.ico");
@@ -114,7 +114,7 @@ Deno.test("static files - 405 on wrong HTTP method", async () => {
114114
"foo.css": { content: "body {}", hash: null },
115115
});
116116
const server = serveMiddleware(
117-
serveInternalStaticFiles(),
117+
staticFiles(),
118118
{ buildCache },
119119
);
120120

@@ -131,7 +131,7 @@ Deno.test("static files - disables caching in development", async () => {
131131
"foo.css": { content: "body {}", hash: null },
132132
});
133133
const server = serveMiddleware(
134-
serveInternalStaticFiles(),
134+
staticFiles(),
135135
{
136136
buildCache,
137137
config: {
@@ -159,7 +159,7 @@ Deno.test("static files - enables caching in production", async () => {
159159
"foo.css": { content: "body {}", hash: null },
160160
});
161161
const server = serveMiddleware(
162-
serveInternalStaticFiles(),
162+
staticFiles(),
163163
{
164164
buildCache,
165165
config: {
@@ -189,7 +189,7 @@ Deno.test("static files - decoded pathname", async () => {
189189
"인천.avif": { content: "body {}", hash: null },
190190
});
191191
const server = serveMiddleware(
192-
serveInternalStaticFiles(),
192+
staticFiles(),
193193
{ buildCache },
194194
);
195195

@@ -205,3 +205,22 @@ Deno.test("static files - decoded pathname", async () => {
205205
expect(res.status).toEqual(200);
206206
}
207207
});
208+
209+
Deno.test("static files - fallthrough", async () => {
210+
const buildCache = new MockBuildCache({
211+
"foo.css": { content: "body {}", hash: null },
212+
});
213+
214+
const server = serveMiddleware(
215+
staticFiles(),
216+
{ buildCache, next: () => Promise.resolve(new Response("it works")) },
217+
);
218+
219+
let res = await server.get("foo.css");
220+
let text = await res.text();
221+
expect(text).toEqual("body {}");
222+
223+
res = await server.get("/");
224+
text = await res.text();
225+
expect(text).toEqual("it works");
226+
});

0 commit comments

Comments
 (0)