Loading...
save

How to force cache revalidation in Next.js 15 when using Mongoose instead of the native fetch API?

clock icon

asked 75 days ago

message icon

0

eye icon

0

I am trying to implement data caching for my question feed page using Next.js 15. When using the native fetch API, it is very straightforward to set up time-based revalidation or tag-based revalidation using options like { next: { revalidate: 3600, tags: ['questions'] } }.

However, my application queries MongoDB directly using Mongoose (Question.find()), which bypasses the global network fetch overrides completely. Because of this, my data is either fetched dynamically on every single page view (ruining performance), or it gets statically cached during the production build and never updates when a user posts a new question.

Here is my current data fetching function:

1// lib/actions/question.action.ts
2import dbConnect from "@/lib/mongoose";
3import Question from "@/database/question.model";
4
5export async function getQuestions() {
6 await dbConnect();
7
8 // This bypasses the Next.js fetch cache layer entirely!
9 const questions = await Question.find({}).sort({ createdAt: -1 });
10 return { questions };
11}
1// lib/actions/question.action.ts
2import dbConnect from "@/lib/mongoose";
3import Question from "@/database/question.model";
4
5export async function getQuestions() {
6 await dbConnect();
7
8 // This bypasses the Next.js fetch cache layer entirely!
9 const questions = await Question.find({}).sort({ createdAt: -1 });
10 return { questions };
11}

0 Answers

Empty state illustration

No Answers Found

The answer board is empty. Make it rain with your brilliant answer.

1

Write your answer here

Top Questions