mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat(nuxt): allow keeping fallback for NuxtClientFallback
(#20336)
This commit is contained in:
parent
ebbda2cbe4
commit
603e7e7fb1
@ -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>
|
||||
|
@ -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?.()
|
||||
}
|
||||
|
@ -23,6 +23,10 @@ const NuxtClientFallbackServer = defineComponent({
|
||||
},
|
||||
placeholderTag: {
|
||||
type: String
|
||||
},
|
||||
keepFallback: {
|
||||
type: Boolean,
|
||||
default: () => false
|
||||
}
|
||||
},
|
||||
emits: ['ssr-error'],
|
||||
|
@ -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()
|
||||
|
10
test/fixtures/basic/pages/client-fallback.vue
vendored
10
test/fixtures/basic/pages/client-fallback.vue
vendored
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user