fix(nuxt): avoid invoking shouldPrefetch on the server side (#30591)

This commit is contained in:
Alex Liu 2025-01-15 07:36:52 +08:00 committed by GitHub
parent 07146ddf48
commit 68ea5c7d85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -325,10 +325,13 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
const elRef = import.meta.server ? undefined : (ref: any) => { el!.value = props.custom ? ref?.$el?.nextElementSibling : ref?.$el }
function shouldPrefetch (mode: 'visibility' | 'interaction') {
if (import.meta.server) { return }
return !prefetched.value && (typeof props.prefetchOn === 'string' ? props.prefetchOn === mode : (props.prefetchOn?.[mode] ?? options.prefetchOn?.[mode])) && (props.prefetch ?? options.prefetch) !== false && props.noPrefetch !== true && props.target !== '_blank' && !isSlowConnection()
}
async function prefetch (nuxtApp = useNuxtApp()) {
if (import.meta.server) { return }
if (prefetched.value) { return }
prefetched.value = true
@ -395,12 +398,14 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
// `custom` API cannot support fallthrough attributes as the slot
// may render fragment or text root nodes (#14897, #19375)
if (!props.custom) {
if (shouldPrefetch('interaction')) {
routerLinkProps.onPointerenter = prefetch.bind(null, undefined)
routerLinkProps.onFocus = prefetch.bind(null, undefined)
}
if (prefetched.value) {
routerLinkProps.class = props.prefetchedClass || options.prefetchedClass
if (import.meta.client) {
if (shouldPrefetch('interaction')) {
routerLinkProps.onPointerenter = prefetch.bind(null, undefined)
routerLinkProps.onFocus = prefetch.bind(null, undefined)
}
if (prefetched.value) {
routerLinkProps.class = props.prefetchedClass || options.prefetchedClass
}
}
routerLinkProps.rel = props.rel || undefined
}