Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

priorization pages #882

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 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,29 @@ async function getAllPages(ctx: AppContext): Promise<Route[]> {

return routes;
}

export interface ExternalProps {
/**
* @title Prioritize External Routes
* @description If there is the same route on the deco and externally, as in proxies, the EXTERNAL route will be used. You can also test with the parameter "rdc=true"
*/
preferRoutes?: boolean;
/**
* @description Deco routes that will ignore the previous rule. If the same route exists on the deco and externally, the DECO route will be used
*/
exceptionRoutes?: SiteRoute[];
}

export interface Props {
external?: ExternalProps;
}

/**
* @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 +66,23 @@ export default async function Pages(
__resolveType: "once",
});

if (props?.external?.preferRoutes) {
return allPages.map(({ pathTemplate, ...pageProps }: Route) => {
const isException = props.external?.exceptionRoutes?.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;
}
Loading