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, component,
handler, handler,
csp: Boolean(config?.csp ?? false), csp: Boolean(config?.csp ?? false),
appLayout: Boolean(config?.appTemplate ?? true), appTemplate: !config?.skipAppTemplate,
rootLayout: Boolean(config?.rootLayout ?? false), inheritLayouts: !config?.skipInheritedLayouts,
}; };
routes.push(route); routes.push(route);
} else if (isMiddleware) { } else if (isMiddleware) {
@@ -268,9 +268,14 @@ export class ServerContext {
) { ) {
app = module as AppModule; app = module as AppModule;
} else if (isLayout) { } else if (isLayout) {
const mod = module as LayoutModule;
const config = mod.config;
layouts.push({ layouts.push({
baseRoute: toBaseRoute(baseRoute), baseRoute: toBaseRoute(baseRoute),
module: module as LayoutModule, handler: mod.handler,
component: mod.default,
appTemplate: !config?.skipAppTemplate,
inheritLayouts: !config?.skipInheritedLayouts,
}); });
} else if ( } else if (
path === "/_404.tsx" || path === "/_404.ts" || path === "/_404.tsx" || path === "/_404.ts" ||
@@ -290,8 +295,8 @@ export class ServerContext {
component, component,
handler: handler ?? ((req) => router.defaultOtherHandler(req)), handler: handler ?? ((req) => router.defaultOtherHandler(req)),
csp: Boolean(config?.csp ?? false), csp: Boolean(config?.csp ?? false),
appLayout: Boolean(config?.appTemplate ?? true), appTemplate: !config?.skipAppTemplate,
rootLayout: Boolean(config?.rootLayout ?? false), inheritLayouts: !config?.skipInheritedLayouts,
}; };
} else if ( } else if (
path === "/_500.tsx" || path === "/_500.ts" || path === "/_500.tsx" || path === "/_500.ts" ||
@@ -312,8 +317,8 @@ export class ServerContext {
handler: handler ?? handler: handler ??
((req, ctx) => router.defaultErrorHandler(req, ctx, ctx.error)), ((req, ctx) => router.defaultErrorHandler(req, ctx, ctx.error)),
csp: Boolean(config?.csp ?? false), csp: Boolean(config?.csp ?? false),
appLayout: Boolean(config?.appTemplate ?? true), appTemplate: !config?.skipAppTemplate,
rootLayout: Boolean(config?.rootLayout ?? false), inheritLayouts: !config?.skipInheritedLayouts,
}; };
} }
} }
@@ -542,13 +547,13 @@ export class ServerContext {
params: paramsAndRouteResult.params, params: paramsAndRouteResult.params,
}; };
for (const mw of mws) { for (const { module } of mws) {
if (mw.handler instanceof Array) { if (module.handler instanceof Array) {
for (const handler of mw.handler) { for (const handler of module.handler) {
handlers.push(() => handler(req, middlewareCtx)); handlers.push(() => handler(req, middlewareCtx));
} }
} else { } else {
const handler = mw.handler; const handler = module.handler;
handlers.push(() => handler(req, middlewareCtx)); handlers.push(() => handler(req, middlewareCtx));
} }
} }
@@ -977,8 +982,8 @@ const DEFAULT_NOT_FOUND: UnknownPage = {
name: "_404", name: "_404",
handler: (req) => router.defaultOtherHandler(req), handler: (req) => router.defaultOtherHandler(req),
csp: false, csp: false,
appLayout: true, appTemplate: true,
rootLayout: false, inheritLayouts: true,
}; };
const DEFAULT_ERROR: ErrorPage = { const DEFAULT_ERROR: ErrorPage = {
@@ -989,23 +994,24 @@ const DEFAULT_ERROR: ErrorPage = {
component: DefaultErrorHandler, component: DefaultErrorHandler,
handler: (_req, ctx) => ctx.render(), handler: (_req, ctx) => ctx.render(),
csp: false, csp: false,
appLayout: true, appTemplate: true,
rootLayout: false, inheritLayouts: true,
}; };
export function selectSharedRoutes<T>( export function selectSharedRoutes<T extends { baseRoute: BaseRoute }>(
curBaseRoute: BaseRoute, curBaseRoute: BaseRoute,
items: { baseRoute: BaseRoute; module: T }[], items: T[],
): T[] { ): T[] {
const selected: T[] = []; const selected: T[] = [];
for (const { baseRoute, module } of items) { for (const item of items) {
const { baseRoute } = item;
const res = curBaseRoute === baseRoute || const res = curBaseRoute === baseRoute ||
curBaseRoute.startsWith( curBaseRoute.startsWith(
baseRoute.length > 1 ? baseRoute + "/" : baseRoute, baseRoute.length > 1 ? baseRoute + "/" : baseRoute,
); );
if (res) { if (res) {
selected.push(module); selected.push(item);
} }
} }
+1
View File
@@ -23,6 +23,7 @@ export type {
Handler, Handler,
HandlerContext, HandlerContext,
Handlers, Handlers,
LayoutConfig,
LayoutContext, LayoutContext,
LayoutProps, LayoutProps,
MiddlewareHandler, MiddlewareHandler,
+7 -7
View File
@@ -15,7 +15,7 @@ import {
AsyncRoute, AsyncRoute,
ErrorPage, ErrorPage,
Island, Island,
LayoutModule, LayoutRoute,
Plugin, Plugin,
PluginRenderFunctionResult, PluginRenderFunctionResult,
PluginRenderResult, PluginRenderResult,
@@ -83,7 +83,7 @@ export interface RenderOptions<Data> {
islands: Island[]; islands: Island[];
plugins: Plugin[]; plugins: Plugin[];
app: AppModule; app: AppModule;
layouts: LayoutModule[]; layouts: LayoutRoute[];
imports: string[]; imports: string[];
dependenciesFn: (path: string) => string[]; dependenciesFn: (path: string) => string[];
url: URL; url: URL;
@@ -182,11 +182,11 @@ export async function render<Data>(
// Only inherit layouts up to the nearest root layout. // Only inherit layouts up to the nearest root layout.
// Note that the route itself can act as the root layout. // Note that the route itself can act as the root layout.
let layouts = opts.layouts; let layouts = opts.layouts;
if (!opts.route.rootLayout) { if (opts.route.inheritLayouts) {
let rootIdx = 0; let rootIdx = 0;
let layoutIdx = opts.layouts.length; let layoutIdx = opts.layouts.length;
while (layoutIdx--) { while (layoutIdx--) {
if (opts.layouts[layoutIdx].config?.rootLayout) { if (!opts.layouts[layoutIdx].inheritLayouts) {
rootIdx = layoutIdx; rootIdx = layoutIdx;
break; break;
} }
@@ -265,13 +265,13 @@ export async function render<Data>(
const renderStack: any[] = []; const renderStack: any[] = [];
// Check if appLayout is enabled // Check if appLayout is enabled
if ( if (
opts.route.appLayout && opts.route.appTemplate &&
layouts.every((layout) => layout.config?.appTemplate !== false) layouts.every((layout) => layout.appTemplate)
) { ) {
renderStack.push(opts.app.default); renderStack.push(opts.app.default);
} }
for (let i = 0; i < layouts.length; i++) { for (let i = 0; i < layouts.length; i++) {
renderStack.push(layouts[i].default); renderStack.push(layouts[i].component);
} }
renderStack.push(component); renderStack.push(component);
+33 -14
View File
@@ -89,16 +89,16 @@ export interface RouteConfig {
csp?: boolean; csp?: boolean;
/** /**
* Mark this route as the root layout and ignore previously * Skip already inherited layouts
* inherited layouts. Default: `false` * Default: `false`
*/ */
rootLayout?: boolean; skipInheritedLayouts?: boolean;
/** /**
* Whether to use the `routes/_app` template if available or not. * Skip rendering the `routes/_app` template
* Default: `true` * Default: `false`
*/ */
appTemplate?: boolean; skipAppTemplate?: boolean;
} }
// deno-lint-ignore no-empty-interface // deno-lint-ignore no-empty-interface
@@ -173,8 +173,8 @@ export interface Route<Data = any> {
component?: PageComponent<Data> | AsyncLayout<Data> | AsyncRoute<Data>; component?: PageComponent<Data> | AsyncLayout<Data> | AsyncRoute<Data>;
handler: Handler<Data> | Handlers<Data>; handler: Handler<Data> | Handlers<Data>;
csp: boolean; csp: boolean;
appLayout: boolean; appTemplate: boolean;
rootLayout: boolean; inheritLayouts: boolean;
} }
export interface RouterState { export interface RouterState {
@@ -213,14 +213,33 @@ export type AsyncLayout<T = any, S = Record<string, unknown>> = (
ctx: LayoutContext<T, S>, ctx: LayoutContext<T, S>,
) => Promise<ComponentChildren | Response>; ) => 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 { export interface LayoutModule {
// deno-lint-ignore no-explicit-any
handler?: Handler<any, any> | Handlers<any, any>;
default: ComponentType<LayoutProps> | AsyncLayout; default: ComponentType<LayoutProps> | AsyncLayout;
config?: Pick<RouteConfig, "appTemplate" | "rootLayout">; config?: LayoutConfig;
} }
export interface LayoutRoute { export interface LayoutRoute {
baseRoute: BaseRoute; 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 --- // --- UNKNOWN PAGE ---
@@ -267,8 +286,8 @@ export interface UnknownPage {
component?: PageComponent<UnknownPageProps>; component?: PageComponent<UnknownPageProps>;
handler: UnknownHandler; handler: UnknownHandler;
csp: boolean; csp: boolean;
appLayout: boolean; appTemplate: boolean;
rootLayout: boolean; inheritLayouts: boolean;
} }
// --- ERROR PAGE --- // --- ERROR PAGE ---
@@ -314,8 +333,8 @@ export interface ErrorPage {
component?: PageComponent<ErrorPageProps>; component?: PageComponent<ErrorPageProps>;
handler: ErrorHandler; handler: ErrorHandler;
csp: boolean; csp: boolean;
appLayout: boolean; appTemplate: boolean;
rootLayout: boolean; inheritLayouts: boolean;
} }
// --- MIDDLEWARES --- // --- 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 $22 from "./routes/other.tsx";
import * as $23 from "./routes/override/_layout.tsx"; import * as $23 from "./routes/override/_layout.tsx";
import * as $24 from "./routes/override/index.tsx"; import * as $24 from "./routes/override/index.tsx";
import * as $25 from "./routes/override/no_app.tsx"; import * as $25 from "./routes/override/layout_no_app/_layout.tsx";
import * as $26 from "./routes/override/no_layout.tsx"; import * as $26 from "./routes/override/layout_no_app/index.tsx";
import * as $27 from "./routes/override/no_layout_no_app.tsx"; import * as $27 from "./routes/override/no_app.tsx";
import * as $28 from "./routes/skip/sub/_layout.tsx"; import * as $28 from "./routes/override/no_layout.tsx";
import * as $29 from "./routes/skip/sub/index.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 = { const manifest = {
routes: { routes: {
@@ -60,11 +62,13 @@ const manifest = {
"./routes/other.tsx": $22, "./routes/other.tsx": $22,
"./routes/override/_layout.tsx": $23, "./routes/override/_layout.tsx": $23,
"./routes/override/index.tsx": $24, "./routes/override/index.tsx": $24,
"./routes/override/no_app.tsx": $25, "./routes/override/layout_no_app/_layout.tsx": $25,
"./routes/override/no_layout.tsx": $26, "./routes/override/layout_no_app/index.tsx": $26,
"./routes/override/no_layout_no_app.tsx": $27, "./routes/override/no_app.tsx": $27,
"./routes/skip/sub/_layout.tsx": $28, "./routes/override/no_layout.tsx": $28,
"./routes/skip/sub/index.tsx": $29, "./routes/override/no_layout_no_app.tsx": $29,
"./routes/skip/sub/_layout.tsx": $30,
"./routes/skip/sub/index.tsx": $31,
}, },
islands: {}, islands: {},
baseUrl: import.meta.url, baseUrl: import.meta.url,
+1 -1
View File
@@ -1,7 +1,7 @@
import { RouteConfig } from "$fresh/server.ts"; import { RouteConfig } from "$fresh/server.ts";
export const config: RouteConfig = { export const config: RouteConfig = {
appTemplate: false, skipAppTemplate: false,
}; };
export default function OverridePage() { 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 = { export const config: LayoutConfig = {
rootLayout: true, skipInheritedLayouts: true,
}; };
export default function OverrideLayout({ Component }: LayoutProps) { 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"; import { RouteConfig } from "$fresh/server.ts";
export const config: RouteConfig = { export const config: RouteConfig = {
appTemplate: false, skipAppTemplate: true,
}; };
export default function OverridePage() { export default function OverridePage() {
@@ -1,7 +1,7 @@
import { RouteConfig } from "$fresh/server.ts"; import { LayoutConfig } from "$fresh/server.ts";
export const config: RouteConfig = { export const config: LayoutConfig = {
rootLayout: true, skipInheritedLayouts: true,
}; };
export default function OverridePage() { export default function OverridePage() {
@@ -1,8 +1,8 @@
import { RouteConfig } from "$fresh/server.ts"; import { LayoutConfig } from "$fresh/server.ts";
export const config: RouteConfig = { export const config: LayoutConfig = {
appTemplate: false, skipAppTemplate: true,
rootLayout: true, skipInheritedLayouts: true,
}; };
export default function OverridePage() { 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 () => { Deno.test("override layouts", async () => {
await withFresh( await withFresh(
"./tests/fixture_layouts/main.ts", "./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( await withFresh(
"./tests/fixture_layouts/main.ts", "./tests/fixture_layouts/main.ts",
async (address) => { async (address) => {