feat: middleware can call renderNotFound (#1975)
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
MiddlewareRoute,
|
||||
RouterState,
|
||||
ServeHandlerInfo,
|
||||
UnknownRenderFunction,
|
||||
} from "./types.ts";
|
||||
|
||||
export const ROOT_BASE_ROUTE = toBaseRoute("/");
|
||||
@@ -56,6 +57,7 @@ export function composeMiddlewares(
|
||||
paramsAndRoute: (
|
||||
url: string,
|
||||
) => RouteResult<RouterState>,
|
||||
renderNotFound: UnknownRenderFunction,
|
||||
) {
|
||||
return (
|
||||
req: Request,
|
||||
@@ -100,6 +102,9 @@ export function composeMiddlewares(
|
||||
},
|
||||
destination: "route",
|
||||
params: paramsAndRouteResult.params,
|
||||
renderNotFound: async () => {
|
||||
return await renderNotFound(req, paramsAndRouteResult.params, ctx);
|
||||
},
|
||||
isPartial: paramsAndRouteResult.isPartial,
|
||||
};
|
||||
|
||||
|
||||
+75
-55
@@ -16,6 +16,7 @@ import {
|
||||
RouterState,
|
||||
ServeHandlerInfo,
|
||||
UnknownPage,
|
||||
UnknownRenderFunction,
|
||||
} from "./types.ts";
|
||||
import { render as internalRender } from "./render.ts";
|
||||
import {
|
||||
@@ -130,12 +131,20 @@ export class ServerContext {
|
||||
* by Fresh, including static files.
|
||||
*/
|
||||
handler(): (req: Request, connInfo?: ServeHandlerInfo) => Promise<Response> {
|
||||
const handlers = this.#handlers();
|
||||
const renderNotFound = createRenderNotFound(
|
||||
this.#extractResult,
|
||||
this.#dev,
|
||||
this.#plugins,
|
||||
this.#renderFn,
|
||||
this.#maybeBuildSnapshot(),
|
||||
);
|
||||
const handlers = this.#handlers(renderNotFound);
|
||||
const inner = router.router<RouterState>(handlers);
|
||||
const withMiddlewares = composeMiddlewares(
|
||||
this.#extractResult.middlewares,
|
||||
handlers.errorHandler,
|
||||
router.getParamsAndRoute<RouterState>(handlers),
|
||||
renderNotFound,
|
||||
);
|
||||
const trailingSlashEnabled = this.#state.config.router?.trailingSlash;
|
||||
const isDev = this.#dev;
|
||||
@@ -226,7 +235,9 @@ export class ServerContext {
|
||||
* This function returns all routes required by Fresh as an extended
|
||||
* path-to-regex, to handler mapping.
|
||||
*/
|
||||
#handlers(): {
|
||||
#handlers(
|
||||
renderNotFound: UnknownRenderFunction,
|
||||
): {
|
||||
internalRoutes: router.Routes<RouterState>;
|
||||
staticRoutes: router.Routes<RouterState>;
|
||||
routes: router.Routes<RouterState>;
|
||||
@@ -283,59 +294,6 @@ export class ServerContext {
|
||||
return snapshot?.dependencies(path) ?? [];
|
||||
};
|
||||
|
||||
const renderNotFound = async <Data = undefined>(
|
||||
req: Request,
|
||||
params: Record<string, string>,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
ctx?: any,
|
||||
data?: Data,
|
||||
error?: unknown,
|
||||
) => {
|
||||
const notFound = this.#extractResult.notFound;
|
||||
if (!notFound.component) {
|
||||
return sendResponse(["Not found.", undefined], {
|
||||
status: Status.NotFound,
|
||||
isDev: this.#dev,
|
||||
statusText: undefined,
|
||||
headers: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
const layouts = selectSharedRoutes(
|
||||
ROOT_BASE_ROUTE,
|
||||
this.#extractResult.layouts,
|
||||
);
|
||||
|
||||
const imports: string[] = [];
|
||||
const resp = await internalRender({
|
||||
request: req,
|
||||
context: ctx,
|
||||
route: notFound,
|
||||
plugins: this.#plugins,
|
||||
app: this.#extractResult.app,
|
||||
layouts,
|
||||
imports,
|
||||
dependenciesFn,
|
||||
renderFn: this.#renderFn,
|
||||
url: new URL(req.url),
|
||||
params,
|
||||
data,
|
||||
state: ctx?.state,
|
||||
error,
|
||||
});
|
||||
|
||||
if (resp instanceof Response) {
|
||||
return resp;
|
||||
}
|
||||
|
||||
return sendResponse(resp, {
|
||||
status: Status.NotFound,
|
||||
isDev: this.#dev,
|
||||
statusText: undefined,
|
||||
headers: undefined,
|
||||
});
|
||||
};
|
||||
|
||||
const genRender = <Data = undefined>(
|
||||
route: Route<Data> | UnknownPage | ErrorPage,
|
||||
status: number,
|
||||
@@ -558,6 +516,68 @@ export class ServerContext {
|
||||
};
|
||||
}
|
||||
|
||||
const createRenderNotFound = (
|
||||
extractResult: FsExtractResult,
|
||||
dev: boolean,
|
||||
plugins: Plugin<Record<string, unknown>>[],
|
||||
renderFunction: RenderFunction,
|
||||
buildSnapshot: BuildSnapshot | null,
|
||||
) => {
|
||||
const dependenciesFn = (path: string) => {
|
||||
const snapshot = buildSnapshot;
|
||||
return snapshot?.dependencies(path) ?? [];
|
||||
};
|
||||
|
||||
return async (
|
||||
...args: Parameters<UnknownRenderFunction>
|
||||
) => {
|
||||
const [req, params, ctx, data, error] = args;
|
||||
const notFound = extractResult.notFound;
|
||||
if (!notFound.component) {
|
||||
return sendResponse(["Not found.", undefined], {
|
||||
status: Status.NotFound,
|
||||
isDev: dev,
|
||||
statusText: undefined,
|
||||
headers: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
const layouts = selectSharedRoutes(
|
||||
ROOT_BASE_ROUTE,
|
||||
extractResult.layouts,
|
||||
);
|
||||
|
||||
const imports: string[] = [];
|
||||
const resp = await internalRender({
|
||||
request: req,
|
||||
context: ctx,
|
||||
route: notFound,
|
||||
plugins: plugins,
|
||||
app: extractResult.app,
|
||||
layouts,
|
||||
imports,
|
||||
dependenciesFn,
|
||||
renderFn: renderFunction,
|
||||
url: new URL(req.url),
|
||||
params,
|
||||
data,
|
||||
state: ctx?.state,
|
||||
error,
|
||||
});
|
||||
|
||||
if (resp instanceof Response) {
|
||||
return resp;
|
||||
}
|
||||
|
||||
return sendResponse(resp, {
|
||||
status: Status.NotFound,
|
||||
isDev: dev,
|
||||
statusText: undefined,
|
||||
headers: undefined,
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
// Normalize a path for use in a URL. Returns null if the path is unparsable.
|
||||
export function normalizeURLPath(path: string): string | null {
|
||||
try {
|
||||
|
||||
@@ -433,6 +433,15 @@ export interface UnknownPage {
|
||||
inheritLayouts: boolean;
|
||||
}
|
||||
|
||||
export type UnknownRenderFunction = (
|
||||
req: Request,
|
||||
params: Record<string, string>,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
ctx?: any,
|
||||
data?: unknown,
|
||||
error?: unknown,
|
||||
) => Promise<Response>;
|
||||
|
||||
// --- ERROR PAGE ---
|
||||
|
||||
export interface ErrorPageProps {
|
||||
@@ -488,6 +497,7 @@ export interface ErrorPage {
|
||||
export interface MiddlewareHandlerContext<State = Record<string, unknown>>
|
||||
extends ServeHandlerInfo {
|
||||
next: () => Promise<Response>;
|
||||
renderNotFound: (state?: State) => Response | Promise<Response>;
|
||||
state: State;
|
||||
destination: router.DestinationKind;
|
||||
params: Record<string, string>;
|
||||
|
||||
Reference in New Issue
Block a user