Revalidating data/caching
Unanswered
West African Lion posted this in #help-forum
West African LionOP
Im pretty new to next.js, Im using sanity CMS to fetch my data.
Here is an example on how i fetch my albums... but even though i edit the data in sanity it wont change in the frontend. So my question is how can i revalidate the fetch so it changes when the data changes.
Thanks for the help
import { getClient } from '@/sanity/lib/client';
import { token } from '@/sanity/lib/token';
import { ALBUM_QUERY } from '@/sanity/lib/queries';
export async function getAlbums({ draftMode = false }) {
try {
const client = getClient(draftMode ? token : undefined);
const albums = await client.fetch(ALBUM_QUERY);
return {
props: {
albums,
draftMode,
// Ensuring token is passed only in draft mode and is empty otherwise
token: draftMode ? token : '',
},
};
} catch (error) {
console.error('Error fetching albums:', error);
return {
props: {
// In case of error, return empty albums array and draft mode status
albums: [],
draftMode,
token: '', // Ensure token is not leaked in case of error
},
};
}
} Here is an example on how i fetch my albums... but even though i edit the data in sanity it wont change in the frontend. So my question is how can i revalidate the fetch so it changes when the data changes.
Thanks for the help