Authentication and stale-while-revalidate #1228
-
Hello everyone! So I added authentication to my app following the Jokes example, and it works great with no cache-control headers being set. However, on pages that I want to cache (due to some slow requests) I've opted to use a stale-while-revalidate cache-control header, but this means that the authenticated UI is also getting cached. Is there a way to add authentication but also cache any pages that I need to? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
In general you don't cache responses that the user has the ability to change. In practice that's pretty much any data tied to a user. So the answer is, don't use HTTP caching for those. HTTP caching is for public facing data that the user can't change. General best practice is to use a server side cache (redis, memcached, etc.) and then expire those caches on mutations, or use a persistence layer that's fast already: AWS DynamoDB, Cloudflare KV/Durable Objects, Fly.io's postgres read replicas, FaunaDB, and CMS's that already built something like this into them. To speed up authenticated UI, you can check the In fact, the direction we're headed with Remix is to push your apps, and your data to the edge, which means everything is fast without ever needing to reach for HTTP caching. |
Beta Was this translation helpful? Give feedback.
In general you don't cache responses that the user has the ability to change. In practice that's pretty much any data tied to a user. So the answer is, don't use HTTP caching for those.
HTTP caching is for public facing data that the user can't change.
General best practice is to use a server side cache (redis, memcached, etc.) and then expire those caches on mutations, or use a persistence layer that's fast already: AWS DynamoDB, Cloudflare KV/Durable Objects, Fly.io's postgres read replicas, FaunaDB, and CMS's that already built something like this into them.
To speed up authenticated UI, you can check the
request.headers.get("Purpose") === "pefetch"
and add a short (3 seconds?)max-age
…