From b286fe81becc73c3af9ba28c855912859d9051fb Mon Sep 17 00:00:00 2001 From: Ayo Date: Tue, 10 Oct 2023 23:15:16 +0200 Subject: [PATCH] refactor: registry insertion --- routes/[...index].ts | 14 ++++++++++++-- src/pages/404.html | 8 ++++++-- src/pages/index.html | 1 + 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/routes/[...index].ts b/routes/[...index].ts index 6899fbf..8b61487 100644 --- a/routes/[...index].ts +++ b/routes/[...index].ts @@ -14,7 +14,7 @@ export default eventHandler(async (event) => { if (!html) html = await useStorage().getItem(getPath("/404.html")); // transforms - const transforms = [insertRegistry, doSetUp, deleteServerScripts]; + const transforms = [doSetUp, deleteServerScripts, insertRegistry]; if (html) { for (const transform of transforms) { html = transform(html.toString()); @@ -37,7 +37,16 @@ function insertRegistry(html: string): string { const registryScript = ''; - return html.toString().replace("", registryScript + ""); + const ast = parse(html); + + // insert registry script to head + walkSync(ast, (node) => { + if (node.type === ELEMENT_NODE && node.name === "head") { + node.children.push(parse(registryScript)); + } + }); + + return renderSync(ast); } function doSetUp(html: string) { @@ -78,6 +87,7 @@ function doSetUp(html: string) { console.log(match[0], match[1]); html = html.replace(match[0], setupMap[match[1].trim()]); } + console.log("---------"); return html; } diff --git a/src/pages/404.html b/src/pages/404.html index 138460b..9aa023d 100644 --- a/src/pages/404.html +++ b/src/pages/404.html @@ -1,4 +1,8 @@ -{{greeting}}!! wow we can't find that + +{{homeLinkLabel}} + +{{greeting }} wow we can't find that diff --git a/src/pages/index.html b/src/pages/index.html index a11a06f..d084195 100644 --- a/src/pages/index.html +++ b/src/pages/index.html @@ -16,6 +16,7 @@ {{ count }} + {{greeting}} hey