mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-30 01:17:16 +00:00
39 lines
1002 B
TypeScript
39 lines
1002 B
TypeScript
import { ref } from 'vue'
|
|
import { parseURL } from 'ufo'
|
|
import { useHead } from '@unhead/vue'
|
|
import { defineNuxtPlugin } from '#app/nuxt'
|
|
|
|
export default defineNuxtPlugin({
|
|
name: 'nuxt:cross-origin-prefetch',
|
|
setup (nuxtApp) {
|
|
const externalURLs = ref(new Set<string>())
|
|
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()]
|
|
})
|
|
}
|
|
})
|
|
}
|
|
})
|