Select Page

Date-based cache busting: hands-free asset refreshing during site development

Last updated on

In a previous post about cache busting, I described a way to force a visitor’s web browser to grab the latest version of a site’s CSS or JS assets. In a production environment, that technique is an ideal solution for the problem of old cache data and is a tool you should keep in your back pocket at all times. During development, though, it’s not always convenient to manually update these includes upon each minor tweak to a stylesheet or javascript file.

As a developer, you’ve likely become familiar with forcing a refresh of all site data and aren’t losing any sleep over the issue. Clients, on the other hand, are (almost by definition) wholly unaware of what a browser cache is, why it exists, or how to clear it. If you’re tired of educating each client on this issue and reminding them about it multiple times throughout their project then this tip is for you.

Automatic asset refresh based on access date

It’s no secret that LightCMS has a built-in date token. What you might not know is that this token can also be used to tell you—down to the second—exactly what time a page is loaded. With a little creative use we can repurpose this functionality by inserting it into a file include and taking advantage of time’s inevitable march forward to trick browsers into always retrieving the current version of that file from the server.

We’re going to create a full timestamp with a custom formatted date token<$date format="yyyy-MM-dd_HH:mm:ss"/$> and use it in the query string for a stylesheet include. Normally that would look something like this:

That doesn’t work, though. LightCMS goofs up the include and leaves you without a proper stylesheet path.

Instead, we’ll need to use the full absolute path to the file:

If you view the page source you’ll see the token has been replaced with a timestamp that changes with every tick of the clock:

Wrapping up

From now until the time you remove the date token every visitor will get the latest copy of your file. It’s a perfect way to make sure your client is always looking at the same version of the site as you—just don’t forget to remove it when you go live or your load times will suffer!

About Denny

I'm a developer who specializes in working with agencies, freelancers, and website owners to take their LightCMS-based sites to the next level. Get in touch if you'd like to learn how I can help with your next LightCMS project.