Next.js Discord

Discord Forum

REST API vercel - doesnt refresh data.

Answered
Florida White posted this in #help-forum
Open in Discord
Florida WhiteOP
Hello everyone! I am working about rest api on nextjs ,and almost everything is good ,but when i push to vercel my project then i have data only the ones I had during build. When i add new data in my database and i will request to api i dont receive new data ,only old. Is there some way to achieve new data? I searched some informations about solution ,but i didnt find.

I am sending fetch like this:

const response = await fetch(
            "http://localhost:3000/api/events",
            {
            
                cache: "no-store",
            }


when i push like this:


const response = await fetch(
            "http://mysite/api/events",
            {
            
                cache: "no-store",
            }


Working only on localhost, corectly. On hosting doesnt refreash data.
Answered by riský
use one of the following: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#opting-out-of-data-caching

this case, id say export const dynamic = 'force-dynamic' or export const revalidate = 0
View full answer

9 Replies

Are you fetching the data on the client or on the server? on the client you can't fech data with http you must use https, check the console to see i you're getting any errors
Chances are that the Api is a route handler and has default cache (not force dynamic)
Florida WhiteOP
Thanks for answers ,I aprreciated. I am using https. I think problem is with my api/route because GET executed only on building ,and after no. But i dont know how I can implemet GET no just on building.
use one of the following: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#opting-out-of-data-caching

this case, id say export const dynamic = 'force-dynamic' or export const revalidate = 0
Answer
Florida WhiteOP
I did it before ,but.. I used
export const dynamic = 'auto'
in a page component.

Like this:

export const dynamic = "force-dynamic";

export default async function Events({ params: { lang } }) {
    const dict = await getDictionaryElements(lang);



layout.js | page.js | route.js

export const dynamic = 'auto'
export const dynamicParams = true
export const revalidate = false
export const fetchCache = 'auto'
export const runtime = 'nodejs'
export const preferredRegion = 'auto'
export const maxDuration = 5

export default function MyComponent() {}
The documentation is a bit confusing, because it shows export is over the component
Thanks a lot !
@Florida White so did it work? (and did you add the opting out of cache to route handler?)
Florida WhiteOP
Yes it work.I just added above the GET handler in route:

export const dynamic = "force-dynamic";


and in my function in Page i fetching like this:

const response = await fetch(apiUrl, {
            { cache: 'force-cache' }),


In localhost i didnt need dynamic = 'force-dynamic' in route ,but in hosting without this didnt work.