Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 11 commits into from May 14, 2023
3 changes: 3 additions & 0 deletions docs/3.api/2.components/1.nuxt-client-fallback.md
Expand Up @@ -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.
huang-julien marked this conversation as resolved.
Show resolved Hide resolved
- **type**: `string`
- **strategy**: Specify a strategy to adopt once in client-side. By default it tries to render the slot if it fails in client-side but your can choose to keep the fallback content by setting this prop to `keep-fallback`
danielroe marked this conversation as resolved.
Show resolved Hide resolved
- **type**: `string`
danielroe marked this conversation as resolved.
Show resolved Hide resolved
- **default**: `default`

```vue
<template>
Expand Down
10 changes: 9 additions & 1 deletion packages/nuxt/src/app/components/client-fallback.client.ts
Expand Up @@ -21,6 +21,10 @@ export default defineComponent({
},
placeholderTag: {
type: String
},
strategy: {
type: String as () => 'default'|'keep-fallback',
danielroe marked this conversation as resolved.
Show resolved Hide resolved
default: () => 'default'
}
},
emits: ['ssr-error'],
Expand All @@ -33,7 +37,11 @@ export default defineComponent({
}

return () => {
if (mounted.value) { return ctx.slots.default?.() }
if (mounted.value) {
if (!ssrFailed.value || (ssrFailed.value && props.strategy === 'default')) {
return ctx.slots.default?.()
}
}
if (ssrFailed.value) {
const slot = ctx.slots.placeholder || ctx.slots.fallback
if (slot) { return slot() }
Expand Down
4 changes: 4 additions & 0 deletions packages/nuxt/src/app/components/client-fallback.server.ts
Expand Up @@ -23,6 +23,10 @@ const NuxtClientFallbackServer = defineComponent({
},
placeholderTag: {
type: String
},
strategy: {
type: String as () => 'default'|'keep-fallback',
default: () => 'default'
}
},
emits: ['ssr-error'],
Expand Down
3 changes: 2 additions & 1 deletion test/basic.test.ts
Expand Up @@ -334,7 +334,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)
await page.close()
})
})
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/basic/pages/client-fallback.vue
Expand Up @@ -38,6 +38,16 @@
<ClientFallbackStatefulSetup />
<ClientFallbackNonStatefulSetup />
<ClientFallbackNonStateful />
<NuxtClientFallback strategy="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
Expand Down