From ff92b73d8f7b7a76aa0a0e1a12891c9fd3f884d1 Mon Sep 17 00:00:00 2001 From: Ayo Date: Thu, 19 Oct 2023 22:59:34 +0200 Subject: [PATCH] perf: give offering to lighthouse --- public/robots.txt | 2 ++ routes/[...index].ts | 28 +++++++++++++++------------- src/components/my-head.html | 20 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 public/robots.txt diff --git a/public/robots.txt b/public/robots.txt new file mode 100644 index 0000000..eb05362 --- /dev/null +++ b/public/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: diff --git a/routes/[...index].ts b/routes/[...index].ts index 3037501..2ea8308 100644 --- a/routes/[...index].ts +++ b/routes/[...index].ts @@ -10,6 +10,7 @@ import { renderSync, walkSync, } from "ultrahtml"; +import { querySelectorAll } from "ultrahtml/selector"; import { parseScript } from "esprima"; import config from "../mcfly.config"; @@ -74,7 +75,6 @@ async function insertRegistry( // insert registry script to head if (usedCustomElements.length > 0) { - console.log(">>> usedCustomElements", usedCustomElements); const registryScript = await buildRegistry(usedCustomElements, type); walkSync(ast, (node) => { if (node.type === ELEMENT_NODE && node.name === "head") { @@ -217,28 +217,30 @@ function removeComments(script: string) { async function useFragments(html: string) { const fragmentFiles = await getFiles("html"); - const availableFragments = fragmentFiles.map((key) => { + const availableFragments = fragmentFiles.reduce((acc, key) => { return { - key: key.replace(".html", ""), - text: "", + ...acc, + [key.replace(".html", "")]: "", }; - }); + }, {}); const ast = parse(html); - for (const fragment of availableFragments) { - fragment.text = await useStorage().getItem( - "assets:components:" + fragment.key + ".html" + // building fragment map + for (const key in availableFragments) { + let text: string = await useStorage().getItem( + "assets:components:" + key + ".html" ); + availableFragments[key] = text.replace(/\n/g, "").replace(/\s+/g, " "); } walkSync(ast, (node) => { - const usedFragment = availableFragments.find( - (fragment) => fragment.key === node.name + const selector = Object.keys(availableFragments).find( + (name) => name === node.name ); - if (node.type === ELEMENT_NODE && !!usedFragment) { - const value = replaceSlots(usedFragment.text, node); - node.children.push(parse(value)); + if (node.type === ELEMENT_NODE && !!selector) { + const index = node.parent.children.indexOf(node); + node.parent.children[index] = parse(availableFragments[selector]); } }); diff --git a/src/components/my-head.html b/src/components/my-head.html index 046e334..3252e36 100644 --- a/src/components/my-head.html +++ b/src/components/my-head.html @@ -3,5 +3,25 @@ McFly: Back to the Basics. Into the Future. + + + + + + + + + + +