Skip to content

Commit

Permalink
fix(scripts): privacy default attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Apr 12, 2024
1 parent 8c69596 commit e2ba513
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion packages/unhead/src/composables/useScript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ScriptNetworkEvents, hashCode } from '@unhead/shared'
import type {
DomRenderTagContext,
Head,
ScriptInstance,
UseScriptInput,
UseScriptOptions,
Expand All @@ -21,6 +22,7 @@ export function useScript<T>(_input: UseScriptInput, _options?: UseScriptOptions
if (!head)
throw new Error('Missing Unhead context.')

const isAbsolute = input.src && (input.src.startsWith('http') || input.src.startsWith('//'))
const id = input.key || hashCode(input.src || (typeof input.innerHTML === 'string' ? input.innerHTML : ''))
const key = `use-script.${id}`
if (head._scripts?.[id])
Expand Down Expand Up @@ -66,9 +68,17 @@ export function useScript<T>(_input: UseScriptInput, _options?: UseScriptOptions
load() {
if (!script.entry) {
syncStatus('loading')
const defaults: Required<Head>['script'][0] = {
defer: true,
fetchpriority: 'low',
}
if (isAbsolute) {
defaults.crossorigin = 'anonymous'
defaults.referrerpolicy = 'no-referrer'
}
// status should get updated from script events
script.entry = head.push({
script: [{ defer: true, fetchpriority: 'low', ...input, key }],
script: [{ ...defaults, ...input, key }],
}, options)
}
return loadPromise
Expand Down
2 changes: 1 addition & 1 deletion test/unhead/dom/useScript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('dom useScript', () => {
expect(await useDelayedSerializedDom()).toMatchInlineSnapshot(`
"<!DOCTYPE html><html><head>
<script data-onload="" data-onerror="" defer="" fetchpriority="low" src="https://cdn.example.com/script.js" data-hid="438d65b"></script></head>
<script data-onload="" data-onerror="" defer="" fetchpriority="low" crossorigin="anonymous" referrerpolicy="no-referrer" src="https://cdn.example.com/script.js" data-hid="438d65b"></script></head>
<body>
<div>
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 @@ -36,7 +36,7 @@ describe('dom useScript', () => {
"bodyAttrs": "",
"bodyTags": "",
"bodyTagsOpen": "",
"headTags": "<script defer fetchpriority="low" src="https://cdn.example.com/script.js" onload="this.dataset.onloadfired = true" onerror="this.dataset.onerrorfired = true" data-hid="438d65b"></script>",
"headTags": "<script defer fetchpriority="low" crossorigin="anonymous" referrerpolicy="no-referrer" src="https://cdn.example.com/script.js" onload="this.dataset.onloadfired = true" onerror="this.dataset.onerrorfired = true" data-hid="438d65b"></script>",
"htmlAttrs": "",
}
`)
Expand Down

0 comments on commit e2ba513

Please sign in to comment.