Issue with API to fetch SSR instead of SSG
Answered
Maine Coon posted this in #help-forum
Maine CoonOP
I have a app/page.tsx which fetch data from api/post/route.ts. Page.tsx has 'use client' and fetch has cache: no cache.
Route.ts has a simple GET method with prisma query for all posts. When I build the app, I see '/' is built in static mode and app/post also built in static mode. This cause the fetch GET always the same result. I do not know what cause this, why cannot I render the API and Page in SSR.
Is there anyone any idea what did I miss?
Route.ts has a simple GET method with prisma query for all posts. When I build the app, I see '/' is built in static mode and app/post also built in static mode. This cause the fetch GET always the same result. I do not know what cause this, why cannot I render the API and Page in SSR.
Is there anyone any idea what did I miss?
Answered by riský
thats a bad recommendation tbh, i think
export const revalidate = 0 in your route file is better: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#opting-out-of-data-caching7 Replies
Short-tailed Shearwater
With a GET method your result will be cached automatically , check the doc.
Use a POST method and it will work as expected.
Use a POST method and it will work as expected.
@Short-tailed Shearwater With a GET method your result will be cached automatically , check the doc.
Use a POST method and it will work as expected.
Maine CoonOP
Thank you it seems work. However, the logic is strange to me, if I want to query 'GET' something, I have to use 'POST' because there is no option to turn off cache in case of GET method.
thats a bad recommendation tbh, i think
export const revalidate = 0 in your route file is better: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#opting-out-of-data-cachingAnswer
@riský thats a bad recommendation tbh, i think `export const revalidate = 0` in your route file is better: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#opting-out-of-data-caching
Maine CoonOP
Thanks, it sounds logically and works. I tried it before, I just made misstake because I use
export const revalidate = 0 at page file instead of route file. Now it is ok if it is stated in route file. Thank again 🙂oh yeah that makes sense
glad to see it makes sense and works 🙂
and that you don't have to use the anti patten of post