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
-1
View File
@@ -1 +0,0 @@
export * from "../../server.ts";
+7 -1
View File
@@ -1,8 +1,14 @@
import { h, IS_BROWSER, useState } from "../deps.ts";
import { h, IS_BROWSER, useData } from "../deps.ts";
export default function Home() {
const message = useData("home", (key) => {
return new Promise<string>((resolve) => {
setTimeout(() => resolve("Hello!"), 100);
});
});
return (
<div>
<p>{message}</p>
<p>{IS_BROWSER ? "Viewing browser render." : "Viewing JIT render."}</p>
</div>
);
+15 -2
View File
@@ -11,10 +11,19 @@ Deno.test("/ page prerender", async () => {
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, "index.js");
assertStringIncludes(body, "<p>Hello!</p>");
assertStringIncludes(body, "<p>Viewing JIT render.</p>");
assertStringIncludes(
body,
`<script id="__FRSH_PROPS" type="application/json">{"params":{}}</script>`,
`<script id="__FRSH_PROPS" type="application/json">`,
);
assertStringIncludes(
body,
`"params":{}`,
);
assertStringIncludes(
body,
`"data":[["home","Hello!"]]`,
);
});
@@ -28,7 +37,11 @@ Deno.test("/[name] page prerender", async () => {
assertStringIncludes(body, "<div>Hello foo</div>");
assertStringIncludes(
body,
`<script id="__FRSH_PROPS" type="application/json">{"params":{"name":"foo"}}</script>`,
`<script id="__FRSH_PROPS" type="application/json">`,
);
assertStringIncludes(
body,
`"params":{"name":"foo"}`,
);
});