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:
@@ -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>;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -3,14 +3,16 @@
|
||||
// To update this file, run `fresh routes`.
|
||||
|
||||
import * as $0 from "./pages/api/joke.ts";
|
||||
import * as $1 from "./pages/[name].tsx";
|
||||
import * as $2 from "./pages/index.tsx";
|
||||
import * as $1 from "./pages/x/[module].tsx";
|
||||
import * as $2 from "./pages/[name].tsx";
|
||||
import * as $3 from "./pages/index.tsx";
|
||||
|
||||
const routes = {
|
||||
pages: {
|
||||
"./pages/api/joke.ts": $0,
|
||||
"./pages/[name].tsx": $1,
|
||||
"./pages/index.tsx": $2,
|
||||
"./pages/x/[module].tsx": $1,
|
||||
"./pages/[name].tsx": $2,
|
||||
"./pages/index.tsx": $3,
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user