feat: add useCache and setCache for nuxtislands

This commit is contained in:
Julien Huang 2023-11-11 20:24:08 +01:00
parent bb533f8d25
commit b48053a078
2 changed files with 23 additions and 3 deletions

View File

@ -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) {

View File

@ -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)
}