What's Changed in Default Caching for Get Route Handlers in Next.js 15?
Next.js 15 updates the behavior of GET Route Handlers, particularly how they handle caching. In the previous version (Next.js 14), GET requests were cached by default unless explicitly marked as dynamic. In Next.js 15, caching for GET routes is now disabled by default. This change gives developers more control over data freshness and reduces issues with stale data.
Why Was Caching Changed?
In Next.js 14, GET routes aimed to boost performance by caching automatically. While this helped reduce server load and speed up responses for static data, it created problems with real-time updates and dynamic content. The change in Next.js 15 aims to:
- Ensure fresh data is delivered by default.
- Improve compatibility with Partial Prerendering (PPR) and real-time APIs.
- Provide developers with more control over caching behavior, making it easier to manage when and where caching is needed.
Flowchart
Configuring Caching for GET Route Handlers in Next.js 15
To enable caching for specific GET routes, you need to opt-in by setting a static route configuration. Hereβs how it works:
1. Default Uncached Behavior
By default, GET handlers are not cached in Next.js 15. Hereβs an example of a basic GET route:
// next-api/routes/hello.ts
export async function GET(request) {
return new Response('Hello, Next.js 15!');
}
- In this setup, each GET request fetches fresh data, ensuring users see the most current information.
2. Opting Into Caching for GET Routes
If you want to enable caching, use the dynamic
option with 'force-static'
to keep the same behavior as Next.js 14:
// next-api/routes/hello.ts
export const dynamic = 'force-static';
export async function GET(request) {
return new Response('Hello, Next.js 15 with Caching!');
}
- Setting
dynamic = 'force-static'
forces the GET handler to cache responses like in Next.js 14, making it suitable for static content.
Special Route Handlers in Next.js 15
Certain handlers like sitemap.ts, opengraph-image.tsx, and icon.tsx still default to static behavior, unless you set them to dynamic. This ensures metadata files remain optimized while allowing flexibility for dynamic configurations.
Example: Sitemap Route
// app/sitemap.ts
export const runtime = 'edge';
export default async function sitemap() {
return [
{ url: 'https://example.com', lastModified: new Date() },
];
}
These routes maintain their default static behavior unless configured otherwise.
FAQs
Q1: Why are GET Route Handlers now uncached by default?
GET handlers are uncached by default to prevent serving outdated data, especially in dynamic use cases. This ensures each request delivers the latest information.
Q2: How do I re-enable caching for specific GET routes?
Use dynamic = 'force-static'
in the route configuration. This enables static caching similar to the behavior in Next.js 14.
Q3: Are special routes like sitemap.ts affected?
No, special routes like sitemap.ts, opengraph-image.tsx, and icon.tsx remain static unless set to dynamic.
Q4: How does this impact server-side performance?
While the uncached default may increase server load, it ensures fresh data. Opting into caching for static data can help maintain performance.
Summary
In Next.js 15, GET Route Handlers are uncached by default to prioritize real-time data handling. Developers can opt into caching manually using the 'force-static'
configuration. This change gives developers better control over data freshness and performance.