fix(nuxt): prefetch object-syntax routes with <NuxtLink> (#19144)

This commit is contained in:
Alex Liu 2023-02-21 04:31:27 +08:00 committed by GitHub
parent 29d6094a8d
commit 268cded26c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -176,7 +176,7 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
const el = process.server ? undefined : ref<HTMLElement | null>(null) const el = process.server ? undefined : ref<HTMLElement | null>(null)
if (process.client) { if (process.client) {
checkPropConflicts(props, 'prefetch', 'noPrefetch') checkPropConflicts(props, 'prefetch', 'noPrefetch')
const shouldPrefetch = props.prefetch !== false && props.noPrefetch !== true && typeof to.value === 'string' && props.target !== '_blank' && !isSlowConnection() const shouldPrefetch = props.prefetch !== false && props.noPrefetch !== true && props.target !== '_blank' && !isSlowConnection()
if (shouldPrefetch) { if (shouldPrefetch) {
const nuxtApp = useNuxtApp() const nuxtApp = useNuxtApp()
let idleId: number let idleId: number
@ -189,8 +189,10 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
unobserve = observer!.observe(el.value, async () => { unobserve = observer!.observe(el.value, async () => {
unobserve?.() unobserve?.()
unobserve = null unobserve = null
const path = typeof to.value === 'string' ? to.value : router.resolve(to.value).fullPath
await Promise.all([ await Promise.all([
nuxtApp.hooks.callHook('link:prefetch', to.value as string).catch(() => {}), nuxtApp.hooks.callHook('link:prefetch', path).catch(() => {}),
!isExternal.value && preloadRouteComponents(to.value as string, router).catch(() => {}) !isExternal.value && preloadRouteComponents(to.value as string, router).catch(() => {})
]) ])
prefetched.value = true prefetched.value = true