Next.js Discord

Discord Forum

API data not fetching

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
I have separate admin and frontend routes. When I add data from the admin side, it should display on the frontend. However, in production, the content doesn't update immediately after posting from the admin, whereas it updates instantly in the development or local server.

34 Replies

American black bear
Sounds like your data cache is not being revalidated after adding new data.
But you will need to share some code for us to help here.
Asiatic LionOP
sure i will provide code i have written for admin and frontend
@American black bear But you will need to share some code for us to help here.
Asiatic LionOP
sure i will provide code i have written for admin and frontend
@American black bear Sounds like your data cache is not being revalidated after adding new data.
Asiatic LionOP
this the admin route
this the is route iam using to fetch the saved data
American black bear
and your next.js version in package.json
@American black bear and your next.js version in `package.json`
Asiatic LionOP
"version": "0.1.0",
American black bear
not possible
should be: "next": "13.5.4" or something similar
like this
Asiatic LionOP
so i need to update it?
American black bear
no it's just a thing that some older next.js versions have had caching enabled by default so that could be causing the problem
which doesn't mean it's bad
you can still use older next.js version just fine, but you will need to revalidate the cache when new course is added or an existing one is updated
so when you fetch the data the results are fresh
Asiatic LionOP
ok
can you please provide me any reference or document similar to it. If possibel
American black bear
can you please send your next.js version so we are sure that fetch is being cached
Asiatic LionOP
"version": "0.1.0",
American black bear
like this
if you are unsure take a look at official next.js docs for opting out of cache in the older versions:
https://nextjs.org/docs/13/app/building-your-application/routing/route-handlers
Asiatic LionOP
"next": "13.5.1",
American black bear
try adding this to your api route see if it fixes the problem:

export const dynamic = "force-dynamic"

export async function GET() {
// ...
}
Asiatic LionOP
ok thank you let me try this one
American black bear
if it does then revalidating the cache is your problem and you should take a look at how to tell next.js to bring refreshed data when the courses changes:
Asiatic LionOP
Okay, give me a moment. I'll add the line and share the output with you.
American black bear
make sure to add it to the route GET that fetches the courses, and NOT to the POST route which creates a new course
Asiatic LionOP
ok
still unable to fetcb the data
@American black bear like here
Asiatic LionOP
I added the line in GET route