#NuxtData duplicate items
1 messages · Page 1 of 1 (latest)
This issue appears to be related to how Nuxt handles data caching and payload management with useFetch. The duplicate items in your payload could be caused by several factors:
Possible Causes
-
Key-related issues: When using
useFetchwith dynamic keys, Nuxt might not properly deduplicate the data in the payload. -
Transform function: Your transform function might be causing issues with how Nuxt handles the data internally. The
toSortedmethod creates a new array, which might be causing unexpected behavior. -
Cache management: Nuxt caches data based on keys, and if the key doesn't change but the data does, you might see duplicate entries.
Solutions to Try
-
Use a more specific key:
Make sure your key is unique and changes wheneversubsheetId.valuechanges:const { data, status } = await useFetch(`/api/scores/${slug}/${subsheetId.value}/team`, { key: `team-scores-${slug}-${subsheetId.value}-overall-${Date.now()}`, // rest of your code })Adding a timestamp as suggested in this Stack Overflow answer can help prevent caching issues.
-
Move the transform logic outside of useFetch:
Try handling the sorting after the data is fetched:const { data: rawData, status } = await useFetch(`/api/scores/${slug}/${subsheetId.value}/team`, { key: `team-scores-${subsheetId.value}-overall` }) const data = computed(() => rawData.value?.toSorted((a, b) => playerIds.indexOf(a.osuUserId) - playerIds.indexOf(b.osuUserId) ) ) -
Clear cache before fetching:
You could try clearing the Nuxt data cache before fetching:
import { clearNuxtData } from '#app'
// Clear the data before fetching
clearNuxtData(`team-scores-${subsheetId.value}-overall`)
const { data, status } = await useFetch(...)
-
Use useAsyncData with explicit control:
As mentioned in the documentation,useFetchis a wrapper arounduseAsyncDataand$fetch. Using them separately might give you more control:const { data, status } = await useAsyncData( `team-scores-${subsheetId.value}-overall`, () => $fetch(`/api/scores/${slug}/${subsheetId.value}/team`).then(res => res.toSorted((a, b) => playerIds.indexOf(a.osuUserId) - playerIds.indexOf(b.osuUserId)) ) )