Weird caching/stale results problem, Nextjs
For context, I am using Neon with Drizzle ORM in a deployed NextJS 14 app (appdir).
I've written multiple simple queries that look like this:
These queries are either used in a single server component that is periodically revalidated, or in "route.ts" files which is the nextjs naming convention for custom request handlers, and in that case it acts as an API endpoint, so that data can be fetched dynamically.
The problem I have is with a different, more complex query I wrote:
This query is only used in a route.ts file (query endpoint). The problem with it is that it almost always returns stale data in both production and development. I can mark a lesson as completed as some user, but it keeps returning stale data, and I can't figure out why.
After some time, I found a possible solution, that doesn't make much sense to me.
3 Replies
rival-blackOP•15mo ago
My solution steps were this:
1) in my route.ts file I added this export:
This didn't fix anything yet, but it makes sense to me to have it, according to nextjs docs.
2) I then updated my file where I create the "neon" object for my database connection:
I use the
dbNoStore object ONLY for that specific query (getAllCompletedLessons) and I get no caching issues:
What I don't understand is why wasn't I getting caching issues with other queries? The only difference I can notice is that getAllCompletedLessons uses db.execute instead of the built in Drizzle syntax.
Also, initially I tried to just use a single "db" export, that would have the { fetchOptions: { cache: "no-store" } } option, but that caused an error when I tried to build the project with the page/server component that is being prerendered and periodically revalidated:
flat-fuchsia•15mo ago
Hey @praskOo
flat-fuchsia•15mo ago
To avoid caching endpoint results - add these two lines (https://github.com/neondatabase/examples/blob/main/ai/aws-bedrock/chatbot-nextjs/app/api/chat/route.ts#L1-L3) and proceed without modifying the Neon constructor.
GitHub
examples/ai/aws-bedrock/chatbot-nextjs/app/api/chat/route.ts at mai...
Examples and code snippets demonstrating common ways of integrating Neon with various frameworks and languages. - neondatabase/examples