Skip to content

Commit 2ab26d4

Browse files
committedMar 8, 2025·
fix(scripts): respect user privacy overrides
1 parent d97d26e commit 2ab26d4

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed
 

‎packages/unhead/src/scripts/useScript.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
144144
const link: RawInput<'link'> = {
145145
href,
146146
rel,
147-
crossorigin: input.crossorigin || isCrossOrigin ? 'anonymous' : undefined,
148-
referrerpolicy: input.referrerpolicy || isCrossOrigin ? 'no-referrer' : undefined,
149-
fetchpriority: input.fetchpriority || 'low',
147+
crossorigin: typeof input.crossorigin !== 'undefined' ? input.crossorigin : (isCrossOrigin ? 'anonymous' : undefined),
148+
referrerpolicy: typeof input.referrerpolicy !== 'undefined' ? input.referrerpolicy : (isCrossOrigin ? 'no-referrer' : undefined),
149+
fetchpriority: typeof input.fetchpriority !== 'undefined' ? input.fetchpriority : 'low',
150150
integrity: input.integrity,
151151
as: rel === 'preload' ? 'script' : undefined,
152152
}

‎packages/vue/test/unit/useScript.test.ts

+49
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { createHead } from '@unhead/vue/client'
2+
import { renderSSRHead } from '@unhead/vue/server'
3+
24
import { describe, it } from 'vitest'
35
import { ref, watch } from 'vue'
46
import { useDom } from '../../../unhead/test/util'
57
import { useScript } from '../../src/scripts/useScript'
8+
import { createHeadCore } from '../../src'
69

710
describe('vue e2e scripts', () => {
811
it('multiple active promise handles', async () => {
@@ -113,4 +116,50 @@ describe('vue e2e scripts', () => {
113116
expect(status.value).toEqual('loading')
114117
expect(status2.value).toEqual('loading')
115118
})
119+
120+
it('respects useScript privacy controls - #293', async () => {
121+
const head = createHeadCore()
122+
const script = useScript({
123+
src: 'https://s.kk-resources.com/leadtag.js',
124+
async: true,
125+
crossorigin: false,
126+
}, {
127+
head,
128+
})
129+
const ssr = await renderSSRHead(head)
130+
expect(ssr.headTags.replace('>', '').split(' ').sort()).toMatchInlineSnapshot(`
131+
[
132+
"<link",
133+
"as="script"",
134+
"fetchpriority="low"",
135+
"href="https://s.kk-resources.com/leadtag.js"",
136+
"referrerpolicy="no-referrer"",
137+
"rel="preload"",
138+
]
139+
`)
140+
script.remove()
141+
})
142+
it('respects useScript privacy controls', async () => {
143+
const head = createHeadCore()
144+
const script = useScript({
145+
src: 'https://s.kk-resources.com/leadtag.js',
146+
crossorigin: 'use-credentials',
147+
referrerpolicy: 'no-referrer-when-downgrade',
148+
}, {
149+
head,
150+
})
151+
const ssr = await renderSSRHead(head)
152+
expect(ssr.headTags.replace('>', '').split(' ').sort()).toMatchInlineSnapshot(`
153+
[
154+
"<link",
155+
"as="script"",
156+
"crossorigin="use-credentials"",
157+
"fetchpriority="low"",
158+
"href="https://s.kk-resources.com/leadtag.js"",
159+
"referrerpolicy="no-referrer-when-downgrade"",
160+
"rel="preload"",
161+
]
162+
`)
163+
script.remove()
164+
})
116165
})

0 commit comments

Comments
 (0)
Please sign in to comment.