Next.js Discord

Discord Forum

Caching

Unanswered
Gray Wagtail posted this in #help-forum
Open in Discord
Gray WagtailOP
Do I need to use API routes to utilize caching? I thought Next did this by extending the native fetch?

Does app router change things?

8 Replies

Nope, you can still control how your data gets cached when making fetch requests. And you don't need to use route handlers to fetch your data since that can also be done directly inside your server components.

See: https://nextjs.org/docs/app/api-reference/functions/fetch
Gray WagtailOP
that wasn't the question
The question is whether the caching is automatic when you use the extended fetch that Next monkey patches, or need to do it manually? And if this changes depending on if you go through a Next API route?
Because in my head the custom header Next sets in fetch, isn't something our Spring backend is going to care about or recognize right? So it must be a thing that is only for Next, and logically that means it needs to be handled in an API route or with the revalidatePath function right?
I asked GPT and it agrees that I need to use an API route in order to get the automatic Next caching
And then I'm guessing that the revlidatePath function looks at this custom header?
I'm also not sure if there's any caching by default or if it's turned off etc
Giant panda
Yes it cached by default when you use wrapped native fetch API, you then have the choice to opt out. You also don't need to use API routes to enjoy caching, I made that mistake
Gray WagtailOP
Right, all good, just needed to make sure I wasn't going crazy 😄