From b48053a078f76acd6776d195e31f34ea39e915c1 Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Sat, 11 Nov 2023 20:24:08 +0100 Subject: [PATCH] feat: add useCache and setCache for nuxtislands --- .../nuxt/src/app/components/nuxt-island.ts | 22 +++++++++++++++++-- .../components/runtime/server-component.ts | 4 +++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/nuxt/src/app/components/nuxt-island.ts b/packages/nuxt/src/app/components/nuxt-island.ts index 40162a15e3..e2de8e5b28 100644 --- a/packages/nuxt/src/app/components/nuxt-island.ts +++ b/packages/nuxt/src/app/components/nuxt-island.ts @@ -44,6 +44,22 @@ export default defineComponent({ source: { type: String, default: () => undefined + }, + /** + * use the NuxtIslandResponse which has been cached if available + * @default true + */ + useCache: { + type: Boolean, + default: true + }, + /** + * allows to set the NuxtIslandResponse into the cache for future updates + * @default true + */ + setCache: { + type: Boolean, + default: true } }, async setup (props, { slots }) { @@ -130,7 +146,9 @@ export default defineComponent({ appendResponseHeader(event, 'x-nitro-prerender', hints) } } - setPayload(key, result) + if (import.meta.client && props.setCache) { + setPayload(key, result) + } return result } const key = ref(0) @@ -167,7 +185,7 @@ export default defineComponent({ } if (import.meta.client) { - watch(props, debounce(() => fetchComponent(), 100)) + watch(props, debounce(() => fetchComponent(!props.useCache), 100)) } if (import.meta.client && !nuxtApp.isHydrating && props.lazy) { diff --git a/packages/nuxt/src/components/runtime/server-component.ts b/packages/nuxt/src/components/runtime/server-component.ts index e1f75e6a59..5345cb2114 100644 --- a/packages/nuxt/src/components/runtime/server-component.ts +++ b/packages/nuxt/src/components/runtime/server-component.ts @@ -6,12 +6,14 @@ export const createServerComponent = (name: string) => { return defineComponent({ name, inheritAttrs: false, - props: { lazy: Boolean }, + props: { lazy: Boolean, useCache: { type: Boolean, default: true }, setCache: { type: Boolean, default: true } }, setup (props, { attrs, slots }) { return () => { return h(NuxtIsland, { name, lazy: props.lazy, + useCache: props.useCache, + setCache: props.setCache, props: attrs }, slots) }