feat: support suspending during SSR and useData (#27)

This commit adds support for suspending during JIT SSR rendering.
Additionally this PR adds `useData` support. This hook is triggered
during JIT render and can be used to fetch data. This data is sent to
the client and returned there from calls to useData too.
This commit is contained in:
Luca Casonato
2021-06-11 13:59:55 +02:00
committed by GitHub
parent ae1e311eb8
commit 56baffa96d
13 changed files with 226 additions and 53 deletions
+8 -2
View File
@@ -17,10 +17,16 @@ function Counter() {
const [count, setCount] = useState(0);
return <div>
<p>{count}</p>
<button onClick={() => setCount(count - 1)} disabled={!IS_BROWSER}>
<button
onClick={() => setCount(count - 1)}
disabled={!IS_BROWSER}
>
-1
</button>
<button onClick={() => setCount(count + 1)} disabled={!IS_BROWSER}>
<button
onClick={() => setCount(count + 1)}
disabled={!IS_BROWSER}
>
+1
</button>
</div>;
+34
View File
@@ -0,0 +1,34 @@
import { h, PageProps, useData } from "../../deps.ts";
export default function ModuleInfoPage({ params }: PageProps) {
const module = params.module;
if (module.includes("/")) {
return <div>Invalid module name.</div>;
}
const url = `https://cdn.deno.land/${params.module}/meta/versions.json`;
const info = useData(url, fetcher);
if (info === null) {
return <div>No module found with name `{params.module}`.</div>;
}
const { latest, versions } = info;
return <p>
The module `{params.module}` has {versions.length} versions, with {latest}
{" "}
being the most recent.
</p>;
}
interface ModuleInfo {
latest: string;
versions: string[];
}
async function fetcher(url: string): Promise<ModuleInfo | null> {
const resp = await fetch(url);
if (resp.status === 200) return resp.json();
return null;
}