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` - **default**: `div`
- **placeholder** | **fallback**: Specify fallback content to be rendered if the slot fails to render. - **placeholder** | **fallback**: Specify fallback content to be rendered if the slot fails to render.
- **type**: `string` - **type**: `string`
- **keepFallback**: Keep the fallback content if it failed to render server-side.
- **type**: `boolean`
- **default**: `false`
```vue ```vue
<template> <template>

View File

@ -21,6 +21,10 @@ export default defineComponent({
}, },
placeholderTag: { placeholderTag: {
type: String type: String
},
keepFallback: {
type: Boolean,
default: () => false
} }
}, },
emits: ['ssr-error'], emits: ['ssr-error'],
@ -33,13 +37,14 @@ export default defineComponent({
} }
return () => { return () => {
if (mounted.value) { return ctx.slots.default?.() }
if (ssrFailed.value) { if (ssrFailed.value) {
const slot = ctx.slots.placeholder || ctx.slots.fallback if (!mounted.value || props.keepFallback) {
if (slot) { return slot() } const slot = ctx.slots.placeholder || ctx.slots.fallback
const fallbackStr = props.placeholder || props.fallback if (slot) { return slot() }
const fallbackTag = props.placeholderTag || props.fallbackTag const fallbackStr = props.placeholder || props.fallback
return createElementBlock(fallbackTag, null, fallbackStr) const fallbackTag = props.placeholderTag || props.fallbackTag
return createElementBlock(fallbackTag, null, fallbackStr)
}
} }
return ctx.slots.default?.() return ctx.slots.default?.()
} }

View File

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

View File

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

View File

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