fix: rename layout config options (#1619)

This commit is contained in:
Marvin Hagemeister
2023-08-14 15:47:36 +02:00
committed by GitHub
parent ba67d895ce
commit 6436a43dce
13 changed files with 124 additions and 63 deletions
+25 -19
View File
@@ -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);
}
}
+1
View File
@@ -23,6 +23,7 @@ export type {
Handler,
HandlerContext,
Handlers,
LayoutConfig,
LayoutContext,
LayoutProps,
MiddlewareHandler,
+7 -7
View File
@@ -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
View File
@@ -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 ---
+14 -10
View File
@@ -27,11 +27,13 @@ import * as $21 from "./routes/no_app.tsx";
import * as $22 from "./routes/other.tsx";
import * as $23 from "./routes/override/_layout.tsx";
import * as $24 from "./routes/override/index.tsx";
import * as $25 from "./routes/override/no_app.tsx";
import * as $26 from "./routes/override/no_layout.tsx";
import * as $27 from "./routes/override/no_layout_no_app.tsx";
import * as $28 from "./routes/skip/sub/_layout.tsx";
import * as $29 from "./routes/skip/sub/index.tsx";
import * as $25 from "./routes/override/layout_no_app/_layout.tsx";
import * as $26 from "./routes/override/layout_no_app/index.tsx";
import * as $27 from "./routes/override/no_app.tsx";
import * as $28 from "./routes/override/no_layout.tsx";
import * as $29 from "./routes/override/no_layout_no_app.tsx";
import * as $30 from "./routes/skip/sub/_layout.tsx";
import * as $31 from "./routes/skip/sub/index.tsx";
const manifest = {
routes: {
@@ -60,11 +62,13 @@ const manifest = {
"./routes/other.tsx": $22,
"./routes/override/_layout.tsx": $23,
"./routes/override/index.tsx": $24,
"./routes/override/no_app.tsx": $25,
"./routes/override/no_layout.tsx": $26,
"./routes/override/no_layout_no_app.tsx": $27,
"./routes/skip/sub/_layout.tsx": $28,
"./routes/skip/sub/index.tsx": $29,
"./routes/override/layout_no_app/_layout.tsx": $25,
"./routes/override/layout_no_app/index.tsx": $26,
"./routes/override/no_app.tsx": $27,
"./routes/override/no_layout.tsx": $28,
"./routes/override/no_layout_no_app.tsx": $29,
"./routes/skip/sub/_layout.tsx": $30,
"./routes/skip/sub/index.tsx": $31,
},
islands: {},
baseUrl: import.meta.url,
+1 -1
View File
@@ -1,7 +1,7 @@
import { RouteConfig } from "$fresh/server.ts";
export const config: RouteConfig = {
appTemplate: false,
skipAppTemplate: false,
};
export default function OverridePage() {
@@ -1,7 +1,7 @@
import { LayoutProps, RouteConfig } from "$fresh/server.ts";
import { LayoutConfig, LayoutProps } from "$fresh/server.ts";
export const config: RouteConfig = {
rootLayout: true,
export const config: LayoutConfig = {
skipInheritedLayouts: true,
};
export default function OverrideLayout({ Component }: LayoutProps) {
@@ -0,0 +1,13 @@
import { LayoutConfig, LayoutProps } from "$fresh/server.ts";
export const config: LayoutConfig = {
skipAppTemplate: true,
};
export default function OverrideLayout({ Component }: LayoutProps) {
return (
<div class="no-app-layout">
<Component />
</div>
);
}
@@ -0,0 +1,7 @@
export default function OverridePage() {
return (
<div class="page">
/override/layout_no_app page
</div>
);
}
@@ -1,7 +1,7 @@
import { RouteConfig } from "$fresh/server.ts";
export const config: RouteConfig = {
appTemplate: false,
skipAppTemplate: true,
};
export default function OverridePage() {
@@ -1,7 +1,7 @@
import { RouteConfig } from "$fresh/server.ts";
import { LayoutConfig } from "$fresh/server.ts";
export const config: RouteConfig = {
rootLayout: true,
export const config: LayoutConfig = {
skipInheritedLayouts: true,
};
export default function OverridePage() {
@@ -1,8 +1,8 @@
import { RouteConfig } from "$fresh/server.ts";
import { LayoutConfig } from "$fresh/server.ts";
export const config: RouteConfig = {
appTemplate: false,
rootLayout: true,
export const config: LayoutConfig = {
skipAppTemplate: true,
skipInheritedLayouts: true,
};
export default function OverridePage() {
+12 -1
View File
@@ -120,6 +120,17 @@ Deno.test("disable _app layout", async () => {
);
});
Deno.test("disable _app in _layout", async () => {
await withFresh(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/override/layout_no_app`);
assertNotSelector(doc, "body body");
assertSelector(doc, "body > .override-layout > .no-app-layout > .page");
},
);
});
Deno.test("override layouts", async () => {
await withFresh(
"./tests/fixture_layouts/main.ts",
@@ -140,7 +151,7 @@ Deno.test("route overrides layout", async () => {
);
});
Deno.test("route overrides layout", async () => {
Deno.test("route overrides layout and app", async () => {
await withFresh(
"./tests/fixture_layouts/main.ts",
async (address) => {