From f420c9fe2bbfe6b84c72dd8906c6d8c82342fbcb Mon Sep 17 00:00:00 2001 From: Ayo Date: Tue, 10 Oct 2023 23:27:58 +0200 Subject: [PATCH] feat: don't register components if no custom-element in page --- routes/[...index].ts | 23 ++++++++++++++++------- src/pages/404.html | 22 ++++++++++++++++------ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/routes/[...index].ts b/routes/[...index].ts index 8b61487..a2a6cf1 100644 --- a/routes/[...index].ts +++ b/routes/[...index].ts @@ -39,13 +39,22 @@ function insertRegistry(html: string): string { const ast = parse(html); - // insert registry script to head + let hasCustomElements = false; + walkSync(ast, (node) => { - if (node.type === ELEMENT_NODE && node.name === "head") { - node.children.push(parse(registryScript)); + if (node.type === ELEMENT_NODE && node.name.includes("-")) { + hasCustomElements = true; } }); + // insert registry script to head + if (hasCustomElements) + walkSync(ast, (node) => { + if (node.type === ELEMENT_NODE && node.name === "head") { + node.children.push(parse(registryScript)); + } + }); + return renderSync(ast); } @@ -82,10 +91,10 @@ function doSetUp(html: string) { const regex = /{{(.*?)}}/g; var match; - let matches = []; - while ((match = regex.exec(html)) != null) { - console.log(match[0], match[1]); - html = html.replace(match[0], setupMap[match[1].trim()]); + while ((match = regex.exec(html))) { + let [key, value] = match; + value = value.replace(/\s/g, ""); + html = html.replace(key, setupMap[value]); } console.log("---------"); diff --git a/src/pages/404.html b/src/pages/404.html index 9aa023d..2ea680f 100644 --- a/src/pages/404.html +++ b/src/pages/404.html @@ -1,8 +1,18 @@ - + + + + + + not found -{{homeLinkLabel}} + + + + {{homeLinkLabel}} -{{greeting }} wow we can't find that + {{greeting }} wow we can't find that + +