Need to hard refresh for App router API to update
Unanswered
West African Lion posted this in #help-forum
West African LionOP
Hi there, kind of new to next js and the app router, I’m using strapi as my backend, and I added “views” to my posts, every time somebody views a post, the post page.tsx sends off to the /app/api/viewpost.ts a post, and that sends another post to the strapi api. This works, but only on hard refresh (ctrl, shift and r), and otherwise does not update the view count. Almost like the page is caching on next side, and just saves the results, instead of re posting to the api, unless I hard refresh the page.
Been stuck on this, trying to add no cache to the post request, with no luck.
I can hit the api with post man over and over and the view count updates, just not when I refresh the page on my next js app, only the first load or hard refresh.
Any help would be super appreciated, been banging my head against that wall.
Thanks!
Been stuck on this, trying to add no cache to the post request, with no luck.
I can hit the api with post man over and over and the view count updates, just not when I refresh the page on my next js app, only the first load or hard refresh.
Any help would be super appreciated, been banging my head against that wall.
Thanks!
6 Replies
@West African Lion Hi there, kind of new to next js and the app router, I’m using strapi as my backend, and I added “views” to my posts, every time somebody views a post, the post page.tsx sends off to the /app/api/viewpost.ts a post, and that sends another post to the strapi api. This works, but only on hard refresh (ctrl, shift and r), and otherwise does not update the view count. Almost like the page is caching on next side, and just saves the results, instead of re posting to the api, unless I hard refresh the page.
Been stuck on this, trying to add no cache to the post request, with no luck.
I can hit the api with post man over and over and the view count updates, just not when I refresh the page on my next js app, only the first load or hard refresh.
Any help would be super appreciated, been banging my head against that wall.
Thanks!
Use a server action instead of the route handler, then use revalidateTag/revalidatePath to refresh the page
@joulev Use a server action instead of the route handler, then use revalidateTag/revalidatePath to refresh the page
West African LionOP
is this App router compatible? thanks!
So basically use middleware, https://nextjs.org/docs/app/building-your-application/routing/middleware
then, these? https://nextjs.org/docs/app/api-reference/functions/revalidateTag I will try thanks
then, these? https://nextjs.org/docs/app/api-reference/functions/revalidateTag I will try thanks
American Crow
No not Middleware do not use route handler means do not use nextjs
api layer at all. You don't need it. You can either use server actions if you want to make a post to strapi from a client component or use a server component to get data from strapi. In both cases you can call strapi directly no need for an API endpoint on nextjs sideWest African LionOP
gotcha, so I just did it within the page file (the post req) that's what you guys mean by server actions? @American Crow
American Crow
It's a technical term and concept within nextjs https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations
It's a function that runs on the server. So it's not in the client bundle.
It's a function that runs on the server. So it's not in the client bundle.