Jump to content

Add Expires Headers when uploading to S3 to improve caching

The Old Man

Recommended Posts

I've been doing a lot of testing with S3/Cloudfront hosted files of late, trying to improve my GT Metrix Pagespeed scores.

Gzipped files in particular uploaded via IPS 4.3 to S3 are flagged by GT Metrix as lacking Specify a Vary: Accept-Encoding header - The following publicly cacheable, compressible resources should have a "Vary: Accept-Encoding" header. This results in a fairly large drop in score especially if both JS and CSS files are missing these headers.


Rather confusingly, you can't actually fix this by adding Vary: Accept-Encoding via the Cloudfront Settings as the advice suggests. I've tried every combination and method of S3/Cloudfront Settings, I can think of or find.

You CAN however fix this every time by manually adding simple Expires headers to those affected files over at S3, but you must invalidate your Cloudfront Distribution cache (and then finally at Cloudflare if using it).



Add the missing Expires header, changing the Cache-Control header to match if necessary/you prefer:-


You can see here I've changed caching and time before Cloudfront checks for an updated file to 8 days rather than 1 year, the minimum required by GTMetrix to pass this test on a cachable static JS file.


Remember that you must invalidate your Cloudfront Distribution cache after editing the Expires headers either individually or via the parent folder (and also at Cloudflare if using it).

I've tested this solution on all of my sites and it works, you can get an A97 or higher Pagespeed rating depending upon what else you do and external 3rd parties aren't affecting your score.


Adlago would be proud...


However whilst this manual intervention works okay for longer retention files like images etc that won't change, the issue is that when and if your IPS recaches itself (or you do it via the Support Tool), IPS uploads brand new cached JS/CSS files to S3. The next time you or GTMetrix bench-tests your site, the score (and performance and hence potential SEO ranking) falls. Then you have to manually add the Expires headers back to those affected temporary cache files.


We're now receiving A(100) for this recommendation!



Now Adlago would be even prouder, A97 rating admittedly not higher, but I'll take a 50% boost in page loading speed! 

The permanent solution, aside from getting stuck in this infinite loop (or writing a Node.js script at LambdaEdge to do it automatically when triggered on every request), is if IPS would automatically add the Expires Headers to resources each time it uploads the files for us to S3, via the S3 Restful API. Secondly, have the option to add headers or amend the values would be the icing on the cake.

Link to comment
Share on other sites

Yes, it's a truly royal PITA. I'm so fed up of manually adding these missing headers and invalidating Cloudfront and Cloudflare caches every time the theme JS or CSS is uploaded ,and multiply that by the number of sites you have.

Have recently tried adding exposeheaders tags to the CORS setup on my S3 bucket, but it made no difference. Unless the Expires tags are manually added to the files Cloudfront or S3 won't forward them to the visitors browser. Adding them automatically when they being uploaded is still the best potential option.

Link to comment
Share on other sites

  • 1 month later...

Hi, no but I wasn't actually expecting to. However I've now been able to create a plug-in which does the job.

I've now tested it on all of my 4 of my IPS Community sites using S3 in the last 48hrs and it works fine.

I'll aiming to upload it to the Marketplace later today, and will post here if and when it's approved.


Link to comment
Share on other sites

Thanks @asigno. I don't know but I've uploaded it to the Marketplace earlier this evening, so hopefully once approved it will save some time and repetition manually adding them for those who want the extra caching headers. Sorry for the delay, I wanted to do some additional testing.



Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...