#How to use revalidateTag for functions that aren't fetching tag?
1 messages · Page 1 of 1 (latest)
To use revalidateTag for functions that aren't directly fetching data, you can still utilize it within server actions or route handlers to trigger refetching for multiple tags when creating a new entity. Here's how you can approach this:
-
First, ensure you're using the latest version of Next.js with the App Router.
-
In your data fetching functions, assign tags to the data you want to be able to revalidate later:
const getData = async () => {
const res = await fetch('<https://api.example.com/data',> {
next: { tags: ['myData'] }
});
return res.json();
}
- Then, in your server action that creates a new entity, you can use
revalidateTagto trigger refetching for multiple tags:
'use server'
import { revalidateTag } from 'next/cache'
export async function createNewEntity() {
// Your logic to create a new entity
await createEntityInDatabase();
// Revalidate multiple tags
revalidateTag('myData')
revalidateTag('anotherTag')
revalidateTag('yetAnotherTag')
}
This approach allows you to trigger revalidation for multiple tags associated with different parts of your application that might be affected by the creation of a new entity.
It's important to note that revalidateTag doesn't immediately refetch the data. Instead, it invalidates the cache for the specified tag, ensuring that the next time the data is requested, it will be refetched.
For more detailed information and examples, you can refer to the Next.js documentation on revalidateTag.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@snow oasis Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Let me check the knowledge base for you again. Give me one moment! :books:
Sorry @snow oasis, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?
Let me check the knowledge base for you again. Give me one moment! :books:
Sorry @snow oasis, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?