Where to put mutation (POST) with separate backend?
Unanswered
Giant Chinchilla posted this in #help-forum
Giant ChinchillaOP
I have an app with a separate backend and frontend. For the frontend, I'm using NextJS with the app router. I have all my database calls, auth etc. on my separate backend. Now I want to do two things:
- GET something from my backend (f.e. a list of articles)
- POST something to my backend (f.e. a new article)
Now, for GETting, from what I understand, I can have a server component and simply call the fetch there (or have a function somewhere in my project, f.e. in a /lib folder that uses fetch, and import that function) and it will be cached by default and work fine
But for POSTing, I want to revalidate my cache since I want to display the list of articles (or whatever) and have it contain the new article - but from the docs it looks like revalidatePath/Tags is only available in Route Handlers and Server Actions
Usually people then use Prisma/Drizzle etc. and then revalidate the cache
But for my use case, I just want to make a POST request to my backend which will then handle updating the database - but I also want to make sure that my GET of the articles is not stale after I update, so I need to revalidate
But how should I do it? Do I need to create a Server Action or even a full API route "only" to access the revalidate method (since it's not available in server components nor client components)? It seems a bit confusing to me
I would really appreciate any help or guidance on this! 🌻
- GET something from my backend (f.e. a list of articles)
- POST something to my backend (f.e. a new article)
Now, for GETting, from what I understand, I can have a server component and simply call the fetch there (or have a function somewhere in my project, f.e. in a /lib folder that uses fetch, and import that function) and it will be cached by default and work fine
But for POSTing, I want to revalidate my cache since I want to display the list of articles (or whatever) and have it contain the new article - but from the docs it looks like revalidatePath/Tags is only available in Route Handlers and Server Actions
Usually people then use Prisma/Drizzle etc. and then revalidate the cache
But for my use case, I just want to make a POST request to my backend which will then handle updating the database - but I also want to make sure that my GET of the articles is not stale after I update, so I need to revalidate
But how should I do it? Do I need to create a Server Action or even a full API route "only" to access the revalidate method (since it's not available in server components nor client components)? It seems a bit confusing to me
I would really appreciate any help or guidance on this! 🌻
1 Reply
Giant ChinchillaOP
Would it be a good idea to simply make the POST request inside my component, then have a route handler only to revalidate the cache and then call that API from my backend once the mutation is finished?