Next.js Discord

Discord Forum

Do API routes cache their responses?

Unanswered
Griffon Nivernais posted this in #help-forum
Open in Discord
Avatar
Griffon NivernaisOP
Writing an API server in Next (for ease of use, refactor soon) and wanted to know if API routes cache their responses provided I have them setup through Nginx.

3 Replies

Avatar
API Routes, are you referring to Pages Router one? If yes, they are not cached. Only GET route handlers (in App Router) are cached by default.
Avatar
@fuma 💙 joulev API Routes, are you referring to Pages Router one? If yes, they are not cached. Only `GET` route handlers (in App Router) are cached by default.
Avatar
Griffon NivernaisOP
How would I go about setting this up instead then? The calculations i'm doing are quite heavy and involve point-in-polygon tests over 300 objects.
Avatar
1. Use Next.js's built-in cache
Change it to a GET route handler, the response will be cached at build time [(under some conditions)](https://nextjs.org/docs/app/building-your-application/routing/route-handlers#caching).

2. Your own cache
Use Redis or your own in-memory cache implementation, this won't depend on any Next.js built-in features.