Skip to content

Commit

Permalink
improved on_demand
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed May 4, 2023
1 parent e6b6723 commit 71006d0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
15 changes: 12 additions & 3 deletions middlewares/on_demand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ export type Router = (url: URL) => string | undefined;
export interface Options {
site: Site;
router?: Router;
extraData?: (request: Request) => Record<string, unknown>;
}

/** Render pages on demand */
export default function onDemand(options: Options): Middleware {
const site = options.site;
let router = options.router;

const { routesFile, extraData } =
(site._data.on_demand || {}) as MiddlewareOptions;

return async (request, next) => {
const response = await next(request);

Expand All @@ -24,7 +26,9 @@ export default function onDemand(options: Options): Middleware {
}

if (!router) {
router = await createDefaultRouter(site.src("_routes.json"));
router = await createDefaultRouter(
routesFile || site.root("/_routes.json"),
);
}

const url = new URL(request.url);
Expand All @@ -34,7 +38,7 @@ export default function onDemand(options: Options): Middleware {
return response;
}

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

if (!page || !page.outputPath) {
Expand Down Expand Up @@ -87,3 +91,8 @@ export function getRouter(routes: Map<string, string>): Router {
return path;
};
}

export interface MiddlewareOptions {
extraData?: (request: Request) => Record<string, unknown>;
routesFile?: string;
}
4 changes: 3 additions & 1 deletion plugins/imagick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ function transform(
(content: Uint8Array) => page.content = new Uint8Array(content),
);
} else {
image.write((content: Uint8Array) => page.content = new Uint8Array(content));
image.write((content: Uint8Array) =>
page.content = new Uint8Array(content)
);
}
});
}
Expand Down
14 changes: 13 additions & 1 deletion plugins/on_demand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { merge } from "../core/utils.ts";
import onDemand, { getRouter } from "../middlewares/on_demand.ts";
import onDemand, {
getRouter,
MiddlewareOptions,
} from "../middlewares/on_demand.ts";
import { extname } from "../deps/path.ts";

import type { Logger, Page, Site } from "../core.ts";
Expand All @@ -10,6 +13,9 @@ export interface Options {

/** The file path to save the preloaded modules */
preloadPath: string;

/** Extra data to pass to the pages */
extraData?: (request: Request) => Record<string, unknown>;
}

// Default options
Expand Down Expand Up @@ -39,6 +45,12 @@ export default function (userOptions?: Partial<Options>) {

// Add the ondemand middleware
site.options.server.middlewares ||= [];

site._data.on_demand = {
extraData: options.extraData,
routesFile,
} as MiddlewareOptions;

site.options.server.middlewares.push(onDemand({
site,
router: getRouter(collector.routes),
Expand Down

0 comments on commit 71006d0

Please sign in to comment.