feat: add support for route groups (#1563)

This commit is contained in:
Marvin Hagemeister
2023-08-02 09:51:20 +02:00
committed by GitHub
parent 4b28e2e269
commit e4d797ae4e
17 changed files with 384 additions and 218 deletions
+4 -26
View File
@@ -15,7 +15,6 @@ import {
ErrorPage,
Island,
LayoutModule,
LayoutRoute,
Plugin,
PluginRenderFunctionResult,
PluginRenderResult,
@@ -58,7 +57,7 @@ export interface RenderOptions<Data> {
islands: Island[];
plugins: Plugin[];
app: AppModule;
layouts: LayoutRoute[];
layouts: LayoutModule[];
imports: string[];
dependenciesFn: (path: string) => string[];
url: URL;
@@ -138,25 +137,6 @@ function defaultCsp() {
};
}
/**
* Return a list of layouts that needs to be applied for request url
* @param url the request url
* @param layouts Array of layouts handlers and their routes as path-to-regexp style
*/
export function selectLayouts(url: string, layouts: LayoutRoute[]) {
const selectedLayouts: LayoutModule[] = [];
const reqURL = new URL(url);
for (const layout of layouts) {
const res = layout.compiledPattern.exec(reqURL);
if (res) {
selectedLayouts.push(layout);
}
}
return selectedLayouts;
}
/**
* This function renders out a page. Rendering is synchronous and non streaming.
* Suspense boundaries are not supported.
@@ -214,13 +194,11 @@ export async function render<Data>(
// deno-lint-ignore no-explicit-any
let finalAppComp: VNode<any> = vnode as any;
const layouts = selectLayouts(opts.url.toString(), opts.layouts);
let i = layouts.length;
let i = opts.layouts.length;
while (i--) {
const layout = layouts[i];
const curComp = finalAppComp;
const layout = opts.layouts[i];
const curComp = finalAppComp;
finalAppComp = h(layout.default, {
params: opts.params as Record<string, string>,
url: opts.url,