+++
date = '2024-11-14T14:24:21+01:00'
draft = false
title = 'Invalidating the browser cache'
tags = ['css','html','linux']
+++
**I had a bit of an issue with my [website](https://demos.ajstepien.xyz) recently.**
I pushed some changes incorporating images for the first time (I know -- very swish, very modern), and everything seemed to be working just fine, but when I loaded the production site in Firefox... the images were not styled. Stranger still, they *were* styled when I loaded the same page in Chrome.
The experienced computer touchers amongst you will be saying "this is obviously a cache problem", and you're right, it is obviously a cache problem. Pressing `CTR + SHIFT + R` (which forces Firefox to clear the cache and do a full reload) proved this thesis, and solved the immediate problem for me, on my machine. But what about other people's machines? **I needed to cache-bust.**
Post-processors such as Tailwind have built-in methods for this, but I want something simpler than that for this project: something I can code myself, without losing sight of what's happening under the hood.
## Invalidating cached HTML
The best way to deal with the caching problem is to tell the browser not to cache our HTML in the first place. Yes, this is kind of cheating (ok, it's 100% cheating), but c'mon bro, it's just one little HTML file, and browsers only cache those because most websites these days are SPAs whose HTML rarely changes.
I can stop HTML files from getting cached by adding the following meta tag. In this case, I'm adding it to `index.html`.
```html
```
## Invalidating cached CSS
That's all well and good, but what I really need is for the browser to recognize my *CSS* as a new file and load it anew from the server. One way to achieve this would be to change the filename whenever I want to bust the cache, but this would get tedious very quickly. What's more, as far as Git is concerned, I'd be deleting the CSS file and writing a new one with every deployment, which would be an intolerable mess. Surely there's a better way?
Of course there is.
### Using a query
Look at this:
```html
```
As I'm requesting the file via http, I can append a query. Awesome. Not awesome enough though. I'm too lazy to edit this line of code every time I push a commit, and, being human, I'll probably forget at a critical moment. This can only mean one thing: it's time to bash (🤣) out a quick build script.
### Automating query insertion
```bash
#!/usr/bin/env bash
COMMIT="$(git rev-parse HEAD)"
sed -i "s/css?v=\w*/css?${COMMIT}/g" index.html
```
Let's talk about what's happening here:
`COMMIT="$(git rev-parse HEAD)"` gets the commit id from Git and assigns it to the variable `$COMMIT`.
Then, `sed -i "s/css?=\w*/css?${COMMIT}/g" index.html` does a find and replace in the file `index.html`. The regular expression `css?=\w*` matches 'css?=' plus any number of contiguous alphanumeric characters (everything until the next quote mark) before replacing these alphanumeric characters with the commit id. The flag `-i` tells `sed` to edit the file in place. The `g` tells it to perform the operation on the whole file.
Now, whenever we push a new commit, any CSS imports in `index.html` will be changed to something like this:
```html
```
Now I just need to add the build script to my Jenkinsfile...
```groovy
stage('build'){
steps{
sh './build.sh'
}
}
```
... and the problem is solved.
Pretty neat, huh?
There's just one thing bugging me: surely I do actually want the CSS to be cached *sometimes*. Caching exists for a reason, and I don't want to sacrifice performance. Maybe I can modify the build script so that it only updates the CSS imports when the CSS files have changed... Sounds like a topic for another blogpost...