Skip to content

Commit

Permalink
fix(useScript): dedupe early connections, server-side only
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Oct 26, 2023
1 parent b6bb423 commit 90a51b9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/content/1.usage/2.composables/4.use-script.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions packages/unhead/src/composables/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export function useScript<T>(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<T> = {
Expand Down
2 changes: 1 addition & 1 deletion test/unhead/ssr/useScript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('dom useScript', () => {
"bodyAttrs": "",
"bodyTags": "",
"bodyTagsOpen": "",
"headTags": "<link rel=\\"preconnect\\" href=\\"https://cdn.example.com\\">
"headTags": "<link rel=\\"preconnect\\" href=\\"https://cdn.example.com\\" data-hid=\\"7b546a7\\">
<script async fetchpriority=\\"low\\" src=\\"https://cdn.example.com/script.js\\" onload=\\"this.dataset.onload = true\\" onerror=\\"this.dataset.onerror = true\\" onabort=\\"this.dataset.onabort = true\\" onprogress=\\"this.dataset.onprogress = true\\" onloadstart=\\"this.dataset.onloadstart = true\\" data-hid=\\"438d65b\\"></script>",
"htmlAttrs": "",
}
Expand Down

0 comments on commit 90a51b9

Please sign in to comment.