feat: support async layout functions (#1597)

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
Marvin Hagemeister
2023-08-08 14:15:02 +02:00
committed by GitHub
co-authored by Asher Gomez
parent 11077227b5
commit 8325225a0d
11 changed files with 277 additions and 65 deletions
+42 -30
View File
@@ -5,42 +5,54 @@
import * as $0 from "./routes/_app.tsx";
import * as $1 from "./routes/_layout.tsx";
import * as $2 from "./routes/_middleware.ts";
import * as $3 from "./routes/files/js/_layout.js";
import * as $4 from "./routes/files/js/index.js";
import * as $5 from "./routes/files/jsx/_layout.jsx";
import * as $6 from "./routes/files/jsx/index.jsx";
import * as $7 from "./routes/files/ts/_layout.ts";
import * as $8 from "./routes/files/ts/index.ts";
import * as $9 from "./routes/files/tsx/_layout.tsx";
import * as $10 from "./routes/files/tsx/index.tsx";
import * as $11 from "./routes/foo/_layout.tsx";
import * as $12 from "./routes/foo/bar.tsx";
import * as $13 from "./routes/foo/index.tsx";
import * as $14 from "./routes/index.tsx";
import * as $15 from "./routes/other.tsx";
import * as $16 from "./routes/skip/sub/_layout.tsx";
import * as $17 from "./routes/skip/sub/index.tsx";
import * as $3 from "./routes/async/_layout.tsx";
import * as $4 from "./routes/async/index.tsx";
import * as $5 from "./routes/async/redirect/_layout.tsx";
import * as $6 from "./routes/async/redirect/index.tsx";
import * as $7 from "./routes/async/sub/_layout.tsx";
import * as $8 from "./routes/async/sub/index.tsx";
import * as $9 from "./routes/files/js/_layout.js";
import * as $10 from "./routes/files/js/index.js";
import * as $11 from "./routes/files/jsx/_layout.jsx";
import * as $12 from "./routes/files/jsx/index.jsx";
import * as $13 from "./routes/files/ts/_layout.ts";
import * as $14 from "./routes/files/ts/index.ts";
import * as $15 from "./routes/files/tsx/_layout.tsx";
import * as $16 from "./routes/files/tsx/index.tsx";
import * as $17 from "./routes/foo/_layout.tsx";
import * as $18 from "./routes/foo/bar.tsx";
import * as $19 from "./routes/foo/index.tsx";
import * as $20 from "./routes/index.tsx";
import * as $21 from "./routes/other.tsx";
import * as $22 from "./routes/skip/sub/_layout.tsx";
import * as $23 from "./routes/skip/sub/index.tsx";
const manifest = {
routes: {
"./routes/_app.tsx": $0,
"./routes/_layout.tsx": $1,
"./routes/_middleware.ts": $2,
"./routes/files/js/_layout.js": $3,
"./routes/files/js/index.js": $4,
"./routes/files/jsx/_layout.jsx": $5,
"./routes/files/jsx/index.jsx": $6,
"./routes/files/ts/_layout.ts": $7,
"./routes/files/ts/index.ts": $8,
"./routes/files/tsx/_layout.tsx": $9,
"./routes/files/tsx/index.tsx": $10,
"./routes/foo/_layout.tsx": $11,
"./routes/foo/bar.tsx": $12,
"./routes/foo/index.tsx": $13,
"./routes/index.tsx": $14,
"./routes/other.tsx": $15,
"./routes/skip/sub/_layout.tsx": $16,
"./routes/skip/sub/index.tsx": $17,
"./routes/async/_layout.tsx": $3,
"./routes/async/index.tsx": $4,
"./routes/async/redirect/_layout.tsx": $5,
"./routes/async/redirect/index.tsx": $6,
"./routes/async/sub/_layout.tsx": $7,
"./routes/async/sub/index.tsx": $8,
"./routes/files/js/_layout.js": $9,
"./routes/files/js/index.js": $10,
"./routes/files/jsx/_layout.jsx": $11,
"./routes/files/jsx/index.jsx": $12,
"./routes/files/ts/_layout.ts": $13,
"./routes/files/ts/index.ts": $14,
"./routes/files/tsx/_layout.tsx": $15,
"./routes/files/tsx/index.tsx": $16,
"./routes/foo/_layout.tsx": $17,
"./routes/foo/bar.tsx": $18,
"./routes/foo/index.tsx": $19,
"./routes/index.tsx": $20,
"./routes/other.tsx": $21,
"./routes/skip/sub/_layout.tsx": $22,
"./routes/skip/sub/index.tsx": $23,
},
islands: {},
baseUrl: import.meta.url,
+1 -1
View File
@@ -1,6 +1,6 @@
import { AppProps } from "$fresh/server.ts";
export default function App({ Component }: AppProps) {
export default function App({ Component, state }: AppProps) {
return (
<div class="app">
<Component />
@@ -0,0 +1,15 @@
import { LayoutProps, RouteContext } from "$fresh/server.ts";
export default async function AsyncLayout(
req: Request,
ctx: RouteContext,
{ Component }: LayoutProps,
) {
await new Promise((r) => setTimeout(r, 10));
return (
<div class="async-layout">
<p>Async layout</p>
<Component />
</div>
);
}
@@ -0,0 +1,10 @@
import { RouteContext } from "$fresh/server.ts";
export default async function AsyncPage(req: Request, ctx: RouteContext) {
await new Promise((r) => setTimeout(r, 10));
return (
<div class="async-page">
<p>Async page</p>
</div>
);
}
@@ -0,0 +1,14 @@
import { LayoutProps, RouteContext } from "$fresh/server.ts";
import { delay } from "$std/async/mod.ts";
export default async function AsyncSubLayout(
req: Request,
ctx: RouteContext,
{ Component }: LayoutProps,
) {
await delay(10);
return new Response(null, {
status: 307,
headers: { Location: "/async/sub" },
});
}
@@ -0,0 +1,13 @@
import { RouteContext } from "$fresh/server.ts";
export default async function AsyncRedirectPage(
req: Request,
ctx: RouteContext,
) {
await new Promise((r) => setTimeout(r, 10));
return (
<div class="async-sub-page">
<p>Async Redirect page</p>
</div>
);
}
@@ -0,0 +1,15 @@
import { LayoutProps, RouteContext } from "$fresh/server.ts";
export default async function AsyncSubLayout(
req: Request,
ctx: RouteContext,
{ Component }: LayoutProps,
) {
await new Promise((r) => setTimeout(r, 10));
return (
<div class="async-sub-layout">
<p>Async Sub layout</p>
<Component />
</div>
);
}
@@ -0,0 +1,10 @@
import { RouteContext } from "$fresh/server.ts";
export default async function AsyncSubPage(req: Request, ctx: RouteContext) {
await new Promise((r) => setTimeout(r, 10));
return (
<div class="async-sub-page">
<p>Async Sub page</p>
</div>
);
}
+39
View File
@@ -64,3 +64,42 @@ Deno.test("check file types", async (t) => {
},
);
});
Deno.test("render async layout", async () => {
await withFresh(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/async`);
assertSelector(doc, ".app .root-layout .async-layout .async-page");
},
);
});
Deno.test("render nested async layout", async () => {
await withFresh(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/async/sub`);
assertSelector(
doc,
".app .root-layout .async-layout .async-sub-layout .async-sub-page",
);
},
);
});
Deno.test({
name: "can return Response from async layout",
fn: async () => {
await withFresh(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/async/redirect`);
assertSelector(
doc,
".app .root-layout .async-layout .async-sub-layout .async-sub-page",
);
},
);
},
});