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
+259 -80
View File
@@ -5,18 +5,32 @@ import { trace } from "@opentelemetry/api";
import { DENO_DEPLOYMENT_ID } from "./runtime/build_id.ts";
import * as colors from "@std/fmt/colors";
import { type MiddlewareFn, runMiddlewares } from "./middlewares/mod.ts";
import { FreshReqContext } from "./context.ts";
import { type Method, type Router, UrlPatternRouter } from "./router.ts";
import { Context, type ServerIslandRegistry } from "./context.ts";
import {
mergePath,
type Method,
type Router,
UrlPatternRouter,
} from "./router.ts";
import {
type FreshConfig,
normalizeConfig,
type ResolvedFreshConfig,
} from "./config.ts";
import { type BuildCache, ProdBuildCache } from "./build_cache.ts";
import type { ServerIslandRegistry } from "./context.ts";
import { FinishSetup, ForgotBuild } from "./finish_setup.tsx";
import { HttpError } from "./error.ts";
import { mergePaths, pathToExportName } from "./utils.ts";
import { pathToExportName } from "./utils.ts";
import type { LayoutConfig, Route } from "./types.ts";
import {
getOrCreateSegment,
newSegment,
renderRoute,
type RouteComponent,
segmentToMiddlewares,
} from "./segments.ts";
import { isHandlerByMethod, type PageResponse } from "./handlers.ts";
import { serveInternalStaticFiles } from "./middlewares/static_files.ts";
// TODO: Completed type clashes in older Deno versions
// deno-lint-ignore no-explicit-any
@@ -25,12 +39,44 @@ export const DEFAULT_CONN_INFO: any = {
remoteAddr: { transport: "tcp", hostname: "localhost", port: 1234 },
};
const DEFAULT_NOT_FOUND = () => {
/** Used to group mounted apps. Only internal */
let INTERNAL_ID = 0;
const DEFAULT_RENDER = <State>(): Promise<PageResponse<State>> =>
// deno-lint-ignore no-explicit-any
Promise.resolve({ data: {} as any });
const DEFAULT_NOT_FOUND = (): Promise<Response> => {
throw new HttpError(404);
};
const DEFAULT_NOT_ALLOWED_METHOD = () => {
const DEFAULT_NOT_ALLOWED_METHOD = (): Promise<Response> => {
throw new HttpError(405);
};
const defaultOptionsHandler = (methods: string[]): () => Promise<Response> => {
return () =>
Promise.resolve(
new Response(null, {
status: 204,
headers: { Allow: methods.join(", ") },
}),
);
};
// deno-lint-ignore require-await
const DEFAULT_ERROR_HANDLER = async <State>(ctx: Context<State>) => {
const { error } = ctx;
if (error instanceof HttpError) {
if (error.status >= 500) {
// deno-lint-ignore no-console
console.error(error);
}
return new Response(error.message, { status: error.status });
}
// deno-lint-ignore no-console
console.error(error);
return new Response("Internal server error", { status: 500 });
};
export type ListenOptions =
& Partial<
@@ -117,7 +163,6 @@ async function listenOnFreePort(
throw firstError;
}
export let getRouter: <State>(app: App<State>) => Router<MiddlewareFn<State>>;
// deno-lint-ignore no-explicit-any
export let getIslandRegistry: (app: App<any>) => ServerIslandRegistry;
// deno-lint-ignore no-explicit-any
@@ -132,9 +177,15 @@ export class App<State> {
#islandRegistry: ServerIslandRegistry = new Map();
#buildCache: BuildCache | null = null;
#islandNames = new Set<string>();
#root = newSegment<State>("", null);
#routeDefs: {
method: Method | "ALL";
pattern: string;
fns: MiddlewareFn<State>[];
unshift: boolean;
}[] = [];
static {
getRouter = (app) => app.#router;
getIslandRegistry = (app) => app.#islandRegistry;
getBuildCache = (app) => app.#buildCache;
setBuildCache = (app, cache) => app.#buildCache = cache;
@@ -175,76 +226,170 @@ export class App<State> {
return this;
}
use(middleware: MiddlewareFn<State>): this {
this.#router.addMiddleware(middleware);
use(...middleware: MiddlewareFn<State>[]): this;
use(path: string, ...middleware: MiddlewareFn<State>[]): this;
use(
pathOrMiddleware: string | MiddlewareFn<State>,
...middlewares: MiddlewareFn<State>[]
): this {
let path: string;
let fns: MiddlewareFn<State>[];
if (typeof pathOrMiddleware === "string") {
path = pathOrMiddleware;
fns = middlewares!;
} else {
path = "*";
middlewares.unshift(pathOrMiddleware);
fns = middlewares;
}
const segment = getOrCreateSegment(this.#root, path, false);
segment.middlewares.push(...fns);
return this;
}
notFound(routeOrMiddleware: Route<State> | MiddlewareFn<State>): this {
const route = typeof routeOrMiddleware === "function"
? { handler: routeOrMiddleware }
: routeOrMiddleware;
ensureHandler(route);
this.#root.notFound = (ctx) => renderRoute(ctx, route);
return this;
}
onError(
path: string,
routeOrMiddleware: Route<State> | MiddlewareFn<State>,
): this {
const segment = getOrCreateSegment(this.#root, path, true);
segment.errorRoute = typeof routeOrMiddleware === "function"
? { handler: routeOrMiddleware }
: routeOrMiddleware;
ensureHandler(segment.errorRoute);
return this;
}
appWrapper(component: RouteComponent<State>) {
const segment = getOrCreateSegment<State>(this.#root, "", false);
segment.app = component;
}
layout(
path: string,
component: RouteComponent<State>,
config?: LayoutConfig,
): this {
const segment = getOrCreateSegment<State>(this.#root, path, true);
segment.layout = { component, config: config ?? null };
return this;
}
route(path: string, route: Route<State>): this {
const segment = getOrCreateSegment<State>(this.#root, path, false);
const middlewares = segmentToMiddlewares(segment);
ensureHandler(route);
middlewares.push((ctx) => renderRoute(ctx, route));
const routePath = route.config?.routeOverride ?? path;
if (typeof route.handler === "function") {
this.#addRoute("ALL", routePath, middlewares);
} else if (isHandlerByMethod(route.handler!)) {
for (const method of Object.keys(route.handler)) {
this.#addRoute(method as Method, routePath, middlewares);
}
}
return this;
}
get(path: string, ...middlewares: MiddlewareFn<State>[]): this {
return this.#addRoutes("GET", path, middlewares);
this.#addMiddleware("GET", path, middlewares);
return this;
}
post(path: string, ...middlewares: MiddlewareFn<State>[]): this {
return this.#addRoutes("POST", path, middlewares);
this.#addMiddleware("POST", path, middlewares);
return this;
}
patch(path: string, ...middlewares: MiddlewareFn<State>[]): this {
return this.#addRoutes("PATCH", path, middlewares);
this.#addMiddleware("PATCH", path, middlewares);
return this;
}
put(path: string, ...middlewares: MiddlewareFn<State>[]): this {
return this.#addRoutes("PUT", path, middlewares);
this.#addMiddleware("PUT", path, middlewares);
return this;
}
delete(path: string, ...middlewares: MiddlewareFn<State>[]): this {
return this.#addRoutes("DELETE", path, middlewares);
this.#addMiddleware("DELETE", path, middlewares);
return this;
}
head(path: string, ...middlewares: MiddlewareFn<State>[]): this {
return this.#addRoutes("HEAD", path, middlewares);
this.#addMiddleware("HEAD", path, middlewares);
return this;
}
all(path: string, ...middlewares: MiddlewareFn<State>[]): this {
return this.#addRoutes("ALL", path, middlewares);
this.#addMiddleware("ALL", path, middlewares);
return this;
}
#addMiddleware(
method: Method | "ALL",
path: string,
fns: MiddlewareFn<State>[],
unshift = false,
) {
const segment = getOrCreateSegment<State>(this.#root, path, false);
const result = segmentToMiddlewares(segment);
result.push(...fns);
const resPath = mergePath(this.config.basePath, path);
this.#addRoute(method, resPath, result, unshift);
}
#addRoute(
method: Method | "ALL",
path: string,
fns: MiddlewareFn<State>[],
unshift = false,
) {
this.#routeDefs.push({ method, pattern: path, fns, unshift });
}
mountApp(path: string, app: App<State>): this {
const routes = app.#router._routes;
const segmentPath = mergePath(path, `/__${INTERNAL_ID++}/`);
const segment = getOrCreateSegment(this.#root, segmentPath, true);
const fns = segmentToMiddlewares(segment);
const routes = app.#routeDefs;
for (let i = 0; i < routes.length; i++) {
const route = routes[i];
const merged = mergePath(path, route.pattern);
const mergedFns = [...fns, ...route.fns];
this.#addRoute(route.method, merged, mergedFns, route.unshift);
}
app.#islandRegistry.forEach((value, key) => {
this.#islandRegistry.set(key, value);
});
const middlewares = app.#router._middlewares;
// Special case when user calls one of these:
// - `app.mountApp("/", otherApp)`
// - `app.mountApp("/*", otherApp)`
const isSelf = path === "/*" || path === "/";
if (isSelf && middlewares.length > 0) {
this.#router._middlewares.push(...middlewares);
}
for (let i = 0; i < routes.length; i++) {
const route = routes[i];
const merged = typeof route.path === "string"
? mergePaths(path, route.path)
: route.path;
const combined = isSelf
? route.handlers
: middlewares.concat(route.handlers);
this.#router.add(route.method, merged, combined);
}
app.#root.notFound = this.#root.notFound;
return this;
}
#addRoutes(
method: Method | "ALL",
pathname: string | URLPattern,
middlewares: MiddlewareFn<State>[],
): this {
const merged = typeof pathname === "string"
? mergePaths(this.config.basePath, pathname)
: pathname;
this.#router.add(method, merged, middlewares);
return this;
}
/**
* Create handler function for `Deno.serve` or to be used in
* testing.
*/
handler(): (
request: Request,
info?: Deno.ServeHandlerInfo,
@@ -263,6 +408,27 @@ export class App<State> {
return missingBuildHandler;
}
const staticMids: MiddlewareFn<State>[] = [];
if (this.#root.middlewares.length > 0) {
staticMids.push(...this.#root.middlewares);
}
staticMids.push(serveInternalStaticFiles());
this.#addMiddleware(
"ALL",
"*",
staticMids,
true,
);
for (let i = 0; i < this.#routeDefs.length; i++) {
const route = this.#routeDefs[i];
this.#router.add(route.method, route.pattern, route.fns, route.unshift);
}
const rootMiddlewares = this.#root.middlewares;
return async (
req: Request,
conn: Deno.ServeHandlerInfo = DEFAULT_CONN_INFO,
@@ -273,13 +439,32 @@ export class App<State> {
const method = req.method.toUpperCase() as Method;
const matched = this.#router.match(method, url);
let { params, pattern, handlers, methodMatch } = matched;
const next = matched.patternMatch && !matched.methodMatch
? DEFAULT_NOT_ALLOWED_METHOD
: DEFAULT_NOT_FOUND;
const span = trace.getActiveSpan();
if (span && pattern) {
span.updateName(`${method} ${pattern}`);
span.setAttribute("http.route", pattern);
}
const { params, handlers, pattern } = matched;
const ctx = new FreshReqContext<State>(
let next: () => Promise<Response>;
if (pattern === null || !methodMatch) {
handlers = rootMiddlewares;
}
if (matched.pattern !== null && !methodMatch) {
if (method === "OPTIONS") {
const allowed = this.#router.getAllowedMethods(matched.pattern);
next = defaultOptionsHandler(allowed);
} else {
next = DEFAULT_NOT_ALLOWED_METHOD;
}
} else {
next = DEFAULT_NOT_FOUND;
}
const ctx = new Context<State>(
req,
url,
conn,
@@ -290,19 +475,10 @@ export class App<State> {
this.#buildCache!,
);
const span = trace.getActiveSpan();
if (span && pattern) {
span.updateName(`${method} ${pattern}`);
span.setAttribute("http.route", pattern);
}
try {
let result: unknown;
if (handlers.length === 1 && handlers[0].length === 1) {
result = await handlers[0][0](ctx);
} else {
result = await runMiddlewares(handlers, ctx);
}
if (handlers.length === 0) return await next();
const result = await runMiddlewares(handlers, ctx);
if (!(result instanceof Response)) {
throw new Error(
`Expected a "Response" instance to be returned, but got: ${result}`,
@@ -311,17 +487,8 @@ export class App<State> {
return result;
} catch (err) {
if (err instanceof HttpError) {
if (err.status >= 500) {
// deno-lint-ignore no-console
console.error(err);
}
return new Response(err.message, { status: err.status });
}
// deno-lint-ignore no-console
console.error(err);
return new Response("Internal server error", { status: 500 });
ctx.error = err;
return await DEFAULT_ERROR_HANDLER(ctx);
}
};
}
@@ -331,7 +498,7 @@ export class App<State> {
options.onListen = createOnListen(this.config.basePath, options);
}
const handler = await this.handler();
const handler = this.handler();
if (options.port) {
await Deno.serve(options, handler);
return;
@@ -351,3 +518,15 @@ const missingBuildHandler = async (): Promise<Response> => {
: renderToString(h(ForgotBuild, null));
return new Response(html, { headers, status: 500 });
};
function ensureHandler<State>(route: Route<State>) {
if (route.handler === undefined) {
route.handler = route.component !== undefined
? DEFAULT_RENDER
: DEFAULT_NOT_FOUND;
} else if (isHandlerByMethod(route.handler)) {
if (route.component !== undefined && !route.handler.GET) {
route.handler.GET = DEFAULT_RENDER;
}
}
}