feat(nuxt): allow keeping fallback for NuxtClientFallback (#20336)

This commit is contained in:
Julien Huang 2023-05-14 23:22:54 +02:00 committed by GitHub
parent ebbda2cbe4
commit 603e7e7fb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 6 deletions

View File

@ -30,6 +30,9 @@ This component is experimental and in order to use it you must enable the `exper
- **default**: `div`
- **placeholder** | **fallback**: Specify fallback content to be rendered if the slot fails to render.
- **type**: `string`
- **keepFallback**: Keep the fallback content if it failed to render server-side.
- **type**: `boolean`
- **default**: `false`
```vue
<template>

View File

@ -21,6 +21,10 @@ export default defineComponent({
},
placeholderTag: {
type: String
},
keepFallback: {
type: Boolean,
default: () => false
}
},
emits: ['ssr-error'],
@ -33,13 +37,14 @@ export default defineComponent({
}
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)
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)
}
}
return ctx.slots.default?.()
}

View File

@ -23,6 +23,10 @@ const NuxtClientFallbackServer = defineComponent({
},
placeholderTag: {
type: String
},
keepFallback: {
type: Boolean,
default: () => false
}
},
emits: ['ssr-error'],

View File

@ -345,6 +345,8 @@ describe('pages', () => {
// ensure components reactivity once mounted
await page.locator('#increment-count').click()
expect(await page.locator('#sugar-counter').innerHTML()).toContain('Sugar Counter 12 x 1 = 12')
// keep-fallback strategy
expect(await page.locator('#keep-fallback').all()).toHaveLength(1)
// #20833
expect(await page.locator('body').innerHTML()).not.toContain('Hello world !')
await page.close()

View File

@ -38,6 +38,16 @@
<ClientFallbackStatefulSetup />
<ClientFallbackNonStatefulSetup />
<ClientFallbackNonStateful />
<NuxtClientFallback keep-fallback>
<div>
<BreakInSetup />
</div>
<template #fallback>
<div id="keep-fallback">
keep fallback in client
</div>
</template>
</NuxtClientFallback>
</div>
<button id="increment-count" @click="multiplier++">
increment count