Files
sweet/example/pages/x/[module].tsx
T
Luca Casonato 56baffa96d 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.
2021-06-11 13:59:55 +02:00

35 lines
848 B
TypeScript

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;
}