Skip to content

Commit

Permalink
priorization pages (#882)
Browse files Browse the repository at this point in the history
* feat: priorization pages

* chore: refactor

* fix: add wildcard

* chore: change names
  • Loading branch information
gabrielMatosBoubee authored Jan 23, 2025
1 parent 0246e5e commit d2515a9
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions website/loaders/pages.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -33,12 +34,25 @@ async function getAllPages(ctx: AppContext): Promise<Route[]> {

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<Route[]> {
const allPages = await ctx.get<
Expand All @@ -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;
}

0 comments on commit d2515a9

Please sign in to comment.