Next.js Discord

Discord Forum

Route Handlers caching in development

Answered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
I have a GET api route handler, that fetches a lot of data from firebase, and i want it cached, this happens by default, when i am in production, the api route is called only once, until i revalidate it from elsewhere in the app, but i'd like the same to happen in development, but instead the api route is executed each time i hit it, can i make the development behaviour be the same as the production one?
Answered by joulev
import { unstable_cache } from "next/cache";

const getData = unstable_cache(async () => {
  console.log("This is run");
  return "hello";
});

export async function GET() {
  const data = await getData();
  return Response.json(data);
}
View full answer

4 Replies

Cape lion
Yeah, this is something big that I feel like is missing from Nextjs, or I couldn't find a way to do it 'natively'. Imagine blowing through your API rate limits when you're working on your CSS. 😄
Answer
whether intentional or not, unstable_cache works exactly the same way in dev mode and prod mode