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:
+11
-3
@@ -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" };
|
||||
|
||||
@@ -7,7 +7,7 @@ import { INTERNAL_PREFIX } from "./constants.ts";
|
||||
import { JS_PREFIX } from "./constants.ts";
|
||||
import { BUILD_ID } from "./constants.ts";
|
||||
import { ApiRoute, Page } from "./types.ts";
|
||||
import { render } from "./render.ts";
|
||||
import { render } from "./render.tsx";
|
||||
|
||||
export class ServerContext {
|
||||
#pages: Page[];
|
||||
@@ -77,11 +77,11 @@ export class ServerContext {
|
||||
for (const page of this.#pages) {
|
||||
const bundlePath = `/${page.name}.js`;
|
||||
const imports = [bundleAssetUrl(bundlePath)];
|
||||
routes[page.route] = (_, match) => {
|
||||
routes[page.route] = async (_, match) => {
|
||||
const preloads = this.#bundler.getPreloads(bundlePath).map(
|
||||
bundleAssetUrl,
|
||||
);
|
||||
const body = render({
|
||||
const body = await render({
|
||||
page,
|
||||
imports,
|
||||
preloads,
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
import { renderToString } from "./deps.ts";
|
||||
import * as rt from "../runtime/deps.ts";
|
||||
import { Page } from "./types.ts";
|
||||
|
||||
export interface RenderOptions {
|
||||
page: Page;
|
||||
imports: string[];
|
||||
preloads: string[];
|
||||
params: Record<string, string>;
|
||||
}
|
||||
|
||||
export function render(options: RenderOptions): string {
|
||||
const props = { params: options.params };
|
||||
const propsStr = JSON.stringify(props);
|
||||
const body = renderToString(rt.h(options.page.component, props));
|
||||
return `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
${
|
||||
options.preloads.map((src) => `<link rel="modulepreload" href="${src}">`)
|
||||
.join(
|
||||
"\n ",
|
||||
)
|
||||
}
|
||||
${
|
||||
options.imports.map((src) => `<script src="${src}" type="module"></script>`)
|
||||
.join(
|
||||
"\n ",
|
||||
)
|
||||
}
|
||||
</head>
|
||||
<body>
|
||||
${body}
|
||||
<script id="__FRSH_PROPS" type="application/json">${propsStr}</script>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import { renderToString } from "./deps.ts";
|
||||
import { h } from "../runtime/deps.ts";
|
||||
import { DATA_CONTEXT } from "../runtime/hooks.ts";
|
||||
import { Page } from "./types.ts";
|
||||
|
||||
export interface RenderOptions {
|
||||
page: Page;
|
||||
imports: string[];
|
||||
preloads: string[];
|
||||
params: Record<string, string>;
|
||||
}
|
||||
|
||||
const MAX_SUSPENSE_DEPTH = 10;
|
||||
|
||||
export async function render(opts: RenderOptions): Promise<string> {
|
||||
const props = { params: opts.params };
|
||||
|
||||
const dataCache = new Map();
|
||||
|
||||
const vnode = h(DATA_CONTEXT.Provider, {
|
||||
value: dataCache,
|
||||
children: h(opts.page.component, props),
|
||||
});
|
||||
|
||||
let suspended = 0;
|
||||
const render = (): string | Promise<string> => {
|
||||
if (++suspended > MAX_SUSPENSE_DEPTH) {
|
||||
throw new Error(
|
||||
`Reached maximum suspense depth of ${MAX_SUSPENSE_DEPTH}.`,
|
||||
);
|
||||
}
|
||||
try {
|
||||
return renderToString(vnode);
|
||||
} catch (e) {
|
||||
if (e && e.then) return e.then(render);
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
const bodyHtml = await render();
|
||||
|
||||
const html = template({
|
||||
bodyHtml,
|
||||
imports: opts.imports,
|
||||
preloads: opts.preloads,
|
||||
props: { params: opts.params, data: [...dataCache.entries()] },
|
||||
});
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
export interface TemplateOptions {
|
||||
bodyHtml: string;
|
||||
imports: string[];
|
||||
preloads: string[];
|
||||
props: unknown;
|
||||
}
|
||||
|
||||
export function template(opts: TemplateOptions): string {
|
||||
const props = JSON.stringify(opts.props);
|
||||
|
||||
const page = (
|
||||
<html>
|
||||
<head>
|
||||
{opts.preloads.map((src) => <link rel="modulepreload" href={src} />)}
|
||||
{opts.imports.map((src) => <script src={src} type="module"></script>)}
|
||||
</head>
|
||||
<body>
|
||||
<div dangerouslySetInnerHTML={{ __html: opts.bodyHtml }} id="__FRSH" />
|
||||
<script
|
||||
id="__FRSH_PROPS"
|
||||
type="application/json"
|
||||
dangerouslySetInnerHTML={{ __html: props }}
|
||||
/>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
|
||||
return "<!DOCTYPE html>" + renderToString(page);
|
||||
}
|
||||
Reference in New Issue
Block a user