Skip to content

Commit

Permalink
allow to pass extra data to on_demand pages
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Apr 28, 2023
1 parent 5c7a8aa commit 4d2df49
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 9 additions & 1 deletion core/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,10 @@ export default class Site {
}

/** Render a single page (used for on demand rendering) */
async renderPage(file: string): Promise<Page | undefined> {
async renderPage(
file: string,
extraData?: Record<string, unknown>,
): Promise<Page | undefined> {
// Load the page
this.fs.init();

Expand All @@ -672,6 +675,11 @@ export default class Site {
return;
}

// Add extra data
if (extraData) {
page.data = { ...page.data, ...extraData };
}

await this.dispatchEvent({ type: "beforeRenderOnDemand", page });

// Render the page
Expand Down
4 changes: 3 additions & 1 deletion middlewares/on_demand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Router = (url: URL) => string | undefined;
export interface Options {
site: Site;
router?: Router;
extraData?: (request: Request) => Record<string, unknown>;
}

/** Render pages on demand */
Expand All @@ -33,7 +34,8 @@ export default function onDemand(options: Options): Middleware {
return response;
}

const page = await site.renderPage(file);
const data = options.extraData?.(request) ?? {};
const page = await site.renderPage(file, data);

if (!page || !page.outputPath) {
return response;
Expand Down

0 comments on commit 4d2df49

Please sign in to comment.