Setting cache headers for static assets

For Workes & Pages, what is the name of the domain?

tatarintsev (dot) photography

What is the issue or error you’re encountering

I’ve migrated my pages static website to workers + static asset beta feature. Bunch of assets of that static site are immutable, however, I can’t seem to configure caching header for them: Cache-Control header is always public, max-age=0, must-revalidate.

What steps have you taken to resolve the issue?

  • Set up caching rules in Website settings that matches on static assets and overrides browser TTL to 1 year.
  • Put _headers file into assets directory with Cache-Control values I want

What are the steps to reproduce the issue?

  1. Run curl -svo /dev/null https://tatarintsev.photography/blurhash-worker.37aaa842adffbc17.js
  2. Observe Cache-Control header value
3 Likes

May I ask where do you host your assets? :thinking:
Is it R2 bucket or some other way?

May I ask if you’ve tried configuring the HTTP cache control headers as follows in the example from GitHub template for Workers? :thinking:

I found another helpful post with an example here:

I am not using Worker Sites (that your examples point to) or Pages, I am using new Workers Static Assets feature.

You can’t really do this today with Workers Assets, sadly.

I believe it’s something the team want to support at some point, but you can’t set specific headers on assets served by Workers Assets today.

I was facing the same issue, a solution for now is to create a Transform Rule: Create an HTTP response header modification rule in the dashboard
you have to define an expression to target the files, like for example (ends_with(http.request.uri.path, "css"))
and then modify response header: set a static Header Cache-Control to public, max-age=31536000, immutable

4 Likes

As far as I can tell, this is still happening. Thanks to the advice in this thread I created an HTTP Response Header Modification Rule and that worked. I have my local _headers file mirroring what I configured there, just in case it should start working correctly. It’s odd as it says that it’s uploading _headers to the Static Assets but then it seems to just not doing anything with it? Oh well. Will continue to watch this thread for updates.

I didn’t try it myself because I’m using Pages, but this page seems to suggest that you have to use service bindings.