(component: T)
| 26 | const cache = new WeakMap() |
| 27 | |
| 28 | function pageToClientOnly<T extends ComponentOptions> (component: T) { |
| 29 | if (import.meta.server) { |
| 30 | return ServerPlaceholder |
| 31 | } |
| 32 | |
| 33 | if (cache.has(component)) { |
| 34 | return cache.get(component) |
| 35 | } |
| 36 | |
| 37 | const clone = { ...component } |
| 38 | |
| 39 | if (clone.render) { |
| 40 | // override the component render (non script setup component) or dev mode |
| 41 | clone.render = (ctx: any, cache: any, $props: any, $setup: any, $data: any, $options: any) => ($setup.mounted$ ?? ctx.mounted$) |
| 42 | ? h(component.render?.bind(ctx)(ctx, cache, $props, $setup, $data, $options)) |
| 43 | : createPlaceholder() |
| 44 | } else { |
| 45 | // handle runtime-compiler template |
| 46 | const placeholderTemplate = clientNodePlaceholder ? '<!--placeholder-->' : '<div></div>' |
| 47 | clone.template &&= ` |
| 48 | <template v-if="mounted$">${component.template}</template> |
| 49 | <template v-else>${placeholderTemplate}</template> |
| 50 | ` |
| 51 | } |
| 52 | |
| 53 | clone.setup = (props, ctx) => { |
| 54 | const nuxtApp = useNuxtApp() |
| 55 | const mounted$ = shallowRef(nuxtApp.isHydrating === false) |
| 56 | provide(clientOnlySymbol, true) |
| 57 | const vm = getCurrentInstance() |
| 58 | if (vm) { |
| 59 | vm._nuxtClientOnly = true |
| 60 | } |
| 61 | onMounted(() => { |
| 62 | mounted$.value = true |
| 63 | }) |
| 64 | const setupState = component.setup?.(props, ctx) || {} |
| 65 | if (isPromise(setupState)) { |
| 66 | return Promise.resolve(setupState).then((setupState: any) => { |
| 67 | if (typeof setupState !== 'function') { |
| 68 | setupState ||= {} |
| 69 | setupState.mounted$ = mounted$ |
| 70 | return setupState |
| 71 | } |
| 72 | return (...args: any[]) => (mounted$.value || !nuxtApp.isHydrating) ? h(setupState(...args)) : createPlaceholder() |
| 73 | }) |
| 74 | } else { |
| 75 | return typeof setupState === 'function' |
| 76 | ? (...args: any[]) => (mounted$.value || !nuxtApp.isHydrating) |
| 77 | ? h(setupState(...args)) |
| 78 | : createPlaceholder() |
| 79 | : Object.assign(setupState, { mounted$ }) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | cache.set(component, clone) |
| 84 | |
| 85 | return clone |
no test coverage detected
searching dependent graphs…