feat: allow layout and _app overrides (#1611)

This commit is contained in:
Marvin Hagemeister
2023-08-09 16:35:53 +00:00
committed by GitHub
parent 81ae7b8427
commit 7ebaf0404d
13 changed files with 277 additions and 20 deletions
+35 -13
View File
@@ -154,7 +154,24 @@ export async function render<Data>(
opts: RenderOptions<Data>,
): Promise<[string, ContentSecurityPolicy | undefined] | Response> {
const component = opts.route.component;
const layouts = opts.layouts;
// Only inherit layouts up to the nearest root layout.
// Note that the route itself can act as the root layout.
let layouts = opts.layouts;
if (!opts.route.rootLayout) {
let rootIdx = 0;
let layoutIdx = opts.layouts.length;
while (layoutIdx--) {
if (opts.layouts[layoutIdx].config?.rootLayout) {
rootIdx = layoutIdx;
break;
}
}
layouts = opts.layouts.slice(rootIdx);
} else {
layouts = [];
}
const isAsyncComponent = checkAsyncComponent(component);
const props: Record<string, unknown> = {
@@ -204,20 +221,25 @@ export async function render<Data>(
const syncPlugins = opts.plugins.filter((p) => p.render);
function buildRoot(appComponent: VNode) {
// Only use the _app layout if not explicitly disabled
const child = opts.route.appLayout
? h(opts.app.default, {
params: opts.params as Record<string, string>,
url: opts.url,
route: opts.route.pattern,
data: opts.data,
state: opts.state!,
Component() {
return appComponent;
},
})
: appComponent;
return h(CSP_CONTEXT.Provider, {
value: csp,
children: h(HEAD_CONTEXT.Provider, {
value: headComponents,
children: h(opts.app.default, {
params: opts.params as Record<string, string>,
url: opts.url,
route: opts.route.pattern,
data: opts.data,
state: opts.state!,
Component() {
return appComponent;
},
}),
children: child,
}),
});
}
@@ -226,7 +248,7 @@ export async function render<Data>(
// deno-lint-ignore no-explicit-any
let finalAppComp: VNode<any> = vnode as any;
let i = opts.layouts.length;
let i = layouts.length;
while (i--) {
const layout = layouts[i];
const curComp = finalAppComp;
@@ -309,7 +331,7 @@ export async function render<Data>(
} else {
if (
isAsyncComponent ||
opts.layouts.some((layout) => checkAsyncComponent(layout.default))
layouts.some((layout) => checkAsyncComponent(layout.default))
) {
if (opts.renderFn !== DEFAULT_RENDER_FN) {
throw new Error(