fix(nuxt): use URL constructor to resolve external protocols

This commit is contained in:
Daniel Roe 2024-05-07 14:14:02 +01:00
parent 8e793ad5cc
commit 5f0693a69a
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
2 changed files with 4 additions and 4 deletions

View File

@ -140,7 +140,7 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
if (!options?.external) { if (!options?.external) {
throw new Error('Navigating to an external URL is not allowed by default. Use `navigateTo(url, { external: true })`.') throw new Error('Navigating to an external URL is not allowed by default. Use `navigateTo(url, { external: true })`.')
} }
const protocol = parseURL(toPath).protocol const { protocol } = new URL(toPath)
if (protocol && isScriptProtocol(protocol)) { if (protocol && isScriptProtocol(protocol)) {
throw new Error(`Cannot navigate to a URL with '${protocol}' protocol.`) throw new Error(`Cannot navigate to a URL with '${protocol}' protocol.`)
} }

View File

@ -1,8 +1,9 @@
import { ref } from 'vue' import { ref } from 'vue'
import { parseURL } from 'ufo'
import { useHead } from '@unhead/vue' import { useHead } from '@unhead/vue'
import { defineNuxtPlugin } from '../nuxt' import { defineNuxtPlugin } from '../nuxt'
const SUPPORTED_PROTOCOLS = ['http:', 'https:']
export default defineNuxtPlugin({ export default defineNuxtPlugin({
name: 'nuxt:cross-origin-prefetch', name: 'nuxt:cross-origin-prefetch',
setup (nuxtApp) { setup (nuxtApp) {
@ -26,8 +27,7 @@ export default defineNuxtPlugin({
script: [generateRules()], script: [generateRules()],
}) })
nuxtApp.hook('link:prefetch', (url) => { nuxtApp.hook('link:prefetch', (url) => {
const { protocol } = parseURL(url) if (SUPPORTED_PROTOCOLS.some(p => url.startsWith(p)) && SUPPORTED_PROTOCOLS.includes(new URL(url).protocol)) {
if (protocol && ['http:', 'https:'].includes(protocol)) {
externalURLs.value.add(url) externalURLs.value.add(url)
head?.patch({ head?.patch({
script: [generateRules()], script: [generateRules()],