Next.js 13 with Prisma: Database entries not updating in production
Answered
Little Bunting posted this in #help-forum
Little BuntingOP
I have a Next.js 13 application integrated with Prisma, connecting to a MySQL database. Everything works perfectly locally. However, in production, when I add new entries, they don't reflect in the UI or even in the API response. This seems like a caching issue, since the updates only appear after every deployment. I've tried various caching headers (force-cache, no-cache, etc.) without success.
Here's my API route for fetching the entries:
And here's the function for getting the entries:
Here is a preview of what I'm trying to do, on guestbook page, you can login with GitHub and let a comment, then all the entries should be updated, same on remove.
https://preprod.jonu.dev/guestbook
Here's my API route for fetching the entries:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export async function GET() {
try {
const guestbookEntries = await prisma.entries.findMany();
return Response.json(guestbookEntries);
} catch (error) {
return Response.json({
error: 'Error getting guestbook entries',
});
}
}And here's the function for getting the entries:
const getGuestbookEntries = async () => {
try {
const response = await fetch('/api/getGuestbookEntries', {
cache: 'force-cache',
});
const entries = await response.json();
setData(entries);
} catch (err) {
setErrorMessage('Error on fetching');
setSeverity('error');
setIsVisible(true);
}
};Here is a preview of what I'm trying to do, on guestbook page, you can login with GitHub and let a comment, then all the entries should be updated, same on remove.
https://preprod.jonu.dev/guestbook
9 Replies
force-cache will only use the cache. You may also want to clear cache on your browser just in case.
Try also configuring the revalidate interval in your API route with
Try also configuring the revalidate interval in your API route with
export const revalidate = 10 (revalidate every 10 seconds)Little BuntingOP
ok, do I need to add this in route file or?
This is converted into static computing by default, meaning only computed once at build time and the rest will used cached bersion.To avoid this, turn this route into dynamic computation using
export const dynamic = 'force-static'Little BuntingOP
Thanks for helping, with 'force-static' the result is the same, when I add new entries, they don't reflect in the UI or even in the API response, but with
export const revalidate = 1, somehow it's working but I need to refresh the page every single time.it should be 'force-dynamic' not static
Answer
Little BuntingOP
ok let me try
thank you, now it's working