feat: expose layouts and pages on App (#3086)

This PR extracts the functionality from the `fs_routes` plugin and
exposes it onto `App`. These are the new APIs:

- `app.layout(pattern, route)`
- `app.onError(pattern, route)`
- `app.notFound(pattern, route)`
- `app.route(pattern, route)`

This also removes the need for `.use(staticFiles())`. This line can be
safely removed.

Fixes https://github.com/denoland/fresh/issues/3040
Fixes https://github.com/denoland/fresh/issues/3033
Fixes https://github.com/denoland/fresh/issues/2591
Fixes https://github.com/denoland/fresh/issues/950
Fixes https://github.com/denoland/fresh/issues/1918
This commit is contained in:
Marvin Hagemeister
2025-07-14 19:27:48 +02:00
committed by GitHub
parent ccb621d2a3
commit c11cf7b41a
35 changed files with 1328 additions and 971 deletions
+11 -8
View File
@@ -3,8 +3,8 @@ import { contentType as getContentType } from "@std/media-types/content-type";
import type { MiddlewareFn } from "./mod.ts";
import { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx";
import { BUILD_ID } from "../runtime/build_id.ts";
import { tracer } from "../otel.ts";
import { getBuildCache } from "../context.ts";
import { trace, tracer } from "../otel.ts";
/**
* Fresh middleware to enable file-system based routing.
@@ -12,11 +12,20 @@ import { trace, tracer } from "../otel.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;
const buildCache = getBuildCache(ctx);
if (buildCache === null) return await ctx.next();
let pathname = decodeURIComponent(url.pathname);
if (config.basePath) {
@@ -33,7 +42,7 @@ export function staticFiles<T>(): MiddlewareFn<T> {
if (pathname === "/favicon.ico") {
return new Response(null, { status: 404 });
}
return ctx.next();
return await ctx.next();
}
if (req.method !== "GET" && req.method !== "HEAD") {
@@ -41,12 +50,6 @@ export function staticFiles<T>(): MiddlewareFn<T> {
return new Response("Method Not Allowed", { status: 405 });
}
const parentSpan = trace.getActiveSpan();
if (parentSpan) {
parentSpan.updateName(`${req.method} /*`);
parentSpan.setAttribute("http.route", "/*");
}
const span = tracer.startSpan("static file", {
attributes: { "fresh.span_type": "static_file" },
startTime,