feat: support route layouts via _layout.tsx (#1506)

Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
Michael Gearhardt
2023-07-26 11:49:08 +02:00
committed by GitHub
co-authored by Marvin Hagemeister
parent 18857ea052
commit 1ed93cec88
27 changed files with 397 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
await dev(import.meta.url, "./main.ts");
+47
View File
@@ -0,0 +1,47 @@
// DO NOT EDIT. This file is generated by Fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/_app.tsx";
import * as $1 from "./routes/_layout.tsx";
import * as $2 from "./routes/files/js/_layout.js";
import * as $3 from "./routes/files/js/index.js";
import * as $4 from "./routes/files/jsx/_layout.jsx";
import * as $5 from "./routes/files/jsx/index.jsx";
import * as $6 from "./routes/files/ts/_layout.ts";
import * as $7 from "./routes/files/ts/index.ts";
import * as $8 from "./routes/files/tsx/_layout.tsx";
import * as $9 from "./routes/files/tsx/index.tsx";
import * as $10 from "./routes/foo/_layout.tsx";
import * as $11 from "./routes/foo/bar.tsx";
import * as $12 from "./routes/foo/index.tsx";
import * as $13 from "./routes/index.tsx";
import * as $14 from "./routes/other.tsx";
import * as $15 from "./routes/skip/sub/_layout.tsx";
import * as $16 from "./routes/skip/sub/index.tsx";
const manifest = {
routes: {
"./routes/_app.tsx": $0,
"./routes/_layout.tsx": $1,
"./routes/files/js/_layout.js": $2,
"./routes/files/js/index.js": $3,
"./routes/files/jsx/_layout.jsx": $4,
"./routes/files/jsx/index.jsx": $5,
"./routes/files/ts/_layout.ts": $6,
"./routes/files/ts/index.ts": $7,
"./routes/files/tsx/_layout.tsx": $8,
"./routes/files/tsx/index.tsx": $9,
"./routes/foo/_layout.tsx": $10,
"./routes/foo/bar.tsx": $11,
"./routes/foo/index.tsx": $12,
"./routes/index.tsx": $13,
"./routes/other.tsx": $14,
"./routes/skip/sub/_layout.tsx": $15,
"./routes/skip/sub/index.tsx": $16,
},
islands: {},
baseUrl: import.meta.url,
};
export default manifest;
+10
View File
@@ -0,0 +1,10 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.unstable" />
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
await start(manifest);
+9
View File
@@ -0,0 +1,9 @@
import { AppProps } from "$fresh/server.ts";
export default function App({ Component }: AppProps) {
return (
<div class="app">
<Component />
</div>
);
}
+9
View File
@@ -0,0 +1,9 @@
import { LayoutProps } from "$fresh/server.ts";
export default function RootLayout({ Component }: LayoutProps) {
return (
<div class="root-layout">
<Component />
</div>
);
}
@@ -0,0 +1,5 @@
import { h } from "preact";
export default function JsLayout({ Component }) {
return h("div", { class: "js-layout" }, h(Component, null));
}
@@ -0,0 +1,5 @@
import { h } from "preact";
export default function JsPage() {
return h("div", { class: "js-page" }, "/files/js");
}
@@ -0,0 +1,9 @@
import { h } from "preact";
export default function JsxLayout({ Component }) {
return (
<div class="jsx-layout">
<Component />
</div>
);
}
@@ -0,0 +1,3 @@
export default function JsxPage() {
return <div class="jsx-page">/files/jsx page</div>;
}
@@ -0,0 +1,6 @@
import { h } from "preact";
import { LayoutProps } from "$fresh/server.ts";
export default function TsLayout({ Component }: LayoutProps) {
return h("div", { class: "ts-layout" }, h(Component, null));
}
@@ -0,0 +1,5 @@
import { h } from "preact";
export default function TsPage() {
return h("div", { class: "ts-page" }, "/files/ts");
}
@@ -0,0 +1,10 @@
import { h } from "preact";
import { LayoutProps } from "$fresh/server.ts";
export default function TsxLayout({ Component }: LayoutProps) {
return (
<div class="tsx-layout">
<Component />
</div>
);
}
@@ -0,0 +1,3 @@
export default function TsxPage() {
return <div class="tsx-page">/files/tsx page</div>;
}
@@ -0,0 +1,9 @@
import { LayoutProps } from "$fresh/server.ts";
export default function FooLayout({ Component }: LayoutProps) {
return (
<div class="foo-layout">
<Component />
</div>
);
}
+7
View File
@@ -0,0 +1,7 @@
export default function BarPage() {
return (
<div class="bar-page">
/foo/bar page
</div>
);
}
@@ -0,0 +1,7 @@
export default function FooPage() {
return (
<div class="foo-page">
/foo index page
</div>
);
}
+7
View File
@@ -0,0 +1,7 @@
export default function Home() {
return (
<div class="home-page">
Home
</div>
);
}
+7
View File
@@ -0,0 +1,7 @@
export default function OtherPage() {
return (
<div class="other-page">
/other
</div>
);
}
@@ -0,0 +1,9 @@
import { LayoutProps } from "$fresh/server.ts";
export default function SubLayout({ Component }: LayoutProps) {
return (
<div class="sub-layout">
<Component />
</div>
);
}
@@ -0,0 +1,7 @@
export default function SubPage() {
return (
<div class="sub-page">
/skip/sub page
</div>
);
}
+64
View File
@@ -0,0 +1,64 @@
import { withPageName } from "./test_utils.ts";
Deno.test("apply root _layout and _app", async () => {
await withPageName(
"./tests/fixture_layouts/main.ts",
async (page, address) => {
await page.goto(address);
await page.waitForSelector(".app .root-layout .home-page");
await page.goto(`${address}/other`);
await page.waitForSelector(".app .root-layout .other-page");
},
);
});
Deno.test("apply sub layouts", async () => {
await withPageName(
"./tests/fixture_layouts/main.ts",
async (page, address) => {
await page.goto(`${address}/foo`);
await page.waitForSelector(".app .root-layout .foo-layout .foo-page");
await page.goto(`${address}/foo/bar`);
await page.waitForSelector(".app .root-layout .foo-layout .bar-page");
},
);
});
Deno.test("skip layouts if not present", async () => {
await withPageName(
"./tests/fixture_layouts/main.ts",
async (page, address) => {
await page.goto(`${address}/skip/sub`);
await page.waitForSelector(".app .root-layout .sub-layout .sub-page");
},
);
});
Deno.test("check file types", async (t) => {
await withPageName(
"./tests/fixture_layouts/main.ts",
async (page, address) => {
await t.step(".js", async () => {
await page.goto(`${address}/files/js`);
await page.waitForSelector(".app .root-layout .js-layout .js-page");
});
await t.step(".jsx", async () => {
await page.goto(`${address}/files/jsx`);
await page.waitForSelector(".app .root-layout .jsx-layout .jsx-page");
});
await t.step(".ts", async () => {
await page.goto(`${address}/files/ts`);
await page.waitForSelector(".app .root-layout .ts-layout .ts-page");
});
await t.step(".tsx", async () => {
await page.goto(`${address}/files/tsx`);
await page.waitForSelector(".app .root-layout .tsx-layout .tsx-page");
});
},
);
});