diff --git a/packages/nuxt/src/app/plugins/cross-origin-prefetch.client.ts b/packages/nuxt/src/app/plugins/cross-origin-prefetch.client.ts index 83988f6038..04b18cfcc4 100644 --- a/packages/nuxt/src/app/plugins/cross-origin-prefetch.client.ts +++ b/packages/nuxt/src/app/plugins/cross-origin-prefetch.client.ts @@ -4,26 +4,31 @@ import { defineNuxtPlugin, useHead } from '#app' export default defineNuxtPlugin((nuxtApp) => { const externalURLs = ref(new Set()) - useHead({ - script: [ - () => ({ - type: 'speculationrules', - innerHTML: JSON.stringify({ - prefetch: [ - { - source: 'list', - urls: [...externalURLs.value], - requires: ['anonymous-client-ip-when-cross-origin'] - } - ] - }) + function generateRules () { + return { + type: 'speculationrules', + key: 'speculationrules', + innerHTML: JSON.stringify({ + prefetch: [ + { + source: 'list', + urls: [...externalURLs.value], + requires: ['anonymous-client-ip-when-cross-origin'] + } + ] }) - ] + } + } + const head = useHead({ + script: [generateRules()] }) nuxtApp.hook('link:prefetch', (url) => { const { protocol } = parseURL(url) if (protocol && ['http:', 'https:'].includes(protocol)) { externalURLs.value.add(url) + head?.patch({ + script: [generateRules()] + }) } }) })