feat: add support for partial <head> merging (#1863)

This commit is contained in:
Marvin Hagemeister
2023-10-05 12:04:38 +02:00
committed by GitHub
parent fd4a2b99ef
commit b3a3bd1069
8 changed files with 297 additions and 104 deletions
+60 -1
View File
@@ -10,7 +10,7 @@ import {
render,
VNode,
} from "preact";
import { assetHashingHook } from "../utils.ts";
import { assetHashingHook, INTERNAL_PREFIX } from "../utils.ts";
import { type SerializedState } from "../../server/rendering/fresh_tags.tsx";
import type { Signal } from "@preact/signals";
import {
@@ -711,6 +711,65 @@ export async function applyPartials(res: Response): Promise<void> {
);
}
// Update <head>
document.title = doc.title;
// Needs to be converted to an array otherwise somehow <link>-tags
// are missing.
Array.from(doc.head.childNodes).forEach((childNode) => {
const child = childNode as HTMLElement;
if (child.nodeName === "TITLE") return;
if (child.nodeName === "META") {
const meta = child as HTMLMetaElement;
// Ignore charset which is usually set site wide anyway
if (meta.hasAttribute("charset")) return;
const name = meta.name;
if (name !== "") {
const existing = document.head.querySelector(`meta[name="${name}"]`) as
| HTMLMetaElement
| null;
if (existing !== null) {
if (existing.content !== meta.content) {
existing.content = meta.content;
}
} else {
document.head.appendChild(meta);
}
} else {
const property = child.getAttribute("property");
const existing = document.head.querySelector(
`meta[property="${property}"]`,
) as HTMLMetaElement | null;
if (existing !== null) {
if (existing.content !== meta.content) {
existing.content = meta.content;
}
} else {
document.head.appendChild(meta);
}
}
} else if (child.nodeName === "LINK") {
const link = child as HTMLLinkElement;
if (link.rel === "modulepreload") return;
if (link.rel === "stylesheet") {
if (document.head.querySelector(`link[href="${link.href}"]`) === null) {
document.head.appendChild(link);
}
}
} else if (child.nodeName === "SCRIPT") {
const script = child as HTMLScriptElement;
if (script.src === `${INTERNAL_PREFIX}/refresh.js`) return;
// TODO: What to do with script tags?
} else if (child.nodeName === "STYLE") {
const style = child as HTMLStyleElement;
// TODO: Do we need a smarter merging strategy?
document.head.appendChild(style);
}
});
// Update all encountered partials
for (let i = 0; i < encounteredPartials.length; i++) {
const { vnode, marker } = encounteredPartials[i];