feat: allow ctx.render() to skip wrappers (#3170)
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/<Component /></>)) .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: ``` <root>/routes └── /foo ├── _layout.tsx └── index.tsx ```
This commit is contained in:
@@ -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/<Component />
|
||||
</>
|
||||
))
|
||||
.get("/", (ctx) => ctx.render(<>index</>));
|
||||
|
||||
const server = new FakeServer(app.handler());
|
||||
|
||||
const res = await server.get("/");
|
||||
expect(await res.text()).toContain("<body>app/index<");
|
||||
});
|
||||
|
||||
Deno.test.ignore("App - .layout()", async () => {
|
||||
const app = new App()
|
||||
.layout("/", ({ Component }) => (
|
||||
<>
|
||||
layout/<Component />
|
||||
</>
|
||||
))
|
||||
.layout("/foo", ({ Component }) => (
|
||||
<>
|
||||
foo/<Component />
|
||||
</>
|
||||
))
|
||||
.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("<body>layout/foo/index<");
|
||||
});
|
||||
|
||||
Deno.test("App - .appWrapper() + .layout()", async () => {
|
||||
const app = new App()
|
||||
.appWrapper(({ Component }) => (
|
||||
<>
|
||||
app/<Component />
|
||||
</>
|
||||
))
|
||||
.layout("/", ({ Component }) => (
|
||||
<>
|
||||
layout/<Component />
|
||||
</>
|
||||
))
|
||||
.get("/", (ctx) => ctx.render(<>index</>));
|
||||
|
||||
const server = new FakeServer(app.handler());
|
||||
|
||||
const res = await server.get("/");
|
||||
expect(await res.text()).toContain("<body>app/layout/index<");
|
||||
});
|
||||
|
||||
Deno.test("App - ctx.render() without app wrapper or layout", async () => {
|
||||
const app = new App()
|
||||
.appWrapper(({ Component }) => (
|
||||
<>
|
||||
app/<Component />
|
||||
</>
|
||||
))
|
||||
.layout("/", ({ Component }) => (
|
||||
<>
|
||||
layout/<Component />
|
||||
</>
|
||||
))
|
||||
.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("<body>index<");
|
||||
});
|
||||
|
||||
+4
-2
@@ -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<State> {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
vnode: VNode<any> | null,
|
||||
init: ResponseInit | undefined = {},
|
||||
config: LayoutConfig = {},
|
||||
): Promise<Response> {
|
||||
if (arguments.length === 0) {
|
||||
throw new Error(`No arguments passed to: ctx.render()`);
|
||||
@@ -194,8 +196,8 @@ export class Context<State> {
|
||||
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<State>;
|
||||
|
||||
// Compose final vnode tree
|
||||
|
||||
Reference in New Issue
Block a user