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:
@@ -1 +0,0 @@
|
||||
export * from "../../server.ts";
|
||||
@@ -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
@@ -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"}`,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user