close #1187 ## points - use `<link rel="modulepreload">` tag and `Link: rel="modulepreload"` header to preload dependencies - No preloading at all for pages that don't use JavaScript at all. - Information about preloading is delivered only when the JS file has already been built. If the JS file hasn't finished building, it will respond without preload information instead of waiting for the build and then returning the HTML. ## performance comparison I've deployed this branch to https://fresh-module-preload.deno.dev/ so you can try it out and see how it behaves. When I accessed [fresh.deno.dev](https://fresh.deno.dev/) and [fresh-module-preload.deno.dev](https://fresh-module-preload.deno.dev/) 20 times each and measured the DOMContentLoaded time, it was 24% faster (157.3ms -> 118.95ms) in my environment (Nagano City, Japan). <details> <summary>Actual measured data</summary> (Don't forget to check `Disable Cache` in devtools) | afeter (ms) | before (ms) -- | -- | -- 0 | 180 | 152 1 | 82 | 144 2 | 78 | 186 3 | 156 | 196 4 | 88 | 211 5 | 85 | 123 6 | 162 | 209 7 | 154 | 110 8 | 86 | 175 9 | 135 | 109 10 | 183 | 117 11 | 115 | 159 12 | 99 | 173 13 | 83 | 131 14 | 82 | 142 15 | 135 | 160 16 | 120 | 125 17 | 148 | 152 18 | 118 | 229 19 | 90 | 143 average | 118.95 | 157.3 </details> ## screen shots   --------- Co-authored-by: Luca Casonato <hello@lcas.dev>
418 lines
11 KiB
TypeScript
418 lines
11 KiB
TypeScript
import { renderToString } from "preact-render-to-string";
|
|
import { ComponentChildren, ComponentType, h, options } from "preact";
|
|
import {
|
|
AppModule,
|
|
ErrorPage,
|
|
Island,
|
|
Plugin,
|
|
PluginRenderFunctionResult,
|
|
PluginRenderResult,
|
|
PluginRenderStyleTag,
|
|
RenderFunction,
|
|
Route,
|
|
UnknownPage,
|
|
} from "./types.ts";
|
|
import { HEAD_CONTEXT } from "../runtime/head.ts";
|
|
import { CSP_CONTEXT, nonce, NONE, UNSAFE_INLINE } from "../runtime/csp.ts";
|
|
import { ContentSecurityPolicy } from "../runtime/csp.ts";
|
|
import { bundleAssetUrl } from "./constants.ts";
|
|
import { assetHashingHook } from "../runtime/utils.ts";
|
|
import { htmlEscapeJsonString } from "./htmlescape.ts";
|
|
import { serialize } from "./serializer.ts";
|
|
|
|
export interface RenderOptions<Data> {
|
|
route: Route<Data> | UnknownPage | ErrorPage;
|
|
islands: Island[];
|
|
plugins: Plugin[];
|
|
app: AppModule;
|
|
imports: string[];
|
|
dependencyMap: Map<string, string[]>;
|
|
url: URL;
|
|
params: Record<string, string | string[]>;
|
|
renderFn: RenderFunction;
|
|
data?: Data;
|
|
error?: unknown;
|
|
lang?: string;
|
|
}
|
|
|
|
export type InnerRenderFunction = () => string;
|
|
|
|
export class RenderContext {
|
|
#id: string;
|
|
#state: Map<string, unknown> = new Map();
|
|
#styles: string[] = [];
|
|
#url: URL;
|
|
#route: string;
|
|
#lang: string;
|
|
|
|
constructor(id: string, url: URL, route: string, lang: string) {
|
|
this.#id = id;
|
|
this.#url = url;
|
|
this.#route = route;
|
|
this.#lang = lang;
|
|
}
|
|
|
|
/** A unique ID for this logical JIT render. */
|
|
get id(): string {
|
|
return this.#id;
|
|
}
|
|
|
|
/**
|
|
* State that is persisted between multiple renders with the same render
|
|
* context. This is useful because one logical JIT render could have multiple
|
|
* preact render passes due to suspense.
|
|
*/
|
|
get state(): Map<string, unknown> {
|
|
return this.#state;
|
|
}
|
|
|
|
/**
|
|
* All of the CSS style rules that should be inlined into the document.
|
|
* Adding to this list across multiple renders is supported (even across
|
|
* suspense!). The CSS rules will always be inserted on the client in the
|
|
* order specified here.
|
|
*/
|
|
get styles(): string[] {
|
|
return this.#styles;
|
|
}
|
|
|
|
/** The URL of the page being rendered. */
|
|
get url(): URL {
|
|
return this.#url;
|
|
}
|
|
|
|
/** The route matcher (e.g. /blog/:id) that the request matched for this page
|
|
* to be rendered. */
|
|
get route(): string {
|
|
return this.#route;
|
|
}
|
|
|
|
/** The language of the page being rendered. Defaults to "en". */
|
|
get lang(): string {
|
|
return this.#lang;
|
|
}
|
|
set lang(lang: string) {
|
|
this.#lang = lang;
|
|
}
|
|
}
|
|
|
|
function defaultCsp() {
|
|
return {
|
|
directives: { defaultSrc: [NONE], styleSrc: [UNSAFE_INLINE] },
|
|
reportOnly: false,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* This function renders out a page. Rendering is synchronous and non streaming.
|
|
* Suspense boundaries are not supported.
|
|
*/
|
|
export async function render<Data>(
|
|
opts: RenderOptions<Data>,
|
|
): Promise<[string, ContentSecurityPolicy | undefined]> {
|
|
const props: Record<string, unknown> = {
|
|
params: opts.params,
|
|
url: opts.url,
|
|
route: opts.route.pattern,
|
|
data: opts.data,
|
|
};
|
|
if (opts.error) {
|
|
props.error = opts.error;
|
|
}
|
|
|
|
const csp: ContentSecurityPolicy | undefined = opts.route.csp
|
|
? defaultCsp()
|
|
: undefined;
|
|
const headComponents: ComponentChildren[] = [];
|
|
|
|
const vnode = h(CSP_CONTEXT.Provider, {
|
|
value: csp,
|
|
children: h(HEAD_CONTEXT.Provider, {
|
|
value: headComponents,
|
|
children: h(opts.app.default, {
|
|
params: opts.params as Record<string, string>,
|
|
url: opts.url,
|
|
route: opts.route.pattern,
|
|
data: opts.data,
|
|
Component() {
|
|
return h(opts.route.component! as ComponentType<unknown>, props);
|
|
},
|
|
}),
|
|
}),
|
|
});
|
|
|
|
const ctx = new RenderContext(
|
|
crypto.randomUUID(),
|
|
opts.url,
|
|
opts.route.pattern,
|
|
opts.lang ?? "en",
|
|
);
|
|
|
|
if (csp) {
|
|
// Clear the csp
|
|
const newCsp = defaultCsp();
|
|
csp.directives = newCsp.directives;
|
|
csp.reportOnly = newCsp.reportOnly;
|
|
}
|
|
// Clear the head components
|
|
headComponents.splice(0, headComponents.length);
|
|
|
|
// Setup the interesting VNode types
|
|
ISLANDS.splice(0, ISLANDS.length, ...opts.islands);
|
|
|
|
// Clear the encountered vnodes
|
|
ENCOUNTERED_ISLANDS.clear();
|
|
|
|
// Clear the island props
|
|
ISLAND_PROPS = [];
|
|
|
|
let bodyHtml: string | null = null;
|
|
|
|
function realRender(): string {
|
|
bodyHtml = renderToString(vnode);
|
|
return bodyHtml;
|
|
}
|
|
|
|
const plugins = opts.plugins.filter((p) => p.render !== null);
|
|
const renderResults: [Plugin, PluginRenderResult][] = [];
|
|
|
|
function render(): PluginRenderFunctionResult {
|
|
const plugin = plugins.shift();
|
|
if (plugin) {
|
|
const res = plugin.render!({ render });
|
|
if (res === undefined) {
|
|
throw new Error(
|
|
`${plugin?.name}'s render hook did not return a PluginRenderResult object.`,
|
|
);
|
|
}
|
|
renderResults.push([plugin, res]);
|
|
} else {
|
|
realRender();
|
|
}
|
|
if (bodyHtml === null) {
|
|
throw new Error(
|
|
`The 'render' function was not called by ${plugin?.name}'s render hook.`,
|
|
);
|
|
}
|
|
return {
|
|
htmlText: bodyHtml,
|
|
requiresHydration: ENCOUNTERED_ISLANDS.size > 0,
|
|
};
|
|
}
|
|
|
|
await opts.renderFn(ctx, () => render().htmlText);
|
|
|
|
if (bodyHtml === null) {
|
|
throw new Error("The `render` function was not called by the renderer.");
|
|
}
|
|
|
|
bodyHtml = bodyHtml as string;
|
|
|
|
const imports: [string, string][] = [];
|
|
const preloadSet = new Set<string>();
|
|
function addImport(path: string): string {
|
|
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
|
if (csp) {
|
|
csp.directives.scriptSrc = [
|
|
...csp.directives.scriptSrc ?? [],
|
|
nonce(randomNonce),
|
|
];
|
|
}
|
|
const url = bundleAssetUrl(path);
|
|
imports.push([url, randomNonce]);
|
|
preloadSet.add(url);
|
|
for (const depPath of opts.dependencyMap.get(path) ?? []) {
|
|
const url = bundleAssetUrl(depPath);
|
|
preloadSet.add(url);
|
|
}
|
|
return url;
|
|
}
|
|
|
|
for (const url of opts.imports) {
|
|
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
|
if (csp) {
|
|
csp.directives.scriptSrc = [
|
|
...csp.directives.scriptSrc ?? [],
|
|
nonce(randomNonce),
|
|
];
|
|
}
|
|
imports.push([url, randomNonce]);
|
|
}
|
|
|
|
const state: [islands: unknown[], plugins: unknown[]] = [ISLAND_PROPS, []];
|
|
const styleTags: PluginRenderStyleTag[] = [];
|
|
const pluginScripts: [string, string, number][] = [];
|
|
|
|
for (const [plugin, res] of renderResults) {
|
|
for (const hydrate of res.scripts ?? []) {
|
|
const i = state[1].push(hydrate.state) - 1;
|
|
pluginScripts.push([plugin.name, hydrate.entrypoint, i]);
|
|
}
|
|
styleTags.splice(styleTags.length, 0, ...res.styles ?? []);
|
|
}
|
|
|
|
// The inline script that will hydrate the page.
|
|
let script = "";
|
|
|
|
// Serialize the state into the <script id=__FRSH_STATE> tag and generate the
|
|
// inline script to deserialize it. This script starts by deserializing the
|
|
// state in the tag. This potentially requires importing @preact/signals.
|
|
if (state[0].length > 0 || state[1].length > 0) {
|
|
const res = serialize(state);
|
|
const escapedState = htmlEscapeJsonString(res.serialized);
|
|
bodyHtml +=
|
|
`<script id="__FRSH_STATE" type="application/json">${escapedState}</script>`;
|
|
|
|
if (res.requiresDeserializer) {
|
|
const url = addImport("/deserializer.js");
|
|
script += `import { deserialize } from "${url}";`;
|
|
}
|
|
if (res.hasSignals) {
|
|
const url = addImport("/signals.js");
|
|
script += `import { signal } from "${url}";`;
|
|
}
|
|
script += `const ST = document.getElementById("__FRSH_STATE").textContent;`;
|
|
script += `const STATE = `;
|
|
if (res.requiresDeserializer) {
|
|
if (res.hasSignals) {
|
|
script += `deserialize(ST, signal);`;
|
|
} else {
|
|
script += `deserialize(ST);`;
|
|
}
|
|
} else {
|
|
script += `JSON.parse(ST).v;`;
|
|
}
|
|
}
|
|
|
|
// Then it imports all plugin scripts and executes them (with their respective
|
|
// state).
|
|
for (const [pluginName, entrypoint, i] of pluginScripts) {
|
|
const url = addImport(`/plugin-${pluginName}-${entrypoint}.js`);
|
|
script += `import p${i} from "${url}";p${i}(STATE[1][${i}]);`;
|
|
}
|
|
|
|
// Finally, it loads all island scripts and hydrates the islands using the
|
|
// reviver from the "main" script.
|
|
if (ENCOUNTERED_ISLANDS.size > 0) {
|
|
// Load the main.js script
|
|
const url = addImport("/main.js");
|
|
script += `import { revive } from "${url}";`;
|
|
|
|
// Prepare the inline script that loads and revives the islands
|
|
let islandRegistry = "";
|
|
for (const island of ENCOUNTERED_ISLANDS) {
|
|
const url = addImport(`/island-${island.id}.js`);
|
|
script += `import ${island.name} from "${url}";`;
|
|
islandRegistry += `${island.id}:${island.name},`;
|
|
}
|
|
script += `revive({${islandRegistry}}, STATE[0]);`;
|
|
}
|
|
|
|
// Append the inline script.
|
|
if (script !== "") {
|
|
const randomNonce = crypto.randomUUID().replace(/-/g, "");
|
|
if (csp) {
|
|
csp.directives.scriptSrc = [
|
|
...csp.directives.scriptSrc ?? [],
|
|
nonce(randomNonce),
|
|
];
|
|
}
|
|
bodyHtml +=
|
|
`<script type="module" nonce="${randomNonce}">${script}</script>`;
|
|
}
|
|
|
|
if (ctx.styles.length > 0) {
|
|
const node = h("style", {
|
|
id: "__FRSH_STYLE",
|
|
dangerouslySetInnerHTML: { __html: ctx.styles.join("\n") },
|
|
});
|
|
headComponents.splice(0, 0, node);
|
|
}
|
|
|
|
for (const style of styleTags) {
|
|
const node = h("style", {
|
|
id: style.id,
|
|
dangerouslySetInnerHTML: { __html: style.cssText },
|
|
media: style.media,
|
|
});
|
|
headComponents.splice(0, 0, node);
|
|
}
|
|
|
|
const preloads = [...preloadSet];
|
|
const html = template({
|
|
bodyHtml,
|
|
headComponents,
|
|
imports,
|
|
preloads,
|
|
lang: ctx.lang,
|
|
});
|
|
|
|
return [html, csp];
|
|
}
|
|
|
|
export interface TemplateOptions {
|
|
bodyHtml: string;
|
|
headComponents: ComponentChildren[];
|
|
imports: (readonly [string, string])[];
|
|
preloads: string[];
|
|
lang: string;
|
|
}
|
|
|
|
export function template(opts: TemplateOptions): string {
|
|
const page = h(
|
|
"html",
|
|
{ lang: opts.lang },
|
|
h(
|
|
"head",
|
|
null,
|
|
h("meta", { charSet: "UTF-8" }),
|
|
h("meta", {
|
|
name: "viewport",
|
|
content: "width=device-width, initial-scale=1.0",
|
|
}),
|
|
opts.preloads.map((src) =>
|
|
h("link", { rel: "modulepreload", href: src })
|
|
),
|
|
opts.imports.map(([src, nonce]) =>
|
|
h("script", { src: src, nonce: nonce, type: "module" })
|
|
),
|
|
opts.headComponents,
|
|
),
|
|
h("body", { dangerouslySetInnerHTML: { __html: opts.bodyHtml } }),
|
|
);
|
|
return "<!DOCTYPE html>" + renderToString(page);
|
|
}
|
|
|
|
// Set up a preact option hook to track when vnode with custom functions are
|
|
// created.
|
|
const ISLANDS: Island[] = [];
|
|
const ENCOUNTERED_ISLANDS: Set<Island> = new Set([]);
|
|
let ISLAND_PROPS: unknown[] = [];
|
|
const originalHook = options.vnode;
|
|
let ignoreNext = false;
|
|
options.vnode = (vnode) => {
|
|
assetHashingHook(vnode);
|
|
const originalType = vnode.type as ComponentType<unknown>;
|
|
if (typeof vnode.type === "function") {
|
|
const island = ISLANDS.find((island) => island.component === originalType);
|
|
if (island) {
|
|
if (ignoreNext) {
|
|
ignoreNext = false;
|
|
return;
|
|
}
|
|
ENCOUNTERED_ISLANDS.add(island);
|
|
vnode.type = (props) => {
|
|
ignoreNext = true;
|
|
const child = h(originalType, props);
|
|
ISLAND_PROPS.push(props);
|
|
return h(
|
|
`!--frsh-${island.id}:${ISLAND_PROPS.length - 1}--`,
|
|
null,
|
|
child,
|
|
);
|
|
};
|
|
}
|
|
}
|
|
if (originalHook) originalHook(vnode);
|
|
};
|