Best practice would be to ensure the resource (that you don't want to be cached by either the browser or an intermediary proxy/cache/cdn) has the appropriate Cache-Control
header set on its response:
Cache-Control: no-cache, no-store, private, max-age=0, max-stale=0
The example contains several directives that control caching behavior:
no-cache
: This directive indicates that the response should not be served from cache without first revalidating it with the server. The client must send a request to the server to check if the cached response is still valid before using it.
no-store
: This directive specifies that the response should not be stored in any cache, including the browser cache or intermediate caches. Each time the client needs the resource, it must make a request to the server.
private
: This directive indicates that the response is intended for a single user and should not be cached by shared caches, such as proxy servers. Private responses are typically specific to a user and contain sensitive information.
max-age=0
: This directive sets the maximum time (in seconds) that the response can be considered fresh or valid. A value of zero indicates that the response has already expired and must be revalidated with the server before it can be used.
max-stale=0
: This directive indicates that the client should not accept a stale response from the cache. If the cached response is no longer fresh, the client must revalidate it with the server.
EDIT: Just to be clear, this Cache-Control
header should only be set for the resource you want to not have cached. I would not recommend setting this generally for all responses.
Additionally, if you have access to the CDN (in this case Fastly), then you'll need to add some custom VCL.
The Fastly documentation has a page on uploading custom VCL so I would recommend having a read-through that (there might already be a custom “main” VCL file; if so just edit that existing file).
NOTE: You'll find more info for "Adding VCL to your service configuration" on the Fastly Developer Hub.
If you're editing an existing VCL file you'll be looking for a vcl_recv
subroutine.
If you're creating a custom VCL for the first time, then you'll want to start by using the Fastly boilerplate VCL.
In the vcl_recv
you’ll want to add the following condition:
if (req.url ~ "^/path/to/specific/file\.ext$") {
return(pass);
}
The return(pass)
is what tells Fastly to skip looking up the resource in its cache and to go straight to the origin server to retrieve it instead.
Additional References
I've already linked to various documentation resources, but here are some more that might be of interest: