diff --git a/website/loaders/pages.ts b/website/loaders/pages.ts index 23465c2ef..347fff625 100644 --- a/website/loaders/pages.ts +++ b/website/loaders/pages.ts @@ -1,3 +1,4 @@ +import { SiteRoute } from "../../admin/widgets.ts"; import { Route } from "../flags/audience.ts"; import { AppContext } from "../mod.ts"; import Page from "../pages/Page.tsx"; @@ -33,12 +34,25 @@ async function getAllPages(ctx: AppContext): Promise { return routes; } + +export interface Props { + /** + * @title Hide pages in deco + * @description Don't route the client to any deco page. Important: those page are still accessible if you set the "rdc=true" query string. + */ + hidePagesInDeco?: boolean; + /** + * @description Deco routes that will ignore the previous rule. If the same route exists on other routes loader, the deco page will be used. + */ + alwaysVisiblePages?: SiteRoute[]; +} + /** * @title Pages */ export default async function Pages( - _props: unknown, - _req: Request, + props: Props, + req: Request, ctx: AppContext, ): Promise { const allPages = await ctx.get< @@ -48,5 +62,23 @@ export default async function Pages( __resolveType: "once", }); + if (props?.hidePagesInDeco) { + return allPages.map(({ pathTemplate, ...pageProps }: Route) => { + const isException = props.alwaysVisiblePages?.some((path) => + path === pathTemplate + ); + const url = new URL(pathTemplate, req.url); + const queryString = new URLSearchParams(url.search).toString(); + const separator = queryString ? "&" : ""; + + return ({ + pathTemplate: isException + ? pathTemplate + : `${pathTemplate}?${queryString}${separator}*rdc=true*`, + ...pageProps, + }); + }); + } + return allPages; }