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