2023-04-12 17:10:18 +00:00
|
|
|
import { createElementBlock, defineComponent, onMounted, ref } from 'vue'
|
|
|
|
import { useState } from '../composables/state'
|
2023-03-08 21:13:06 +00:00
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name: 'NuxtClientFallback',
|
|
|
|
inheritAttrs: false,
|
|
|
|
props: {
|
|
|
|
uid: {
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
fallbackTag: {
|
|
|
|
type: String,
|
|
|
|
default: () => 'div'
|
|
|
|
},
|
|
|
|
fallback: {
|
|
|
|
type: String,
|
|
|
|
default: () => ''
|
|
|
|
},
|
|
|
|
placeholder: {
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
placeholderTag: {
|
|
|
|
type: String
|
2023-05-14 21:22:54 +00:00
|
|
|
},
|
|
|
|
keepFallback: {
|
|
|
|
type: Boolean,
|
|
|
|
default: () => false
|
2023-03-08 21:13:06 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
emits: ['ssr-error'],
|
|
|
|
setup (props, ctx) {
|
|
|
|
const mounted = ref(false)
|
|
|
|
const ssrFailed = useState(`${props.uid}`)
|
|
|
|
|
|
|
|
if (ssrFailed.value) {
|
|
|
|
onMounted(() => { mounted.value = true })
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (ssrFailed.value) {
|
2023-05-14 21:22:54 +00:00
|
|
|
if (!mounted.value || props.keepFallback) {
|
|
|
|
const slot = ctx.slots.placeholder || ctx.slots.fallback
|
|
|
|
if (slot) { return slot() }
|
|
|
|
const fallbackStr = props.placeholder || props.fallback
|
|
|
|
const fallbackTag = props.placeholderTag || props.fallbackTag
|
|
|
|
return createElementBlock(fallbackTag, null, fallbackStr)
|
|
|
|
}
|
2023-03-08 21:13:06 +00:00
|
|
|
}
|
|
|
|
return ctx.slots.default?.()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|