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
+11 -3
View File
@@ -119,11 +119,19 @@ function freshPlugin(pageList: Page[]): esbuild.Plugin {
if (!page) return null;
const contents = `
import Page from "${page.url}";
import { h, render } from "${runtime.href}";
import { h, render, DATA_CONTEXT } from "${runtime.href}";
addEventListener("DOMContentLoaded", () => {
const props = JSON.parse(document.getElementById("__FRSH_PROPS").textContent);
render(h(Page, props), document.body);
const { params, data } = JSON.parse(document.getElementById("__FRSH_PROPS").textContent);
try {
render(h(DATA_CONTEXT.Provider, { value: new Map(data) }, h(Page, { params })), document.getElementById("__FRSH"));
} catch(err) {
if (err instanceof Promise) {
console.error("Render tried to suspend without a suspense boundary.");
} else {
console.error("Render threw an error:", err);
}
}
});
`;
return { contents, loader: "js" };