Next.js API Endpoints Not Updating After Deployment
Problem: Data fetched from a database via an API endpoint is remaining static after project deployment.
Analysis: The issue likely stems from caching. In Next.js, API routes and Server Components have caching enabled by default for performance optimization. This caching can interfere with data updates.
Solution:
To prevent caching and ensure dynamic data updates, adjust the caching behavior as follows:
Using fetch():
Add the revalidate option to the fetch() request to specify a revalidation interval in seconds:
fetch('https://...', { next: { revalidate: 10 } });
Alternatively, set the cache option to no-store to disable caching:
fetch('https://...', { cache: 'no-store' });
Using Route Segment Config:
In the layout.js, page.js, or route.js file, add the following code to disable caching for the specific route segment:
import prisma from './lib/prisma'; export const dynamic = 'force-dynamic'; async function getPosts() { const posts = await prisma.post.findMany(); return posts; } export default async function Page() { const posts = await getPosts(); // ... }
By following these steps, you can disable caching for API endpoints and ensure that data updates are reflected promptly after deployment.
The above is the detailed content of Why are My Next.js API Endpoints Not Updating After Deployment?. For more information, please follow other related articles on the PHP Chinese website!