2

What does everyone consider a sensible cache duration to use when using Assets with S3/Cloudfront? Is 3 hours sensible or should it be longer?

Secondly, does this have any impact on the assets being displayed within Craft? Listing Asset directories takes a while due to my connection speed I'm sure but aren't these assets cached locally?

1

With the correct cache busting, files can remain cached for as long as you like. Craft handles a lot of this for you.

I typically cache items for a year but do take caution to ensure every reference to assets within the site has a cache busting string on it. It's important to note that Cloudfront needs to be setup to use these query strings.

On the frontend, Craft appends an mtime attribute to the query string. If you ever replace an asset, the mtime is updated and subsequently a new cache is created.

Within the Craft CP, thumbnails of your remote images are generated rather than serving them from a CloudFront URL, generally there are no worries with caching here as Craft handles that.

Volumes with an expiry set will automatically load the mtime parameter when fetching the URL from Craft. Alternatively, you can implement your own cache busting with the url() function shown here.

4
  • Thanks Matt. Do you have any links to or directions on the cache-busting? At the moment I have the S3 cache in Craft set to the default 3 hours. Apr 28 at 3:39
  • I've updated my answer to contain a link to setting up your own cache busting within Craft. However, given that you are already setting an expiry, the mtime attribute should be appended when you fetch the URL from Craft. You will likely need the Base URL and CloudFront distribution ID set to the corresponding values at CloudFront for that to work. I am unable to find any documentation to support that but this answer seems to show the same thing. Apr 28 at 7:18
  • Thanks again. So if the URL strings already have an mtime attribute (they do) I don't need to do anything? Apr 29 at 1:51
  • No problem, yes so long as CloudFront is setup to cache query strings. If you were to request image.png?mtime=1, CloudFront will cache that. If you then replace the asset within Craft, Craft will automatically adjust the value of mtime. No cache will exist at CloudFront for the new mtime so it will be re-fetched from the origin. Apr 29 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.