Allow plugin render hooks to add link tags to the page (#1963)

Adds an extra optional property to plugin render results to allow
plugins to add `<link>` tags to the page.
This commit is contained in:
Adam Gregory
2023-11-08 23:51:14 +01:00
committed by GitHub
parent 1b767233f4
commit 14cecc0a6f
6 changed files with 52 additions and 2 deletions
+13 -1
View File
@@ -2,7 +2,12 @@ import { bundleAssetUrl } from "../constants.ts";
import { RenderState } from "./state.ts";
import { htmlEscapeJsonString } from "../htmlescape.ts";
import { serialize } from "../serializer.ts";
import { Plugin, PluginRenderResult, PluginRenderStyleTag } from "../types.ts";
import {
Plugin,
PluginRenderLink,
PluginRenderResult,
PluginRenderStyleTag,
} from "../types.ts";
import { ContentSecurityPolicy, nonce } from "../../runtime/csp.ts";
import { h } from "preact";
@@ -52,6 +57,7 @@ export function renderFreshTags(
[],
];
const styleTags: PluginRenderStyleTag[] = [];
const linkTags: PluginRenderLink[] = [];
const pluginScripts: [string, string, number][] = [];
for (const [plugin, res] of opts.pluginRenderResults) {
@@ -60,6 +66,7 @@ export function renderFreshTags(
pluginScripts.push([plugin.name, hydrate.entrypoint, i]);
}
styleTags.splice(styleTags.length, 0, ...res.styles ?? []);
linkTags.splice(linkTags.length, 0, ...res.links ?? []);
}
// The inline script that will hydrate the page.
@@ -180,5 +187,10 @@ export function renderFreshTags(
renderState.headVNodes.splice(0, 0, node);
}
for (const link of linkTags) {
const node = h("link", link);
renderState.headVNodes.splice(0, 0, node);
}
return { bodyHtml: opts.bodyHtml, preloadSet, moduleScripts };
}