feat: middleware can call renderNotFound (#1975)
This commit is contained in:
+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 {
|
||||
|
||||
Reference in New Issue
Block a user