feat: allow passing additional args to HandlerContext.render (#82)
Co-authored-by: Luca Casonato <lucacasonato@yahoo.com>
This commit is contained in:
co-authored by
Luca Casonato
parent
6abf62c832
commit
788a85c431
@@ -1,4 +1,4 @@
|
|||||||
export interface PageProps {
|
export interface PageProps<T extends Record<string, unknown>> {
|
||||||
/** The URL of the request that resulted in this page being rendered. */
|
/** The URL of the request that resulted in this page being rendered. */
|
||||||
url: URL;
|
url: URL;
|
||||||
|
|
||||||
@@ -15,6 +15,12 @@ export interface PageProps {
|
|||||||
* be `{ path: [ 'bar', 'baz' ] }`.
|
* be `{ path: [ 'bar', 'baz' ] }`.
|
||||||
*/
|
*/
|
||||||
params: Record<string, string | string[]>;
|
params: Record<string, string | string[]>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Additional data passed into `HandlerContext.render`. Not present by
|
||||||
|
* default.
|
||||||
|
*/
|
||||||
|
renderData?: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PageConfig {
|
export interface PageConfig {
|
||||||
|
|||||||
@@ -203,7 +203,7 @@ export class ServerContext {
|
|||||||
params: Record<string, string | string[]>,
|
params: Record<string, string | string[]>,
|
||||||
) => {
|
) => {
|
||||||
if (page.component === undefined) return undefined;
|
if (page.component === undefined) return undefined;
|
||||||
const render = async () => {
|
const render = async (renderData?: Record<string, unknown>) => {
|
||||||
const preloads = page.runtimeJS
|
const preloads = page.runtimeJS
|
||||||
? this.#bundler.getPreloads(bundlePath).map(bundleAssetUrl)
|
? this.#bundler.getPreloads(bundlePath).map(bundleAssetUrl)
|
||||||
: [];
|
: [];
|
||||||
@@ -214,6 +214,7 @@ export class ServerContext {
|
|||||||
renderer: this.#renderer,
|
renderer: this.#renderer,
|
||||||
url: new URL(req.url),
|
url: new URL(req.url),
|
||||||
params,
|
params,
|
||||||
|
renderData,
|
||||||
});
|
});
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
"content-type": "text/html; charset=utf-8",
|
"content-type": "text/html; charset=utf-8",
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ export interface RenderOptions {
|
|||||||
url: URL;
|
url: URL;
|
||||||
params: Record<string, string | string[]>;
|
params: Record<string, string | string[]>;
|
||||||
renderer: Renderer;
|
renderer: Renderer;
|
||||||
|
renderData?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RenderFn = () => void;
|
export type RenderFn = () => void;
|
||||||
@@ -114,7 +115,12 @@ function defaultCsp() {
|
|||||||
export async function* render(
|
export async function* render(
|
||||||
opts: RenderOptions,
|
opts: RenderOptions,
|
||||||
): AsyncIterable<string | [string, ContentSecurityPolicy | undefined]> {
|
): AsyncIterable<string | [string, ContentSecurityPolicy | undefined]> {
|
||||||
const props = { params: opts.params, url: opts.url, route: opts.page.route };
|
const props = {
|
||||||
|
params: opts.params,
|
||||||
|
url: opts.url,
|
||||||
|
route: opts.page.route,
|
||||||
|
renderData: opts.renderData,
|
||||||
|
};
|
||||||
|
|
||||||
const csp: ContentSecurityPolicy | undefined = opts.page.csp
|
const csp: ContentSecurityPolicy | undefined = opts.page.csp
|
||||||
? defaultCsp()
|
? defaultCsp()
|
||||||
|
|||||||
+1
-1
@@ -12,7 +12,7 @@ export interface PageModule {
|
|||||||
export interface HandlerContext {
|
export interface HandlerContext {
|
||||||
req: Request;
|
req: Request;
|
||||||
match: Record<string, string>;
|
match: Record<string, string>;
|
||||||
render?: () => Promise<Response>;
|
render?: (args?: Record<string, unknown>) => Promise<Response>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Handler = (ctx: HandlerContext) => Response | Promise<Response>;
|
export type Handler = (ctx: HandlerContext) => Response | Promise<Response>;
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import { h } from "../deps.ts";
|
||||||
|
import { HandlerContext } from "../../../server.ts";
|
||||||
|
|
||||||
|
interface renderData extends Record<string, unknown> {
|
||||||
|
info: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Page({
|
||||||
|
renderData,
|
||||||
|
}: {
|
||||||
|
renderData: renderData;
|
||||||
|
}) {
|
||||||
|
return <div>{renderData.info}</div>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const handler = {
|
||||||
|
GET({ req, render }: HandlerContext) {
|
||||||
|
if (req.headers.get("accept")?.includes("text/html")) {
|
||||||
|
return render!({
|
||||||
|
info: "intercepted",
|
||||||
|
} as renderData);
|
||||||
|
} else {
|
||||||
|
return new Response("This is plain text");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
POST() {
|
||||||
|
return new Response("POST response");
|
||||||
|
},
|
||||||
|
};
|
||||||
+16
-14
@@ -2,25 +2,27 @@
|
|||||||
// This file SHOULD be checked into source version control.
|
// This file SHOULD be checked into source version control.
|
||||||
// To update this file, run `fresh routes`.
|
// To update this file, run `fresh routes`.
|
||||||
|
|
||||||
import * as $0 from "./pages/_middleware.ts";
|
import * as $0 from "./pages/index.tsx";
|
||||||
import * as $1 from "./pages/api/get_only.ts";
|
import * as $1 from "./pages/static.tsx";
|
||||||
import * as $2 from "./pages/books/[id].tsx";
|
import * as $2 from "./pages/intercept_args.tsx";
|
||||||
import * as $3 from "./pages/props/[id].tsx";
|
import * as $3 from "./pages/props/[id].tsx";
|
||||||
import * as $4 from "./pages/static.tsx";
|
import * as $4 from "./pages/_middleware.ts";
|
||||||
import * as $5 from "./pages/intercept.tsx";
|
import * as $5 from "./pages/books/[id].tsx";
|
||||||
import * as $6 from "./pages/[name].tsx";
|
import * as $6 from "./pages/api/get_only.ts";
|
||||||
import * as $7 from "./pages/index.tsx";
|
import * as $7 from "./pages/intercept.tsx";
|
||||||
|
import * as $8 from "./pages/[name].tsx";
|
||||||
|
|
||||||
const routes = {
|
const routes = {
|
||||||
pages: {
|
pages: {
|
||||||
"./pages/_middleware.ts": $0,
|
"./pages/index.tsx": $0,
|
||||||
"./pages/api/get_only.ts": $1,
|
"./pages/static.tsx": $1,
|
||||||
"./pages/books/[id].tsx": $2,
|
"./pages/intercept_args.tsx": $2,
|
||||||
"./pages/props/[id].tsx": $3,
|
"./pages/props/[id].tsx": $3,
|
||||||
"./pages/static.tsx": $4,
|
"./pages/_middleware.ts": $4,
|
||||||
"./pages/intercept.tsx": $5,
|
"./pages/books/[id].tsx": $5,
|
||||||
"./pages/[name].tsx": $6,
|
"./pages/api/get_only.ts": $6,
|
||||||
"./pages/index.tsx": $7,
|
"./pages/intercept.tsx": $7,
|
||||||
|
"./pages/[name].tsx": $8,
|
||||||
},
|
},
|
||||||
baseUrl: import.meta.url,
|
baseUrl: import.meta.url,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -98,6 +98,17 @@ Deno.test("/intercept - DELETE", async () => {
|
|||||||
assertEquals(resp.status, 405);
|
assertEquals(resp.status, 405);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("/intercept_args - GET html", async () => {
|
||||||
|
const req = new Request("https://fresh.deno.dev/intercept_args", {
|
||||||
|
headers: { "accept": "text/html" },
|
||||||
|
});
|
||||||
|
const resp = await router(req);
|
||||||
|
assert(resp);
|
||||||
|
assertEquals(resp.status, 200);
|
||||||
|
const body = await resp.text();
|
||||||
|
assert(body.includes("<div>intercepted</div>"));
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test("/api/get_only - NOTAMETHOD", async () => {
|
Deno.test("/api/get_only - NOTAMETHOD", async () => {
|
||||||
const resp = await router(
|
const resp = await router(
|
||||||
new Request("https://fresh.deno.dev/api/get_only", {
|
new Request("https://fresh.deno.dev/api/get_only", {
|
||||||
|
|||||||
Reference in New Issue
Block a user