mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-03 02:47:12 +00:00
30 lines
737 B
TypeScript
30 lines
737 B
TypeScript
|
import { ref } from 'vue'
|
||
|
import { parseURL } from 'ufo'
|
||
|
import { defineNuxtPlugin, useHead } from '#app'
|
||
|
|
||
|
export default defineNuxtPlugin((nuxtApp) => {
|
||
|
const externalURLs = ref(new Set<string>())
|
||
|
useHead({
|
||
|
script: [
|
||
|
() => ({
|
||
|
type: 'speculationrules',
|
||
|
innerHTML: JSON.stringify({
|
||
|
prefetch: [
|
||
|
{
|
||
|
source: 'list',
|
||
|
urls: [...externalURLs.value],
|
||
|
requires: ['anonymous-client-ip-when-cross-origin']
|
||
|
}
|
||
|
]
|
||
|
})
|
||
|
})
|
||
|
]
|
||
|
})
|
||
|
nuxtApp.hook('link:prefetch', (url) => {
|
||
|
const { protocol } = parseURL(url)
|
||
|
if (protocol && ['http:', 'https:'].includes(protocol)) {
|
||
|
externalURLs.value.add(url)
|
||
|
}
|
||
|
})
|
||
|
})
|