Skip to content

Commit 25f1ade

Browse files
committed
fix: avoid overriding <link rek="preload" with invalid src
Fixes #218
1 parent 931f4f6 commit 25f1ade

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/runtime/composables/useScript.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
2929
const rel = options.trigger === 'onNuxtReady' ? 'preload' : 'preconnect'
3030
const isCrossOrigin = input.src && !input.src.startsWith('/')
3131
const id = resolveScriptKey(input) as keyof typeof nuxtApp._scripts
32-
if (input.src && options.trigger !== 'server' && (rel === 'preload' || isCrossOrigin)) {
32+
const nuxtApp = useNuxtApp()
33+
nuxtApp.$scripts = nuxtApp.$scripts! || reactive({})
34+
const exists = !!(nuxtApp.$scripts as Record<string, any>)?.[id]
35+
// need to make sure it's not already registered
36+
if (!exists && input.src && options.trigger !== 'server' && (rel === 'preload' || isCrossOrigin)) {
3337
useHead({
3438
link: [
3539
{
@@ -47,9 +51,6 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
4751
if (options.trigger === 'onNuxtReady') {
4852
options.trigger = onNuxtReady
4953
}
50-
const nuxtApp = useNuxtApp()
51-
nuxtApp.$scripts = nuxtApp.$scripts! || reactive({})
52-
const exists = !!(nuxtApp.$scripts as Record<string, any>)?.[id]
5354
if (import.meta.client) {
5455
// only validate if we're initializing the script
5556
if (!exists) {

0 commit comments

Comments
 (0)