From d2515a9e39ffb7b3ce58040849c03b2f367325f4 Mon Sep 17 00:00:00 2001 From: Gabriel Matos Boubee <108771428+gabrielMatosBoubee@users.noreply.github.com> Date: Thu, 23 Jan 2025 17:19:40 -0300 Subject: [PATCH] priorization pages (#882) * feat: priorization pages * chore: refactor * fix: add wildcard * chore: change names --- website/loaders/pages.ts | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) 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; }