From 90a51b9da6e3a3c4ee23d9461fd93ba1f23275ac Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Thu, 26 Oct 2023 10:41:39 +0900 Subject: [PATCH] fix(useScript): dedupe early connections, server-side only --- docs/content/1.usage/2.composables/4.use-script.md | 6 ++++++ packages/unhead/src/composables/useScript.ts | 5 +++-- test/unhead/ssr/useScript.test.ts | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/content/1.usage/2.composables/4.use-script.md b/docs/content/1.usage/2.composables/4.use-script.md index a7a82dec..5e6a8e0e 100644 --- a/docs/content/1.usage/2.composables/4.use-script.md +++ b/docs/content/1.usage/2.composables/4.use-script.md @@ -114,6 +114,12 @@ useScript({ ### options +- `skipEarlyConnections` + +Used to skip early connections such as `dns-prefetch` and `preconnect`. + +Useful when you're loading a script from your own domain. + - `use` A function that resolves the scripts API. This is only called client-side. diff --git a/packages/unhead/src/composables/useScript.ts b/packages/unhead/src/composables/useScript.ts index 8501c4d1..fcbd337f 100644 --- a/packages/unhead/src/composables/useScript.ts +++ b/packages/unhead/src/composables/useScript.ts @@ -36,9 +36,10 @@ export function useScript(input: UseScriptInput, _options?: UseScriptOptions< !head!.ssr ) return + const key = `use-script.${id}.early-connection` head!.push({ - link: [{ rel, href: new URL(input.src).origin }] - }) + link: [{ key, rel, href: new URL(input.src).origin }] + }, { mode: 'server' }) } const script: ScriptInstance = { diff --git a/test/unhead/ssr/useScript.test.ts b/test/unhead/ssr/useScript.test.ts index f1b0a053..49bfaba3 100644 --- a/test/unhead/ssr/useScript.test.ts +++ b/test/unhead/ssr/useScript.test.ts @@ -16,7 +16,7 @@ describe('dom useScript', () => { "bodyAttrs": "", "bodyTags": "", "bodyTagsOpen": "", - "headTags": " + "headTags": " ", "htmlAttrs": "", }