Replies: 2 comments 3 replies
-
If there are no other solutions, then it would be necessary for Vercel to support caching for POST requests, or for Next.js server components to support rewriting POST requests as GET requests. |
Beta Was this translation helpful? Give feedback.
-
u're absolutely right — Vercel’s edge network currently doesn’t support caching for POST requests, and rewriting POST to GET via middleware is a known workaround. But as u mentioned, that doesn’t play well with Next.js server components, which expect specific HTTP method semantics. ✅ Why POST isn’t cached: By default, HTTP caching is designed to work with idempotent methods like GET or HEAD. POST is considered unsafe to cache because it may imply a state change — even if in your case, the POST is read-only. Vercel and most CDNs will skip caching POST requests entirely. ✅ Available Workarounds:
If your POST endpoint doesn’t actually change state, consider exposing the same logic via a GET route that can be safely cached: // pages/api/data.ts
export async function GET(req: Request) {
const data = await fetchData();
return Response.json(data, {
headers: {
"Cache-Control": "s-maxage=86400, stale-while-revalidate=3600"
}
});
}
This lets Vercel cache the response at the edge without extra config.
2 Edge Middleware to transform POST → GET (with caution)
Technically u could write middleware to rewrite POST to GET, but u’ll lose access to the request body in that flow. And server components won’t support POST-as-GET transformations cleanly either.
So unless the body is empty or not critical, this is not reliable:
// middleware.ts
import { NextRequest, NextResponse } from 'next/server';
export function middleware(req: NextRequest) {
if (req.method === 'POST' && req.nextUrl.pathname === '/api/data') {
const url = req.nextUrl.clone();
url.pathname = '/api/data';
return NextResponse.rewrite(url, { method: 'GET' });
}
}
But again: this won't carry the POST body, and Next.js might reject the request if used in server components.
Best Practical Recommendation:
Keep your POST API as is (for security and clarity)
Duplicate the same logic under a GET route just for cached reads
Use Cache-Control headers to control the behavior
Vercel Edge does not cache POST — by design
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
If Vercel edge network cache doesn't support caching for POST requests, the only solution I can think of is using edge middleware to rewrite POST requests as GET requests. However, Next.js server components don't support this approach. Is there any other way? I really want to take advantage of caching.
Additional information
My service is an API service, but the data doesn't change frequently, so caching doesn't cause problems.
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions