Controlling HTTP caching impossible with App Router? #51460
Replies: 6 comments 4 replies
-
|
I've been loosing days trying to understand how to have |
Beta Was this translation helpful? Give feedback.
-
|
Agree with everything in the original post. Caching at the edge is critically important to reduce network latency. We can't have requests going all the way back to origin just to serve the same cached content. @hesalx Minor typo in the issue: "304 Not Found" should be "304 Not Modified" |
Beta Was this translation helpful? Give feedback.
-
|
I completely agree too. You can patch Next.js to include a |
Beta Was this translation helpful? Give feedback.
-
|
This is driving me crazy! Next 14 looks like a money printing machine for the assholes at Vercel! Upgrading force me to use app router, which forced me to have many dynamic pages that just weren't dynamic in ISG, and they are freaking impossible to cache without a bunch of hacks! Next is the only major framework I know that makes things way worse on an upgrade. |
Beta Was this translation helpful? Give feedback.
-
|
Any news on that ? We're migrating to next for a big website and it's crazy there no way to at least override max-age |
Beta Was this translation helpful? Give feedback.
-
|
It's now possible to customize |
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.
-
HTTP cache is a fundamental and critical part of a web application, especially if the latter is able to serve fully rendered HTML pages.
App Router provides easier than ever SSR but seems so completely miss some of the other features critical for the purpose for which SSR is desired in the first place.
The problem
In a nutshell, the developer has no control over
Last-Modified,Etag, and status code (i.e.304) of a generated Page when using the App Router.App Router fails to provide this functionality even in the simplest of scenarios:
/api/v1/items/:id;/items/:idwhose job is to provide UI on top of the API data and that does exactly one fetch to the API above;The issues with App Router:
fetchdepends onparams, however dynamic rendering always returns200 OK;Cache-Control: no-cachein thefetchrequest rendering any API-level CDN or caching completely useless.fetchresponse is304there is not way to forward it to the client or render any data (there is no response body);if-Modified-Sincewith the resource data (likeupdatedAt) because there is no way to set response status code to304or setLast-Modified(to be later received asif-Modified-Since) in the first place;The desired capabilities:
Etagof the primary API resource to the client and forwardIf-None-Matchto the API (ETag is URL-scoped so this is perfectly fine if the page can be considered just a different representation of the API resource);If-None-Matchinfetchrequests when the page's ETag is deterministically derived from resource's ETag, such as having app version to invalidate layout changes without invalidating API-level chache;Last-Modifiedamong all the fetched resources to the client or setLast-Modifiedbased on the application-specific property (updatedAt,editedAt, etc.) of a fetched resource; and forwardIf-Modified-Sincefrom the client to the API;304if all fetched resources returned304; or manually set the response code to304if the primary API resource returned304; or set the response code to304manually upon a custom comparison ofIf-Modified-Sincewith the application-specific property (updatedAt,editedAt, etc.);Cache-Controlvalue based on the fetched resources (maxageof the fetched resource) or based on the request parameters (privateif logged in,publicis logged out, etc.).Why
Last-Modified,Etag,Cache-Control, and304 Not Modifiedare importantSource: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified
Source: https://developers.google.com/search/docs/crawling-indexing/large-site-managing-crawl-budget
Note that the support of
If-None-Matchimplies the support and use ofEtag.Soure: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html
There are zero mentions of these concerns in the documentation. Please keep in mind that the way Next.js handles
fetchcache is a separate concern. It is either a server-side cache (orthogonal to HTTP cache) or affectsfetchon the client-side when the app behaves like a Single-Page Application and not when requesting an entire page (like a crawler, a CDN, or the first visit in a user session).This makes it nearly impossible to implement efficient caching strategies both for SEO or even having a simple CDN in front of a Next.js app.
Beta Was this translation helpful? Give feedback.
All reactions