This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba30fb2
commit e1212f2
Showing
5 changed files
with
129 additions
and
121 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
packages/nuxt/src/app/components/client-fallback.client.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { defineComponent, createElementBlock } from 'vue' | ||
|
||
export default defineComponent({ | ||
name: 'ClientFallback', | ||
inheritAttrs: false, | ||
props: { | ||
uid: { | ||
type: String | ||
}, | ||
fallbackTag: { | ||
type: String, | ||
default: () => 'div' | ||
}, | ||
fallback: { | ||
type: String, | ||
default: () => '' | ||
}, | ||
placeholder: { | ||
type: String | ||
}, | ||
placeholderTag: { | ||
type: String | ||
} | ||
}, | ||
emits: ['ssr-error'], | ||
setup (props, ctx) { | ||
const mounted = ref(false) | ||
const ssrFailed = useState(`${props.uid}`) | ||
|
||
if (ssrFailed.value) { | ||
onMounted(() => { mounted.value = true }) | ||
} | ||
|
||
return () => { | ||
if (mounted.value) { return ctx.slots.default?.() } | ||
if (ssrFailed.value) { | ||
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) | ||
} | ||
return ctx.slots.default?.() | ||
} | ||
} | ||
}) |
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
packages/nuxt/src/app/components/client-fallback.server.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { defineComponent, getCurrentInstance, onErrorCaptured } from 'vue' | ||
import { ssrRenderVNode, ssrRenderAttrs, ssrRenderSlot } from 'vue/server-renderer' | ||
import { createBuffer } from './utils' | ||
|
||
const ClientFallbackServer = defineComponent({ | ||
name: 'ClientFallback', | ||
inheritAttrs: false, | ||
props: { | ||
uid: { | ||
type: String | ||
}, | ||
fallbackTag: { | ||
type: String, | ||
default: () => 'div' | ||
}, | ||
fallback: { | ||
type: String, | ||
default: () => '' | ||
}, | ||
placeholder: { | ||
type: String | ||
}, | ||
placeholderTag: { | ||
type: String | ||
} | ||
}, | ||
emits: ['ssr-error'], | ||
setup (props, ctx) { | ||
const vm = getCurrentInstance() | ||
const ssrFailed = ref(false) | ||
|
||
onErrorCaptured(() => { | ||
useState(`${props.uid}`, () => true) | ||
ssrFailed.value = true | ||
ctx.emit('ssr-error') | ||
return false | ||
}) | ||
|
||
try { | ||
const defaultSlot = ctx.slots.default?.() | ||
const ssrVNodes = createBuffer() | ||
|
||
for (let i = 0; i < defaultSlot.length; i++) { | ||
ssrRenderVNode(ssrVNodes.push, defaultSlot[i], vm) | ||
} | ||
|
||
return { ssrFailed, ssrVNodes } | ||
} catch { | ||
// catch in dev | ||
useState(`${props.uid}`, () => true) | ||
ctx.emit('ssr-error') | ||
return { ssrFailed: true, ssrVNodes: [] } | ||
} | ||
}, | ||
ssrRender (ctx, push, parent) { | ||
if (ctx.ssrFailed) { | ||
const { fallback, placeholder } = ctx.$slots | ||
if (fallback || placeholder) { | ||
ssrRenderSlot(ctx.$slots, fallback ? 'fallback' : 'placeholder', {}, null, push, parent) | ||
} else { | ||
const content = ctx.placeholder || ctx.fallback | ||
const tag = ctx.placeholderTag || ctx.fallbackTag | ||
push(`<${tag}${ssrRenderAttrs(ctx.$attrs)}>${content}</${tag}>`) | ||
} | ||
} else { | ||
// push Fragment markup | ||
push('<!--[-->') | ||
push(ctx.ssrVNodes.getBuffer()) | ||
push('<!--]-->') | ||
} | ||
} | ||
}) | ||
|
||
export default ClientFallbackServer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters