fix: support non-fallthrough static files like before (#3099)
This commit is contained in:
+3
-9
@@ -30,7 +30,7 @@ import {
|
|||||||
segmentToMiddlewares,
|
segmentToMiddlewares,
|
||||||
} from "./segments.ts";
|
} from "./segments.ts";
|
||||||
import { isHandlerByMethod, type PageResponse } from "./handlers.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
|
// TODO: Completed type clashes in older Deno versions
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
@@ -408,17 +408,11 @@ export class App<State> {
|
|||||||
return missingBuildHandler;
|
return missingBuildHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
const staticMids: MiddlewareFn<State>[] = [];
|
// Fallthrough
|
||||||
if (this.#root.middlewares.length > 0) {
|
|
||||||
staticMids.push(...this.#root.middlewares);
|
|
||||||
}
|
|
||||||
|
|
||||||
staticMids.push(serveInternalStaticFiles());
|
|
||||||
|
|
||||||
this.#addMiddleware(
|
this.#addMiddleware(
|
||||||
"ALL",
|
"ALL",
|
||||||
"*",
|
"*",
|
||||||
staticMids,
|
[...this.#root.middlewares, staticFiles()],
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -7,20 +7,13 @@ import { tracer } from "../otel.ts";
|
|||||||
import { getBuildCache } from "../context.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
|
* ```ts
|
||||||
* // Enable Fresh static file serving
|
* // Enable Fresh static file serving
|
||||||
* app.use(staticFiles());
|
* app.use(staticFiles());
|
||||||
* ```
|
* ```
|
||||||
* @deprecated This is not needed anymore. Remove this middleware.
|
|
||||||
*/
|
*/
|
||||||
export function staticFiles<T>(): MiddlewareFn<T> {
|
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) {
|
return async function freshServeStaticFiles(ctx) {
|
||||||
const { req, url, config } = ctx;
|
const { req, url, config } = ctx;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { serveInternalStaticFiles } from "./static_files.ts";
|
import { staticFiles } from "./static_files.ts";
|
||||||
import { serveMiddleware } from "../test_utils.ts";
|
import { serveMiddleware } from "../test_utils.ts";
|
||||||
import type { BuildCache, StaticFile } from "../build_cache.ts";
|
import type { BuildCache, StaticFile } from "../build_cache.ts";
|
||||||
import { expect } from "@std/expect";
|
import { expect } from "@std/expect";
|
||||||
@@ -39,7 +39,7 @@ Deno.test("static files - 200", async () => {
|
|||||||
"foo.css": { content: "body {}", hash: null },
|
"foo.css": { content: "body {}", hash: null },
|
||||||
});
|
});
|
||||||
const server = serveMiddleware(
|
const server = serveMiddleware(
|
||||||
serveInternalStaticFiles(),
|
staticFiles(),
|
||||||
{ buildCache },
|
{ buildCache },
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ Deno.test("static files - HEAD 200", async () => {
|
|||||||
"foo.css": { content: "body {}", hash: null },
|
"foo.css": { content: "body {}", hash: null },
|
||||||
});
|
});
|
||||||
const server = serveMiddleware(
|
const server = serveMiddleware(
|
||||||
serveInternalStaticFiles(),
|
staticFiles(),
|
||||||
{ buildCache },
|
{ buildCache },
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ Deno.test("static files - etag", async () => {
|
|||||||
"foo.css": { content: "body {}", hash: "123" },
|
"foo.css": { content: "body {}", hash: "123" },
|
||||||
});
|
});
|
||||||
const server = serveMiddleware(
|
const server = serveMiddleware(
|
||||||
serveInternalStaticFiles(),
|
staticFiles(),
|
||||||
{ buildCache },
|
{ buildCache },
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -101,7 +101,7 @@ Deno.test("static files - etag", async () => {
|
|||||||
Deno.test("static files - 404 on missing favicon.ico", async () => {
|
Deno.test("static files - 404 on missing favicon.ico", async () => {
|
||||||
const buildCache = new MockBuildCache({});
|
const buildCache = new MockBuildCache({});
|
||||||
const server = serveMiddleware(
|
const server = serveMiddleware(
|
||||||
serveInternalStaticFiles(),
|
staticFiles(),
|
||||||
{ buildCache },
|
{ buildCache },
|
||||||
);
|
);
|
||||||
const res = await server.get("favicon.ico");
|
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 },
|
"foo.css": { content: "body {}", hash: null },
|
||||||
});
|
});
|
||||||
const server = serveMiddleware(
|
const server = serveMiddleware(
|
||||||
serveInternalStaticFiles(),
|
staticFiles(),
|
||||||
{ buildCache },
|
{ buildCache },
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ Deno.test("static files - disables caching in development", async () => {
|
|||||||
"foo.css": { content: "body {}", hash: null },
|
"foo.css": { content: "body {}", hash: null },
|
||||||
});
|
});
|
||||||
const server = serveMiddleware(
|
const server = serveMiddleware(
|
||||||
serveInternalStaticFiles(),
|
staticFiles(),
|
||||||
{
|
{
|
||||||
buildCache,
|
buildCache,
|
||||||
config: {
|
config: {
|
||||||
@@ -159,7 +159,7 @@ Deno.test("static files - enables caching in production", async () => {
|
|||||||
"foo.css": { content: "body {}", hash: null },
|
"foo.css": { content: "body {}", hash: null },
|
||||||
});
|
});
|
||||||
const server = serveMiddleware(
|
const server = serveMiddleware(
|
||||||
serveInternalStaticFiles(),
|
staticFiles(),
|
||||||
{
|
{
|
||||||
buildCache,
|
buildCache,
|
||||||
config: {
|
config: {
|
||||||
@@ -189,7 +189,7 @@ Deno.test("static files - decoded pathname", async () => {
|
|||||||
"인천.avif": { content: "body {}", hash: null },
|
"인천.avif": { content: "body {}", hash: null },
|
||||||
});
|
});
|
||||||
const server = serveMiddleware(
|
const server = serveMiddleware(
|
||||||
serveInternalStaticFiles(),
|
staticFiles(),
|
||||||
{ buildCache },
|
{ buildCache },
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -205,3 +205,22 @@ Deno.test("static files - decoded pathname", async () => {
|
|||||||
expect(res.status).toEqual(200);
|
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");
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user