feat: a new "island" based architechture (#97)
This commit is contained in:
@@ -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>;
|
||||
}
|
||||
@@ -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>;
|
||||
}
|
||||
@@ -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>;
|
||||
}
|
||||
@@ -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 />
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
},
|
||||
};
|
||||
@@ -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+)",
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
import { HandlerContext } from "../../../server.ts";
|
||||
|
||||
export const handler = {
|
||||
GET(_ctx: HandlerContext) {
|
||||
throw Error("it errored!");
|
||||
},
|
||||
};
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
@@ -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");
|
||||
},
|
||||
};
|
||||
@@ -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");
|
||||
},
|
||||
};
|
||||
@@ -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>;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { h } from "../client_deps.ts";
|
||||
|
||||
export default function StaticPage() {
|
||||
return <p>This is a static page.</p>;
|
||||
}
|
||||
@@ -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*",
|
||||
};
|
||||
Reference in New Issue
Block a user