Next.js Discord

Discord Forum

Caching issue with Prisma and Axios

Answered
Atlantic salmon posted this in #help-forum
Open in Discord
Atlantic salmonOP
I am using Prisma in all of my api routes to fetch data. In my server components i fetch my data with prisma directly. In client components i use axios to make my api calls.

To give some context about my problem: I have a user page where the current amount of users is displayed and below it is a table with all users. The amount is managed in a client component and the table is a server component. On the same page there is an option to create a new user. Creating the new user works perfectly fine, but even after a page reload, the client component, that shows the amount is not changing, the server component otherwise is showing the new data.

I think the data is somehow cached and that is the reason i tried to add export const revalidate = 0, but that did not work. I hope someone can explain me how to fix this issue.

If you need any code just tell me what! It is difficult to provide whole code, because it is splitted in multiple components and there is more happening than just that...

Thanks for any help!
Answered by fuma
I think your route handler is cached by Next.js, try adding that segment config to the route.ts instead
View full answer

9 Replies

How do you define the API Route? Is it a route handler under app dir?

As you mentioned, the following isn't working:
export const revalidate = 0

Where do you add this segment configuration?
Client components cache neither fetch nor axio, It is likely your route handler is being cached/statically generated
@fuma How do you define the API Route? Is it a route handler under `app` dir? As you mentioned, the following isn't working: ts export const revalidate = 0 Where do you add this segment configuration?
Atlantic salmonOP
The route handler is based on the app dir. I tried to add the revalidate in my layout.tsx/page.tsx and neither of it worked.
I think your route handler is cached by Next.js, try adding that segment config to the route.ts instead
Answer
By default GET route handlers are statically generated during build time
Atlantic salmonOP
Ok i am going to try this! Is there a way to add it to every route without manually doing it? I have many routes beside my example from above...
Atlantic salmonOP
So this is definitely the issue! With the revalidate in my route.ts file the data is finally updated! But i still hope there is a better way than manually adding this to all get routes, because there are about 30 in my project :Gur_Cry:
@Atlantic salmon Ok i am going to try this! Is there a way to add it to every route without manually doing it? I have many routes beside my example from above...
Unfortunately, there's no way to change the default behavior for now. All GET route handlers are cached despite dynamic routes.
@fuma Unfortunately, there's no way to change the default behavior for now. All `GET` route handlers are cached despite dynamic routes.
Atlantic salmonOP
Ok! But thank you really much, this issue kept me awake for the last days :blobthanks: