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
+11
View File
@@ -0,0 +1,11 @@
/** @jsx h */
import { h } from "../client_deps.ts";
interface Props {
params: Record<string, string | string[]>;
}
export default function Greet(props: Props) {
return <div>Hello {props.params.name}</div>;
}
+7
View File
@@ -0,0 +1,7 @@
/** @jsx h */
import { h, UnknownPageProps } from "../client_deps.ts";
export default function NotFoundPage({ url }: UnknownPageProps) {
return <p>404 not found: {url.pathname}</p>;
}
+7
View File
@@ -0,0 +1,7 @@
/** @jsx h */
import { ErrorPageProps, h } from "../client_deps.ts";
export default function Error500Page({ error }: ErrorPageProps) {
return <p>500 internal error: {(error as Error).message}</p>;
}
+15
View File
@@ -0,0 +1,15 @@
/** @jsx h */
/** @jsxFrag Fragment */
import { AppProps, Fragment, h, Head } from "../client_deps.ts";
export default function App(props: AppProps) {
return (
<>
<Head>
<meta name="description" content="Hello world!" />
</Head>
<props.Component />
</>
);
}
+5
View File
@@ -0,0 +1,5 @@
export async function handler(_req: Request, handle: () => Promise<Response>) {
const resp = await handle();
resp.headers.set("server", "fresh test server");
return resp;
}
+8
View File
@@ -0,0 +1,8 @@
import { RenderContext, RenderFn } from "../../../server.ts";
export function render(_ctx: RenderContext, render: RenderFn) {
const body = render();
if (typeof body !== "string") {
throw new Error("body is missing");
}
}
+12
View File
@@ -0,0 +1,12 @@
export const handler = {
GET() {
return new Response("Get fresh!", {
headers: {
"Content-Type": "application/json; charset=utf-8",
},
});
},
NOTAMETHOD() {
throw new Error("unreachable");
},
};
+11
View File
@@ -0,0 +1,11 @@
/** @jsx h */
import { h, PageConfig, PageProps } from "../../client_deps.ts";
export default function Page(props: PageProps) {
return <div>Book {props.params.id}</div>;
}
export const config: PageConfig = {
routeOverride: "/books/:id(\\d+)",
};
+7
View File
@@ -0,0 +1,7 @@
import { HandlerContext } from "../../../server.ts";
export const handler = {
GET(_ctx: HandlerContext) {
throw Error("it errored!");
},
};
+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>
);
}
+21
View File
@@ -0,0 +1,21 @@
/** @jsx h */
import { h } from "../client_deps.ts";
import { HandlerContext } from "../../../server.ts";
export default function Page() {
return <div>This is HTML</div>;
}
export const handler = {
GET({ req, render }: HandlerContext) {
if (req.headers.get("accept")?.includes("text/html")) {
return render();
} else {
return new Response("This is plain text");
}
},
POST() {
return new Response("POST response");
},
};
+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>;
}
+7
View File
@@ -0,0 +1,7 @@
/** @jsx h */
import { h } from "../client_deps.ts";
export default function StaticPage() {
return <p>This is a static page.</p>;
}
+15
View File
@@ -0,0 +1,15 @@
/** @jsx h */
import { h, PageConfig, PageProps } from "../client_deps.ts";
export default function WildcardPage({ params }: PageProps) {
if (typeof params.path === "string") {
return <p>{params.path}</p>;
} else {
return <p>Not a string.</p>;
}
}
export const config: PageConfig = {
routeOverride: "/foo/:path*",
};