fix: rename layout config options (#1619)
This commit is contained in:
+25
-19
@@ -253,8 +253,8 @@ export class ServerContext {
|
||||
component,
|
||||
handler,
|
||||
csp: Boolean(config?.csp ?? false),
|
||||
appLayout: Boolean(config?.appTemplate ?? true),
|
||||
rootLayout: Boolean(config?.rootLayout ?? false),
|
||||
appTemplate: !config?.skipAppTemplate,
|
||||
inheritLayouts: !config?.skipInheritedLayouts,
|
||||
};
|
||||
routes.push(route);
|
||||
} else if (isMiddleware) {
|
||||
@@ -268,9 +268,14 @@ export class ServerContext {
|
||||
) {
|
||||
app = module as AppModule;
|
||||
} else if (isLayout) {
|
||||
const mod = module as LayoutModule;
|
||||
const config = mod.config;
|
||||
layouts.push({
|
||||
baseRoute: toBaseRoute(baseRoute),
|
||||
module: module as LayoutModule,
|
||||
handler: mod.handler,
|
||||
component: mod.default,
|
||||
appTemplate: !config?.skipAppTemplate,
|
||||
inheritLayouts: !config?.skipInheritedLayouts,
|
||||
});
|
||||
} else if (
|
||||
path === "/_404.tsx" || path === "/_404.ts" ||
|
||||
@@ -290,8 +295,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),
|
||||
appTemplate: !config?.skipAppTemplate,
|
||||
inheritLayouts: !config?.skipInheritedLayouts,
|
||||
};
|
||||
} else if (
|
||||
path === "/_500.tsx" || path === "/_500.ts" ||
|
||||
@@ -312,8 +317,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),
|
||||
appTemplate: !config?.skipAppTemplate,
|
||||
inheritLayouts: !config?.skipInheritedLayouts,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -542,13 +547,13 @@ export class ServerContext {
|
||||
params: paramsAndRouteResult.params,
|
||||
};
|
||||
|
||||
for (const mw of mws) {
|
||||
if (mw.handler instanceof Array) {
|
||||
for (const handler of mw.handler) {
|
||||
for (const { module } of mws) {
|
||||
if (module.handler instanceof Array) {
|
||||
for (const handler of module.handler) {
|
||||
handlers.push(() => handler(req, middlewareCtx));
|
||||
}
|
||||
} else {
|
||||
const handler = mw.handler;
|
||||
const handler = module.handler;
|
||||
handlers.push(() => handler(req, middlewareCtx));
|
||||
}
|
||||
}
|
||||
@@ -977,8 +982,8 @@ const DEFAULT_NOT_FOUND: UnknownPage = {
|
||||
name: "_404",
|
||||
handler: (req) => router.defaultOtherHandler(req),
|
||||
csp: false,
|
||||
appLayout: true,
|
||||
rootLayout: false,
|
||||
appTemplate: true,
|
||||
inheritLayouts: true,
|
||||
};
|
||||
|
||||
const DEFAULT_ERROR: ErrorPage = {
|
||||
@@ -989,23 +994,24 @@ const DEFAULT_ERROR: ErrorPage = {
|
||||
component: DefaultErrorHandler,
|
||||
handler: (_req, ctx) => ctx.render(),
|
||||
csp: false,
|
||||
appLayout: true,
|
||||
rootLayout: false,
|
||||
appTemplate: true,
|
||||
inheritLayouts: true,
|
||||
};
|
||||
|
||||
export function selectSharedRoutes<T>(
|
||||
export function selectSharedRoutes<T extends { baseRoute: BaseRoute }>(
|
||||
curBaseRoute: BaseRoute,
|
||||
items: { baseRoute: BaseRoute; module: T }[],
|
||||
items: T[],
|
||||
): T[] {
|
||||
const selected: T[] = [];
|
||||
|
||||
for (const { baseRoute, module } of items) {
|
||||
for (const item of items) {
|
||||
const { baseRoute } = item;
|
||||
const res = curBaseRoute === baseRoute ||
|
||||
curBaseRoute.startsWith(
|
||||
baseRoute.length > 1 ? baseRoute + "/" : baseRoute,
|
||||
);
|
||||
if (res) {
|
||||
selected.push(module);
|
||||
selected.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ export type {
|
||||
Handler,
|
||||
HandlerContext,
|
||||
Handlers,
|
||||
LayoutConfig,
|
||||
LayoutContext,
|
||||
LayoutProps,
|
||||
MiddlewareHandler,
|
||||
|
||||
@@ -15,7 +15,7 @@ import {
|
||||
AsyncRoute,
|
||||
ErrorPage,
|
||||
Island,
|
||||
LayoutModule,
|
||||
LayoutRoute,
|
||||
Plugin,
|
||||
PluginRenderFunctionResult,
|
||||
PluginRenderResult,
|
||||
@@ -83,7 +83,7 @@ export interface RenderOptions<Data> {
|
||||
islands: Island[];
|
||||
plugins: Plugin[];
|
||||
app: AppModule;
|
||||
layouts: LayoutModule[];
|
||||
layouts: LayoutRoute[];
|
||||
imports: string[];
|
||||
dependenciesFn: (path: string) => string[];
|
||||
url: URL;
|
||||
@@ -182,11 +182,11 @@ export async function render<Data>(
|
||||
// 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) {
|
||||
if (opts.route.inheritLayouts) {
|
||||
let rootIdx = 0;
|
||||
let layoutIdx = opts.layouts.length;
|
||||
while (layoutIdx--) {
|
||||
if (opts.layouts[layoutIdx].config?.rootLayout) {
|
||||
if (!opts.layouts[layoutIdx].inheritLayouts) {
|
||||
rootIdx = layoutIdx;
|
||||
break;
|
||||
}
|
||||
@@ -265,13 +265,13 @@ export async function render<Data>(
|
||||
const renderStack: any[] = [];
|
||||
// Check if appLayout is enabled
|
||||
if (
|
||||
opts.route.appLayout &&
|
||||
layouts.every((layout) => layout.config?.appTemplate !== false)
|
||||
opts.route.appTemplate &&
|
||||
layouts.every((layout) => layout.appTemplate)
|
||||
) {
|
||||
renderStack.push(opts.app.default);
|
||||
}
|
||||
for (let i = 0; i < layouts.length; i++) {
|
||||
renderStack.push(layouts[i].default);
|
||||
renderStack.push(layouts[i].component);
|
||||
}
|
||||
renderStack.push(component);
|
||||
|
||||
|
||||
+33
-14
@@ -89,16 +89,16 @@ export interface RouteConfig {
|
||||
csp?: boolean;
|
||||
|
||||
/**
|
||||
* Mark this route as the root layout and ignore previously
|
||||
* inherited layouts. Default: `false`
|
||||
* Skip already inherited layouts
|
||||
* Default: `false`
|
||||
*/
|
||||
rootLayout?: boolean;
|
||||
skipInheritedLayouts?: boolean;
|
||||
|
||||
/**
|
||||
* Whether to use the `routes/_app` template if available or not.
|
||||
* Default: `true`
|
||||
* Skip rendering the `routes/_app` template
|
||||
* Default: `false`
|
||||
*/
|
||||
appTemplate?: boolean;
|
||||
skipAppTemplate?: boolean;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-empty-interface
|
||||
@@ -173,8 +173,8 @@ export interface Route<Data = any> {
|
||||
component?: PageComponent<Data> | AsyncLayout<Data> | AsyncRoute<Data>;
|
||||
handler: Handler<Data> | Handlers<Data>;
|
||||
csp: boolean;
|
||||
appLayout: boolean;
|
||||
rootLayout: boolean;
|
||||
appTemplate: boolean;
|
||||
inheritLayouts: boolean;
|
||||
}
|
||||
|
||||
export interface RouterState {
|
||||
@@ -213,14 +213,33 @@ export type AsyncLayout<T = any, S = Record<string, unknown>> = (
|
||||
ctx: LayoutContext<T, S>,
|
||||
) => Promise<ComponentChildren | Response>;
|
||||
|
||||
export interface LayoutConfig {
|
||||
/**
|
||||
* Skip already inherited layouts
|
||||
* Default: `false`
|
||||
*/
|
||||
skipAppTemplate?: boolean;
|
||||
/**
|
||||
* Skip rendering the `routes/_app`.
|
||||
* Default: `false`
|
||||
*/
|
||||
skipInheritedLayouts?: boolean;
|
||||
}
|
||||
|
||||
export interface LayoutModule {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
handler?: Handler<any, any> | Handlers<any, any>;
|
||||
default: ComponentType<LayoutProps> | AsyncLayout;
|
||||
config?: Pick<RouteConfig, "appTemplate" | "rootLayout">;
|
||||
config?: LayoutConfig;
|
||||
}
|
||||
|
||||
export interface LayoutRoute {
|
||||
baseRoute: BaseRoute;
|
||||
module: LayoutModule;
|
||||
// deno-lint-ignore no-explicit-any
|
||||
handler?: Handler<any, any> | Handlers<any, any>;
|
||||
component: LayoutModule["default"];
|
||||
appTemplate: boolean;
|
||||
inheritLayouts: boolean;
|
||||
}
|
||||
|
||||
// --- UNKNOWN PAGE ---
|
||||
@@ -267,8 +286,8 @@ export interface UnknownPage {
|
||||
component?: PageComponent<UnknownPageProps>;
|
||||
handler: UnknownHandler;
|
||||
csp: boolean;
|
||||
appLayout: boolean;
|
||||
rootLayout: boolean;
|
||||
appTemplate: boolean;
|
||||
inheritLayouts: boolean;
|
||||
}
|
||||
|
||||
// --- ERROR PAGE ---
|
||||
@@ -314,8 +333,8 @@ export interface ErrorPage {
|
||||
component?: PageComponent<ErrorPageProps>;
|
||||
handler: ErrorHandler;
|
||||
csp: boolean;
|
||||
appLayout: boolean;
|
||||
rootLayout: boolean;
|
||||
appTemplate: boolean;
|
||||
inheritLayouts: boolean;
|
||||
}
|
||||
|
||||
// --- MIDDLEWARES ---
|
||||
|
||||
Reference in New Issue
Block a user