From 1c66d1a8083f9f2b1e4181ff9a8050e23b34c057 Mon Sep 17 00:00:00 2001 From: Wesley Date: Fri, 10 Jan 2025 14:15:26 +1300 Subject: [PATCH] https://github.com/itsmatteomanf/astro-components/issues/40 --- .../astro-early-hints/src/middleware/index.ts | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/astro-early-hints/src/middleware/index.ts b/packages/astro-early-hints/src/middleware/index.ts index 24593eb..04bc436 100644 --- a/packages/astro-early-hints/src/middleware/index.ts +++ b/packages/astro-early-hints/src/middleware/index.ts @@ -10,19 +10,23 @@ export const onRequest = defineMiddleware(async (_, next) => { const root = parse(await response.text()); const head = root.querySelector("head")!; - root.querySelectorAll('link[rel="stylesheet"]').map((style) => { - head.insertAdjacentHTML( - "afterbegin", - ``, - ); - }); + root.querySelectorAll('link[rel="stylesheet"]') + .filter((style) => style.hasAttribute("href") && style.getAttribute("href")) + .map((style) => { + head.insertAdjacentHTML( + "afterbegin", + ``, + ); + }); - root.querySelectorAll('script[type="module"]').map((script) => { - head.insertAdjacentHTML( - "afterbegin", - ``, - ); - }); + root.querySelectorAll('script[type="module"]') + .filter((script) => script.hasAttribute("src") && script.getAttribute("src")) + .map((script) => { + head.insertAdjacentHTML( + "afterbegin", + ``, + ); + }); return new Response(root.toString(), response); });