mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 07:32:01 +00:00
refactor: avoid function chain
This commit is contained in:
parent
f0c492a657
commit
5e973777b7
@ -10,92 +10,83 @@ function elementIsVisibleInViewport (el: Element) {
|
|||||||
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
|
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
|
||||||
}
|
}
|
||||||
|
|
||||||
function delayedHydrationComponent (loader: AsyncComponentLoader, hydrate: HydrationStrategy) {
|
|
||||||
return defineAsyncComponent({
|
|
||||||
loader,
|
|
||||||
hydrate,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/* @__NO_SIDE_EFFECTS__ */
|
/* @__NO_SIDE_EFFECTS__ */
|
||||||
export const createLazyIOComponent = (componentLoader: AsyncComponentLoader) => {
|
export const createLazyIOComponent = (loader: AsyncComponentLoader) => {
|
||||||
return defineComponent({
|
return defineComponent({
|
||||||
inheritAttrs: false,
|
inheritAttrs: false,
|
||||||
setup (_, { attrs }) {
|
setup (_, { attrs }) {
|
||||||
if (import.meta.server) {
|
if (import.meta.server) {
|
||||||
return () => h(defineAsyncComponent(componentLoader), attrs)
|
return () => h(defineAsyncComponent(loader), attrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
const nuxt = useNuxtApp()
|
const nuxt = useNuxtApp()
|
||||||
const instance = getCurrentInstance()!
|
const instance = getCurrentInstance()!
|
||||||
|
|
||||||
if (instance.vnode.el && nuxt.isHydrating && elementIsVisibleInViewport(instance.vnode.el as Element)) {
|
if (instance.vnode.el && nuxt.isHydrating && elementIsVisibleInViewport(instance.vnode.el as Element)) {
|
||||||
return () => h(defineAsyncComponent(componentLoader), attrs)
|
return () => h(defineAsyncComponent(loader), attrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return () => h(delayedHydrationComponent(componentLoader, hydrateOnVisible(attrs.hydrate as IntersectionObserverInit | undefined)))
|
return () => h(defineAsyncComponent({ loader, hydrate: hydrateOnVisible(attrs.hydrate as IntersectionObserverInit | undefined) }))
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @__NO_SIDE_EFFECTS__ */
|
/* @__NO_SIDE_EFFECTS__ */
|
||||||
export const createLazyNetworkComponent = (componentLoader: AsyncComponentLoader) => {
|
export const createLazyNetworkComponent = (loader: AsyncComponentLoader) => {
|
||||||
return defineComponent({
|
return defineComponent({
|
||||||
inheritAttrs: false,
|
inheritAttrs: false,
|
||||||
setup (_, { attrs }) {
|
setup (_, { attrs }) {
|
||||||
if (import.meta.server) {
|
if (import.meta.server) {
|
||||||
return () => h(defineAsyncComponent(componentLoader), attrs)
|
return () => h(defineAsyncComponent(loader), attrs)
|
||||||
}
|
}
|
||||||
return () => h(delayedHydrationComponent(componentLoader, hydrateOnIdle(attrs.hydrate as number | undefined)))
|
return () => h(defineAsyncComponent({ loader, hydrate: hydrateOnIdle(attrs.hydrate as number | undefined) }))
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @__NO_SIDE_EFFECTS__ */
|
/* @__NO_SIDE_EFFECTS__ */
|
||||||
export const createLazyEventComponent = (componentLoader: AsyncComponentLoader) => {
|
export const createLazyEventComponent = (loader: AsyncComponentLoader) => {
|
||||||
return defineComponent({
|
return defineComponent({
|
||||||
inheritAttrs: false,
|
inheritAttrs: false,
|
||||||
setup (_, { attrs }) {
|
setup (_, { attrs }) {
|
||||||
if (import.meta.server) {
|
if (import.meta.server) {
|
||||||
return () => h(defineAsyncComponent(componentLoader), attrs)
|
return () => h(defineAsyncComponent(loader), attrs)
|
||||||
}
|
}
|
||||||
const events: Array<keyof HTMLElementEventMap> = attrs.hydrate as Array<keyof HTMLElementEventMap> ?? ['mouseover']
|
const events: Array<keyof HTMLElementEventMap> = attrs.hydrate as Array<keyof HTMLElementEventMap> ?? ['mouseover']
|
||||||
return () => h(delayedHydrationComponent(componentLoader, hydrateOnInteraction(events)))
|
return () => h(defineAsyncComponent({ loader, hydrate: hydrateOnInteraction(events) }))
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @__NO_SIDE_EFFECTS__ */
|
/* @__NO_SIDE_EFFECTS__ */
|
||||||
export const createLazyMediaComponent = (componentLoader: AsyncComponentLoader) => {
|
export const createLazyMediaComponent = (loader: AsyncComponentLoader) => {
|
||||||
return defineComponent({
|
return defineComponent({
|
||||||
inheritAttrs: false,
|
inheritAttrs: false,
|
||||||
setup (_, { attrs }) {
|
setup (_, { attrs }) {
|
||||||
if (import.meta.server) {
|
const media = attrs.hydrate as string | undefined
|
||||||
return () => h(defineAsyncComponent(componentLoader), attrs)
|
if (import.meta.server || !media) {
|
||||||
|
return () => h(defineAsyncComponent(loader), attrs)
|
||||||
}
|
}
|
||||||
return () => h(delayedHydrationComponent(componentLoader, hydrateOnMediaQuery(attrs.hydrate as string | undefined ?? '')))
|
return () => h(defineAsyncComponent({ loader, hydrate: hydrateOnMediaQuery(attrs.hydrate as string | undefined ?? '') }))
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @__NO_SIDE_EFFECTS__ */
|
/* @__NO_SIDE_EFFECTS__ */
|
||||||
export const createLazyIfComponent = (componentLoader: AsyncComponentLoader) => {
|
export const createLazyIfComponent = (loader: AsyncComponentLoader) => {
|
||||||
return defineComponent({
|
return defineComponent({
|
||||||
inheritAttrs: false,
|
inheritAttrs: false,
|
||||||
setup (_, { attrs }) {
|
setup (_, { attrs }) {
|
||||||
if (import.meta.server) {
|
const condition = !!(attrs.hydrate ?? true)
|
||||||
return () => h(defineAsyncComponent(componentLoader), attrs)
|
if (import.meta.server || condition) {
|
||||||
|
return () => h(defineAsyncComponent(loader), attrs)
|
||||||
}
|
}
|
||||||
const shouldHydrate = ref(!!(attrs.hydrate ?? true))
|
const shouldHydrate = ref(condition)
|
||||||
if (shouldHydrate.value) {
|
|
||||||
return () => h(defineAsyncComponent(componentLoader), attrs)
|
|
||||||
}
|
|
||||||
|
|
||||||
const strategy: HydrationStrategy = (hydrate) => {
|
const strategy: HydrationStrategy = (hydrate) => {
|
||||||
const unwatch = watch(shouldHydrate, () => hydrate(), { once: true })
|
const unwatch = watch(shouldHydrate, () => hydrate(), { once: true })
|
||||||
return () => unwatch()
|
return () => unwatch()
|
||||||
}
|
}
|
||||||
return () => h(delayedHydrationComponent(componentLoader, strategy))
|
return () => h(defineAsyncComponent({ loader, hydrate: strategy }))
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user