zangetsu
zangetsu
NNuxt
Created by zangetsu on 3/28/2025 in #❓・help
Nuxt 3.16.1 - getCachedData not working (?)
No description
13 replies
NNuxt
Created by zangetsu on 3/28/2025 in #❓・help
Nuxt 3.16.1 - getCachedData not working (?)
It's strange, so I can't cache API calls that are made by a sub-component?:usefetchwrong:
13 replies
NNuxt
Created by zangetsu on 3/28/2025 in #❓・help
Nuxt 3.16.1 - getCachedData not working (?)
Parent.vue
<script setup>

// Here is caching
const url = '/api/tipologiche/tematiche_secondarie'
const options = useCachedData().useFetchOptions({ url })
const { data: tematiche } = useFetch<JsonLdCollection<ITipologica>>('/api/tipologiche/tematiche_secondarie', options)

const rows = [...]
</script>

<template>

<table>
<tbody>
<tr v-for="row in rows">
<child-component :row />
</tr>
</tbody>
</table>
</template>
<script setup>

// Here is caching
const url = '/api/tipologiche/tematiche_secondarie'
const options = useCachedData().useFetchOptions({ url })
const { data: tematiche } = useFetch<JsonLdCollection<ITipologica>>('/api/tipologiche/tematiche_secondarie', options)

const rows = [...]
</script>

<template>

<table>
<tbody>
<tr v-for="row in rows">
<child-component :row />
</tr>
</tbody>
</table>
</template>
Child.vue
<script setup lang="ts">

const props = defineProps<{row: any}>()
// making api call every time it is instantiated
const url = '/api/tipologiche/tematiche_secondarie'
const options = useCachedData().useFetchOptions({ url })
const { data: tematiche } = useFetch<JsonLdCollection<ITipologica>>('/api/tipologiche/tematiche_secondarie', options)

</script>

<template>
{{tematiche.includes(row) ? 'yes' : 'no'}}
</template>
<script setup lang="ts">

const props = defineProps<{row: any}>()
// making api call every time it is instantiated
const url = '/api/tipologiche/tematiche_secondarie'
const options = useCachedData().useFetchOptions({ url })
const { data: tematiche } = useFetch<JsonLdCollection<ITipologica>>('/api/tipologiche/tematiche_secondarie', options)

</script>

<template>
{{tematiche.includes(row) ? 'yes' : 'no'}}
</template>
the useCachedData is the wrapper of your code
13 replies
NNuxt
Created by zangetsu on 3/28/2025 in #❓・help
Nuxt 3.16.1 - getCachedData not working (?)
i got it, i was using this code on a component inside a v-for, moving it on the parent component fixed this behavior, im still wondering why it wasn't caching btw
13 replies
NNuxt
Created by zangetsu on 3/28/2025 in #❓・help
Nuxt 3.16.1 - getCachedData not working (?)
yep
13 replies