feat: allow layout and _app overrides (#1611)
This commit is contained in:
@@ -253,6 +253,8 @@ export class ServerContext {
|
||||
component,
|
||||
handler,
|
||||
csp: Boolean(config?.csp ?? false),
|
||||
appLayout: Boolean(config?.appTemplate ?? true),
|
||||
rootLayout: Boolean(config?.rootLayout ?? false),
|
||||
};
|
||||
routes.push(route);
|
||||
} else if (isMiddleware) {
|
||||
@@ -288,6 +290,8 @@ export class ServerContext {
|
||||
component,
|
||||
handler: handler ?? ((req) => router.defaultOtherHandler(req)),
|
||||
csp: Boolean(config?.csp ?? false),
|
||||
appLayout: Boolean(config?.appTemplate ?? true),
|
||||
rootLayout: Boolean(config?.rootLayout ?? false),
|
||||
};
|
||||
} else if (
|
||||
path === "/_500.tsx" || path === "/_500.ts" ||
|
||||
@@ -308,6 +312,8 @@ export class ServerContext {
|
||||
handler: handler ??
|
||||
((req, ctx) => router.defaultErrorHandler(req, ctx, ctx.error)),
|
||||
csp: Boolean(config?.csp ?? false),
|
||||
appLayout: Boolean(config?.appTemplate ?? true),
|
||||
rootLayout: Boolean(config?.rootLayout ?? false),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -971,6 +977,8 @@ const DEFAULT_NOT_FOUND: UnknownPage = {
|
||||
name: "_404",
|
||||
handler: (req) => router.defaultOtherHandler(req),
|
||||
csp: false,
|
||||
appLayout: true,
|
||||
rootLayout: false,
|
||||
};
|
||||
|
||||
const DEFAULT_ERROR: ErrorPage = {
|
||||
@@ -981,6 +989,8 @@ const DEFAULT_ERROR: ErrorPage = {
|
||||
component: DefaultErrorHandler,
|
||||
handler: (_req, ctx) => ctx.render(),
|
||||
csp: false,
|
||||
appLayout: true,
|
||||
rootLayout: false,
|
||||
};
|
||||
|
||||
export function selectSharedRoutes<T>(
|
||||
|
||||
+35
-13
@@ -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(
|
||||
|
||||
@@ -79,6 +79,18 @@ export interface RouteConfig {
|
||||
* using the `useCSP` hook.
|
||||
*/
|
||||
csp?: boolean;
|
||||
|
||||
/**
|
||||
* Mark this route as the root layout and ignore previously
|
||||
* inherited layouts. Default: `false`
|
||||
*/
|
||||
rootLayout?: boolean;
|
||||
|
||||
/**
|
||||
* Whether to use the `routes/_app` template if available or not.
|
||||
* Default: `true`
|
||||
*/
|
||||
appTemplate?: boolean;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-empty-interface
|
||||
@@ -153,6 +165,8 @@ export interface Route<Data = any> {
|
||||
component?: PageComponent<Data>;
|
||||
handler: Handler<Data> | Handlers<Data>;
|
||||
csp: boolean;
|
||||
appLayout: boolean;
|
||||
rootLayout: boolean;
|
||||
}
|
||||
|
||||
export interface RouterState {
|
||||
@@ -185,6 +199,7 @@ export type AsyncLayout<T = any, S = Record<string, unknown>> = (
|
||||
|
||||
export interface LayoutModule {
|
||||
default: ComponentType<LayoutProps> | AsyncLayout;
|
||||
config?: Pick<RouteConfig, "appTemplate" | "rootLayout">;
|
||||
}
|
||||
|
||||
export interface LayoutRoute {
|
||||
@@ -236,6 +251,8 @@ export interface UnknownPage {
|
||||
component?: PageComponent<UnknownPageProps>;
|
||||
handler: UnknownHandler;
|
||||
csp: boolean;
|
||||
appLayout: boolean;
|
||||
rootLayout: boolean;
|
||||
}
|
||||
|
||||
// --- ERROR PAGE ---
|
||||
@@ -281,6 +298,8 @@ export interface ErrorPage {
|
||||
component?: PageComponent<ErrorPageProps>;
|
||||
handler: ErrorHandler;
|
||||
csp: boolean;
|
||||
appLayout: boolean;
|
||||
rootLayout: boolean;
|
||||
}
|
||||
|
||||
// --- MIDDLEWARES ---
|
||||
|
||||
Reference in New Issue
Block a user