fix(nuxt): observe slot element in custom nuxt-link (#19802)

This commit is contained in:
Mehdi HosseinZade 2023-03-21 01:16:12 +03:30 committed by GitHub
parent 4f683319cb
commit 3a971d0b36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 3 deletions

View File

@ -198,13 +198,15 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
// Prefetching
const prefetched = ref(false)
const el = process.server ? undefined : ref<HTMLElement | null>(null)
const elRef = process.server ? undefined : (ref: any) => { el!.value = props.custom ? ref?.$el?.nextElementSibling : ref?.$el }
if (process.client) {
checkPropConflicts(props, 'prefetch', 'noPrefetch')
const shouldPrefetch = props.prefetch !== false && props.noPrefetch !== true && props.target !== '_blank' && !isSlowConnection()
if (shouldPrefetch) {
const nuxtApp = useNuxtApp()
let idleId: number
let unobserve: (() => void)| null = null
let unobserve: (() => void) | null = null
onMounted(() => {
const observer = useObserver()
onNuxtReady(() => {
@ -236,7 +238,7 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
return () => {
if (!isExternal.value) {
const routerLinkProps: Record<string, any> = {
ref: process.server ? undefined : (ref: any) => { el!.value = ref?.$el },
ref: elRef,
to: to.value,
activeClass: props.activeClass || options.activeClass,
exactActiveClass: props.exactActiveClass || options.exactActiveClass,

View File

@ -26,7 +26,7 @@ describe.skipIf(isWindows)('minimal nuxt application', () => {
it('default client bundle size', async () => {
stats.client = await analyzeSizes('**/*.js', publicDir)
expect(stats.client.totalBytes).toBeLessThan(106600)
expect(stats.client.totalBytes).toBeLessThan(106650)
expect(stats.client.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(`
[
"_nuxt/_plugin-vue_export-helper.js",