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
+119 -112
View File
@@ -1,22 +1,26 @@
import {
type AnyComponent,
type ComponentType,
Fragment,
type FunctionComponent,
h,
isValidElement,
type VNode,
} from "preact";
import { renderToString } from "preact-render-to-string";
import { SpanStatusCode } from "@opentelemetry/api";
import type { ResolvedFreshConfig } from "./config.ts";
import type { BuildCache } from "./build_cache.ts";
import {
FreshScripts,
RenderState,
setRenderState,
} from "./runtime/server/preact_hooks.tsx";
import { DEV_ERROR_OVERLAY_URL, PARTIAL_SEARCH_PARAM } from "./constants.ts";
import { BUILD_ID } from "./runtime/build_id.ts";
import { RenderState, setRenderState } from "./runtime/server/preact_hooks.tsx";
import { PARTIAL_SEARCH_PARAM } from "./constants.ts";
import { tracer } from "./otel.ts";
import {
type ComponentDef,
isAsyncAnyComponent,
type PageProps,
preactRender,
renderAsyncAnyComponent,
renderRouteComponent,
} from "./render.ts";
export interface Island {
file: string | URL;
@@ -27,24 +31,44 @@ export interface Island {
export type ServerIslandRegistry = Map<ComponentType, Island>;
export const internals: unique symbol = Symbol("fresh_internal");
export interface UiTree<Data, State> {
app: AnyComponent<PageProps<Data, State>> | null;
layouts: ComponentDef<Data, State>[];
}
/**
* @deprecated Use {@linkcode Context} instead.
*/
export type FreshContext<State = unknown> = Context<State>;
export let getBuildCache: <T>(ctx: Context<T>) => BuildCache;
export let getInternals: <T>(ctx: Context<T>) => UiTree<unknown, T>;
/**
* The context passed to every middleware. It is unique for every request.
*/
export interface FreshContext<State = unknown> {
export class Context<State> {
#internal: UiTree<unknown, State> = {
app: null,
layouts: [],
};
/** Reference to the resolved Fresh configuration */
readonly config: ResolvedFreshConfig;
readonly state: State;
/** The original incoming `Request` object` */
readonly req: Request;
/**
* The request url parsed into an `URL` instance. This is typically used
* to apply logic based on the pathname of the incoming url or when
* certain search parameters are set.
*/
readonly url: URL;
/** The original incoming `Request` object` */
readonly req: Request;
readonly params: Record<string, string>;
readonly error: unknown;
readonly info: Deno.ServeHandlerInfo;
readonly state: State = {} as State;
data: unknown = undefined;
error: unknown | null = null;
readonly info: Deno.ServeHandlerInfo | Deno.ServeHandlerInfo;
/**
* Whether the current Request is a partial request.
*
@@ -53,20 +77,7 @@ export interface FreshContext<State = unknown> {
* be used to determine if only `<Partial>`'s need to be rendered.
*/
readonly isPartial: boolean;
/**
* Return a redirect response to the specified path. This is the
* preferred way to do redirects in Fresh.
*
* ```ts
* ctx.redirect("/foo/bar") // redirect user to "<yoursite>/foo/bar"
*
* // Disallows protocol relative URLs for improved security. This
* // redirects the user to `<yoursite>/evil.com` which is safe,
* // instead of redirecting to `http://evil.com`.
* ctx.redirect("//evil.com/");
* ```
*/
redirect(path: string, status?: number): Response;
/**
* Call the next middleware.
* ```ts
@@ -90,25 +101,7 @@ export interface FreshContext<State = unknown> {
* return res
* }
*/
next(): Promise<Response>;
render(vnode: VNode, init?: ResponseInit): Response | Promise<Response>;
}
export let getBuildCache: (ctx: FreshContext<unknown>) => BuildCache;
export class FreshReqContext<State>
implements FreshContext<State>, PageProps<unknown, State> {
readonly config: ResolvedFreshConfig;
readonly url: URL;
readonly req: Request;
readonly params: Record<string, string>;
readonly state: State = {} as State;
data: unknown = undefined;
error: unknown | null = null;
readonly info: Deno.ServeHandlerInfo | Deno.ServeHandlerInfo;
readonly isPartial: boolean;
next: FreshContext<State>["next"];
next: () => Promise<Response>;
#islandRegistry: ServerIslandRegistry;
#buildCache: BuildCache;
@@ -117,7 +110,9 @@ export class FreshReqContext<State>
Component!: FunctionComponent;
static {
getBuildCache = (ctx) => (ctx as FreshReqContext<unknown>).#buildCache;
// deno-lint-ignore no-explicit-any
getInternals = (ctx) => (ctx as Context<unknown>).#internal as any;
getBuildCache = (ctx) => (ctx as Context<unknown>).#buildCache;
}
constructor(
@@ -126,7 +121,7 @@ export class FreshReqContext<State>
info: Deno.ServeHandlerInfo,
params: Record<string, string>,
config: ResolvedFreshConfig,
next: FreshContext<State>["next"],
next: () => Promise<Response>,
islandRegistry: ServerIslandRegistry,
buildCache: BuildCache,
) {
@@ -141,6 +136,19 @@ export class FreshReqContext<State>
this.#buildCache = buildCache;
}
/**
* Return a redirect response to the specified path. This is the
* preferred way to do redirects in Fresh.
*
* ```ts
* ctx.redirect("/foo/bar") // redirect user to "<yoursite>/foo/bar"
*
* // Disallows protocol relative URLs for improved security. This
* // redirects the user to `<yoursite>/evil.com` which is safe,
* // instead of redirecting to `http://evil.com`.
* ctx.redirect("//evil.com/");
* ```
*/
redirect(pathOrUrl: string, status = 302): Response {
let location = pathOrUrl;
@@ -166,17 +174,61 @@ export class FreshReqContext<State>
});
}
render(
async render(
// deno-lint-ignore no-explicit-any
vnode: VNode<any>,
vnode: VNode<any> | null,
init: ResponseInit | undefined = {},
): Response | Promise<Response> {
): Promise<Response> {
if (arguments.length === 0) {
throw new Error(`No arguments passed to: ctx.render()`);
} else if (vnode !== null && !isValidElement(vnode)) {
throw new Error(`Non-JSX element passed to: ctx.render()`);
}
const defs = this.#internal.layouts;
const appDef = this.#internal.app;
const props = this as Context<State>;
// Compose final vnode tree
for (let i = defs.length - 1; i >= 0; i--) {
const child = vnode;
props.Component = () => child;
const def = defs[i];
const result = await renderRouteComponent(this, def, () => child);
if (result instanceof Response) {
return result;
}
vnode = result;
}
if (isAsyncAnyComponent(appDef)) {
const child = vnode;
props.Component = () => child;
const result = await renderAsyncAnyComponent(appDef, props);
if (result instanceof Response) {
return result;
}
vnode = result;
} else if (appDef !== null) {
const child = vnode;
vnode = h(appDef, {
Component: () => child,
config: this.config,
data: null,
error: this.error,
info: this.info,
isPartial: this.isPartial,
params: this.params,
req: this.req,
state: this.state,
url: this.url,
});
}
const headers = init.headers !== undefined
? init.headers instanceof Headers
? init.headers
@@ -198,13 +250,21 @@ export class FreshReqContext<State>
const html = tracer.startActiveSpan("render", (span) => {
span.setAttribute("fresh.span_type", "render");
const state = new RenderState(
this,
this.#islandRegistry,
this.#buildCache,
partialId,
);
try {
setRenderState(state);
return preactRender(
vnode,
vnode ?? h(Fragment, null),
this,
this.#islandRegistry,
state,
this.#buildCache,
partialId,
headers,
);
} catch (err) {
@@ -218,6 +278,9 @@ export class FreshReqContext<State>
}
throw err;
} finally {
state.clear();
setRenderState(null);
span.end();
}
});
@@ -225,7 +288,7 @@ export class FreshReqContext<State>
}
}
Object.defineProperties(FreshReqContext.prototype, {
Object.defineProperties(Context.prototype, {
config: { enumerable: true },
url: { enumerable: true },
req: { enumerable: true },
@@ -236,59 +299,3 @@ Object.defineProperties(FreshReqContext.prototype, {
next: { enumerable: true },
info: { enumerable: true },
});
function preactRender<State, Data>(
vnode: VNode,
ctx: PageProps<Data, State>,
islandRegistry: ServerIslandRegistry,
buildCache: BuildCache,
partialId: string,
headers: Headers,
) {
const state = new RenderState(ctx, islandRegistry, buildCache, partialId);
setRenderState(state);
try {
let res = renderToString(vnode);
// We require a the full outer DOM structure so that browser put
// comment markers in the right place in the DOM.
if (!state.renderedHtmlBody) {
let scripts = "";
if (ctx.url.pathname !== ctx.config.basePath + DEV_ERROR_OVERLAY_URL) {
scripts = renderToString(h(FreshScripts, null));
}
res = `<body>${res}${scripts}</body>`;
}
if (!state.renderedHtmlHead) {
res = `<head><meta charset="utf-8"></head>${res}`;
}
if (!state.renderedHtmlTag) {
res = `<html>${res}</html>`;
}
return `<!DOCTYPE html>${res}`;
} finally {
// Add preload headers
const basePath = ctx.config.basePath;
const runtimeUrl = `${basePath}/_fresh/js/${BUILD_ID}/fresh-runtime.js`;
let link = `<${encodeURI(runtimeUrl)}>; rel="modulepreload"; as="script"`;
state.islands.forEach((island) => {
const chunk = buildCache.getIslandChunkName(island.name);
if (chunk !== null) {
link += `, <${
encodeURI(`${basePath}${chunk}`)
}>; rel="modulepreload"; as="script"`;
}
});
if (link !== "") {
headers.append("Link", link);
}
state.clear();
setRenderState(null);
}
}
export type PageProps<Data = unknown, T = unknown> =
& Omit<FreshContext<T>, "next" | "render" | "redirect">
& { data: Data; Component: FunctionComponent };