From 92edab20f9d1aee81a19a79fb45134665b93ac82 Mon Sep 17 00:00:00 2001 From: Christian Svensson Date: Fri, 1 Aug 2025 14:48:32 +0200 Subject: [PATCH] feat: allow `ctx.render()` to skip wrappers (#3170) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allow `ctx.render()` to bypass app wrapper and layouts. - I originally intended this as a solution for middleware (`app.use()`) to have the same capability as routes (`app.route()`) to solve the `devErrorOverlay()` including wrappers. - The docs mention it's possible to "opt-out" during `ctx.render()` > Every [ctx.render()](https://fresh.deno.dev/docs/canary/concepts/context#render-1) call will include the app wrapper component by default, unless opted out. > \- https://fresh.deno.dev/docs/canary/advanced/app-wrapper I don't need this myself currently, but it seemed a bit weird that middleware was not able to bypass app wrapper or layouts, because it's not possible to pass a `RouteConfig`. If this API is not something you want to support, I could remove the code changes in favor of just updating docs (above) and adding the tests. ### Open questions Should it not be possible for "index" to use a layout? E.g. calls to `/foo` should include the layout: ```ts const app = new App() .layout("/foo", ({ Component }) => (<>foo/)) .get("/foo", (ctx) => ctx.render(<>index)); // Should this not be "foo/index"? Currently renders "index" ``` Currently, I ignored the test for this, since it was not what I expected. I tried to simulate the following folder layout, but with imperative code: ``` /routes └── /foo    ├── _layout.tsx    └── index.tsx ``` --- src/app_test.tsx | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ src/context.ts | 6 ++-- 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/app_test.tsx b/src/app_test.tsx index 86c43b64..f2c6570c 100644 --- a/src/app_test.tsx +++ b/src/app_test.tsx @@ -697,3 +697,83 @@ Deno.test("App - .get/post/patch/put/delete/head/all() - lazy", async () => { res = await server.head("/"); expect(await res.text()).toEqual("ok"); }); + +Deno.test("App - .appWrapper()", async () => { + const app = new App() + .appWrapper(({ Component }) => ( + <> + app/ + + )) + .get("/", (ctx) => ctx.render(<>index)); + + const server = new FakeServer(app.handler()); + + const res = await server.get("/"); + expect(await res.text()).toContain("app/index<"); +}); + +Deno.test.ignore("App - .layout()", async () => { + const app = new App() + .layout("/", ({ Component }) => ( + <> + layout/ + + )) + .layout("/foo", ({ Component }) => ( + <> + foo/ + + )) + .get("/foo", (ctx) => ctx.render(<>index)); + + const server = new FakeServer(app.handler()); + + // The `/foo` layout is not applied for GET `/foo`, is that correct? + const res = await server.get("/foo"); + expect(await res.text()).toContain("layout/foo/index<"); +}); + +Deno.test("App - .appWrapper() + .layout()", async () => { + const app = new App() + .appWrapper(({ Component }) => ( + <> + app/ + + )) + .layout("/", ({ Component }) => ( + <> + layout/ + + )) + .get("/", (ctx) => ctx.render(<>index)); + + const server = new FakeServer(app.handler()); + + const res = await server.get("/"); + expect(await res.text()).toContain("app/layout/index<"); +}); + +Deno.test("App - ctx.render() without app wrapper or layout", async () => { + const app = new App() + .appWrapper(({ Component }) => ( + <> + app/ + + )) + .layout("/", ({ Component }) => ( + <> + layout/ + + )) + .get("/", (ctx) => + ctx.render(<>index, undefined, { + skipAppWrapper: true, + skipInheritedLayouts: true, + })); + + const server = new FakeServer(app.handler()); + + const res = await server.get("/"); + expect(await res.text()).toContain("index<"); +}); diff --git a/src/context.ts b/src/context.ts index 7d6d5490..a829ef4a 100644 --- a/src/context.ts +++ b/src/context.ts @@ -10,6 +10,7 @@ import { import { SpanStatusCode } from "@opentelemetry/api"; import type { ResolvedFreshConfig } from "./config.ts"; import type { BuildCache } from "./build_cache.ts"; +import type { LayoutConfig } from "./types.ts"; import { RenderState, setRenderState } from "./runtime/server/preact_hooks.tsx"; import { PARTIAL_SEARCH_PARAM } from "./constants.ts"; import { tracer } from "./otel.ts"; @@ -187,6 +188,7 @@ export class Context { // deno-lint-ignore no-explicit-any vnode: VNode | null, init: ResponseInit | undefined = {}, + config: LayoutConfig = {}, ): Promise { if (arguments.length === 0) { throw new Error(`No arguments passed to: ctx.render()`); @@ -194,8 +196,8 @@ export class Context { throw new Error(`Non-JSX element passed to: ctx.render()`); } - const defs = this.#internal.layouts; - const appDef = this.#internal.app; + const defs = config.skipInheritedLayouts ? [] : this.#internal.layouts; + const appDef = config.skipAppWrapper ? null : this.#internal.app; const props = this as Context; // Compose final vnode tree