Write JavaScript like a pro. Javascript Icon

Follow the ultimate JavaScript roadmap.

Cache-busting in Jekyll, GitHub pages

I’ve always found updating my blog an interesting feat, however with several million users per year (you crazy cats) cache-busting is something I’ve recently been thinking since rolling out my new blog design. Implementing cache-busting each time I make a change will allow the user’s browser to download the latest assets, therefore I get no image/style/layout breakages until a hard refresh.

Cache-busting assets

This is actually a very simple trick by essentially adding a unix timestamp to asset urls.

Angular Directives In-Depth eBook Cover

Free eBook

Directives, simple right? Wrong! On the outside they look simple, but even skilled Angular devs haven’t grasped every concept in this eBook.

  • Green Tick Icon Observables and Async Pipe
  • Green Tick Icon Identity Checking and Performance
  • Green Tick Icon Web Components <ng-template> syntax
  • Green Tick Icon <ng-container> and Observable Composition
  • Green Tick Icon Advanced Rendering Patterns
  • Green Tick Icon Setters and Getters for Styles and Class Bindings

For example, here was my stylesheet before implementing cache-busting:

<link href="{{ "/css/main.css" | prepend: site.baseurl }}" rel="stylesheet">

This would then compile and render out this once I made a change to my website:

<link href="/css/main.css" rel="stylesheet">

To add cache-busting, I can simply append the site.now global to the end of my assets, and force it to a unix timestamp:

<link href="{{ "/css/main.css" | prepend: site.baseurl }}?{{ site.time | date: '%s%N' }}" rel="stylesheet">

This will then compile and render out the current timestamp everytime I make a change to my blog, as the site is statically rendered on the server upon changing something:

<link href="/css/main.css?1477265627121082292" rel="stylesheet">

At the time of writing this post, that’s what my current blog is displaying. Once I’ve posted this blog (i.e. now, as you’re reading) it will have changed again. This means no hard refreshes for browsers or funky styles being shown if you’re making important site updates.

Using Jekyll’s Sass

Because I’m using _sass as a base folder for my CSS, to tell Jekyll to compile with Sass, everytime I make a style change as well, Jekyll will recompile and redeploy to my website. If you’re using a script (such as gulp-sass for example) then updating styles alone may not work.

Learn JavaScript the right way.

The most complete guide to learning JavaScript ever built.
Trusted by 82,951 students.

Todd Motto

with Todd Motto

Google Developer Expert icon Google Developer Expert

Related blogs 🚀

Free eBooks:

Angular Directives In-Depth eBook Cover

JavaScript Array Methods eBook Cover

NestJS Build a RESTful CRUD API eBook Cover