feat: a new "island" based architechture (#97)

This commit is contained in:
Luca Casonato
2022-03-04 19:49:27 +01:00
committed by GitHub
parent d441bf2489
commit 8daa280862
70 changed files with 820 additions and 972 deletions
+46
View File
@@ -0,0 +1,46 @@
// DO NOT EDIT. This file is generated by `fresh`.
// This file SHOULD be checked into source version control.
// To update this file, run `fresh routes`.
import * as $0 from "./routes/_middleware.ts";
import * as $1 from "./routes/_404.tsx";
import * as $2 from "./routes/failure.ts";
import * as $3 from "./routes/api/get_only.ts";
import * as $4 from "./routes/books/[id].tsx";
import * as $5 from "./routes/_render.ts";
import * as $6 from "./routes/props/[id].tsx";
import * as $7 from "./routes/_app.tsx";
import * as $8 from "./routes/static.tsx";
import * as $9 from "./routes/intercept_args.tsx";
import * as $10 from "./routes/wildcard.tsx";
import * as $11 from "./routes/intercept.tsx";
import * as $12 from "./routes/[name].tsx";
import * as $13 from "./routes/_500.tsx";
import * as $14 from "./routes/index.tsx";
import * as $$0 from "./islands/Test.tsx";
const manifest = {
routes: {
"./routes/_middleware.ts": $0,
"./routes/_404.tsx": $1,
"./routes/failure.ts": $2,
"./routes/api/get_only.ts": $3,
"./routes/books/[id].tsx": $4,
"./routes/_render.ts": $5,
"./routes/props/[id].tsx": $6,
"./routes/_app.tsx": $7,
"./routes/static.tsx": $8,
"./routes/intercept_args.tsx": $9,
"./routes/wildcard.tsx": $10,
"./routes/intercept.tsx": $11,
"./routes/[name].tsx": $12,
"./routes/_500.tsx": $13,
"./routes/index.tsx": $14,
},
islands: {
"./islands/Test.tsx": $$0,
},
baseUrl: import.meta.url,
};
export default manifest;
+7
View File
@@ -0,0 +1,7 @@
/** @jsx h */
import { h } from "../client_deps.ts";
export default function Test(props: { message: string }) {
return <p>{props.message}</p>;
}
+2 -2
View File
@@ -1,4 +1,4 @@
import { start } from "../../server.ts";
import routes from "./routes.gen.ts";
import { start } from "./server_deps.ts";
import routes from "./fresh.gen.ts";
await start(routes);
-19
View File
@@ -1,19 +0,0 @@
/** @jsx h */
import { h, IS_BROWSER, PageConfig, useData } from "../deps.ts";
export default function Home() {
const message = useData("home", (key) => {
return new Promise<string>((resolve) => {
setTimeout(() => resolve("Hello!"), 100);
});
});
return (
<div>
<p>{message}</p>
<p>{IS_BROWSER ? "Viewing browser render." : "Viewing JIT render."}</p>
</div>
);
}
export const config: PageConfig = { runtimeJS: true };
-31
View File
@@ -1,31 +0,0 @@
/** @jsx h */
import { h } from "../deps.ts";
import { HandlerContext } from "../../../server.ts";
interface renderData extends Record<string, unknown> {
info: string;
}
export default function Page({
renderData,
}: {
renderData: renderData;
}) {
return <div>{renderData.info}</div>;
}
export const handler = {
GET({ req, render }: HandlerContext) {
if (req.headers.get("accept")?.includes("text/html")) {
return render!({
info: "intercepted",
} as renderData);
} else {
return new Response("This is plain text");
}
},
POST() {
return new Response("POST response");
},
};
-9
View File
@@ -1,9 +0,0 @@
/** @jsx h */
import { h, PageConfig, PageProps } from "../../deps.ts";
export default function Home(props: PageProps) {
return <div>{JSON.stringify(props)}</div>;
}
export const config: PageConfig = { runtimeJS: true };
-42
View File
@@ -1,42 +0,0 @@
// DO NOT EDIT. This file is generated by `fresh`.
// This file SHOULD be checked into source version control.
// To update this file, run `fresh routes`.
import * as $0 from "./pages/index.tsx";
import * as $1 from "./pages/static.tsx";
import * as $2 from "./pages/intercept_args.tsx";
import * as $3 from "./pages/_render.ts";
import * as $4 from "./pages/props/[id].tsx";
import * as $5 from "./pages/failure.ts";
import * as $6 from "./pages/_middleware.ts";
import * as $7 from "./pages/books/[id].tsx";
import * as $8 from "./pages/wildcard.tsx";
import * as $9 from "./pages/api/get_only.ts";
import * as $10 from "./pages/_500.tsx";
import * as $11 from "./pages/intercept.tsx";
import * as $12 from "./pages/_404.tsx";
import * as $13 from "./pages/[name].tsx";
import * as $14 from "./pages/_app.tsx";
const routes = {
pages: {
"./pages/index.tsx": $0,
"./pages/static.tsx": $1,
"./pages/intercept_args.tsx": $2,
"./pages/_render.ts": $3,
"./pages/props/[id].tsx": $4,
"./pages/failure.ts": $5,
"./pages/_middleware.ts": $6,
"./pages/books/[id].tsx": $7,
"./pages/wildcard.tsx": $8,
"./pages/api/get_only.ts": $9,
"./pages/_500.tsx": $10,
"./pages/intercept.tsx": $11,
"./pages/_404.tsx": $12,
"./pages/[name].tsx": $13,
"./pages/_app.tsx": $14,
},
baseUrl: import.meta.url,
};
export default routes;
@@ -1,6 +1,6 @@
/** @jsx h */
import { h, PageConfig } from "../deps.ts";
import { h } from "../client_deps.ts";
interface Props {
params: Record<string, string | string[]>;
@@ -9,5 +9,3 @@ interface Props {
export default function Greet(props: Props) {
return <div>Hello {props.params.name}</div>;
}
export const config: PageConfig = { runtimeJS: true };
@@ -1,6 +1,6 @@
/** @jsx h */
import { h, UnknownPageProps } from "../deps.ts";
import { h, UnknownPageProps } from "../client_deps.ts";
export default function NotFoundPage({ url }: UnknownPageProps) {
return <p>404 not found: {url.pathname}</p>;
@@ -1,6 +1,6 @@
/** @jsx h */
import { ErrorPageProps, h } from "../deps.ts";
import { ErrorPageProps, h } from "../client_deps.ts";
export default function Error500Page({ error }: ErrorPageProps) {
return <p>500 internal error: {(error as Error).message}</p>;
@@ -1,7 +1,7 @@
/** @jsx h */
/** @jsxFrag Fragment */
import { AppProps, Fragment, h, Head } from "../deps.ts";
import { AppProps, Fragment, h, Head } from "../client_deps.ts";
export default function App(props: AppProps) {
return (
@@ -1,6 +1,6 @@
/** @jsx h */
import { h, PageConfig, PageProps } from "../../deps.ts";
import { h, PageConfig, PageProps } from "../../client_deps.ts";
export default function Page(props: PageProps) {
return <div>Book {props.params.id}</div>;
+13
View File
@@ -0,0 +1,13 @@
/** @jsx h */
import { h, IS_BROWSER } from "../client_deps.ts";
import Test from "../islands/Test.tsx";
export default function Home() {
return (
<div>
<Test message="Hello!" />
<p>{IS_BROWSER ? "Viewing browser render." : "Viewing JIT render."}</p>
</div>
);
}
@@ -1,6 +1,6 @@
/** @jsx h */
import { h } from "../deps.ts";
import { h } from "../client_deps.ts";
import { HandlerContext } from "../../../server.ts";
export default function Page() {
@@ -10,7 +10,7 @@ export default function Page() {
export const handler = {
GET({ req, render }: HandlerContext) {
if (req.headers.get("accept")?.includes("text/html")) {
return render!();
return render();
} else {
return new Response("This is plain text");
}
+27
View File
@@ -0,0 +1,27 @@
/** @jsx h */
import { h, PageProps } from "../client_deps.ts";
import { HandlerContext, Handlers } from "../../../server.ts";
interface Data extends Record<string, unknown> {
info: string;
}
export default function Page({ data }: PageProps<Data>) {
return <div>{data.info}</div>;
}
export const handler: Handlers<Data> = {
GET({ req, render }) {
if (req.headers.get("accept")?.includes("text/html")) {
return render({
info: "intercepted",
});
} else {
return new Response("This is plain text");
}
},
POST() {
return new Response("POST response");
},
};
+7
View File
@@ -0,0 +1,7 @@
/** @jsx h */
import { h, PageProps } from "../../client_deps.ts";
export default function Home(props: PageProps) {
return <div>{JSON.stringify(props)}</div>;
}
@@ -1,6 +1,6 @@
/** @jsx h */
import { h } from "../deps.ts";
import { h } from "../client_deps.ts";
export default function StaticPage() {
return <p>This is a static page.</p>;
@@ -1,6 +1,6 @@
/** @jsx h */
import { h, PageConfig, PageProps } from "../deps.ts";
import { h, PageConfig, PageProps } from "../client_deps.ts";
export default function WildcardPage({ params }: PageProps) {
if (typeof params.path === "string") {
+1
View File
@@ -0,0 +1 @@
export * from "../../server.ts";
+5 -18
View File
@@ -1,8 +1,8 @@
import { ServerContext } from "../server.ts";
import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
import routes from "./fixture/routes.gen.ts";
import manifest from "./fixture/fresh.gen.ts";
const ctx = await ServerContext.fromRoutes(routes);
const ctx = await ServerContext.fromManifest(manifest);
const router = ctx.handler();
Deno.test("/ page prerender", async () => {
@@ -13,16 +13,12 @@ Deno.test("/ page prerender", async () => {
assertEquals(resp.headers.get("server"), "fresh test server");
const body = await resp.text();
assertStringIncludes(body, `<html lang="en">`);
assertStringIncludes(body, "index.js");
assertStringIncludes(body, "test.js");
assertStringIncludes(body, "<p>Hello!</p>");
assertStringIncludes(body, "<p>Viewing JIT render.</p>");
assertStringIncludes(
body,
`<script id="__FRSH_PROPS" type="application/json">`,
);
assertStringIncludes(
body,
`"data":[["home","Hello!"]]`,
`props="{&quot;message&quot;:&quot;Hello!&quot;}">`,
);
assertStringIncludes(
body,
@@ -38,7 +34,7 @@ Deno.test("/props/123 page prerender", async () => {
const body = await resp.text();
assertStringIncludes(
body,
`{"props":{"params":{"id":"123"},"url":"https://fresh.deno.dev/props/123","route":"/props/:id"}}`,
`{&quot;params&quot;:{&quot;id&quot;:&quot;123&quot;},&quot;url&quot;:&quot;https://fresh.deno.dev/props/123&quot;,&quot;route&quot;:&quot;/props/:id&quot;}`,
);
});
@@ -48,16 +44,7 @@ Deno.test("/[name] page prerender", async () => {
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, "[name].js");
assertStringIncludes(body, "<div>Hello bar</div>");
assertStringIncludes(
body,
`<script id="__FRSH_PROPS" type="application/json">`,
);
assertStringIncludes(
body,
`"params":{"name":"bar"}`,
);
});
Deno.test("/intercept - GET html", async () => {