Busting The Cache
Last week, I completed my new hexo theme, but newly added page were broken because the stylesheet were not updated with the page. This was caused by the cache either on browser side or on the CDN of github pages side. So I decided to solve this issue by using a method to busting the cache whenever I have updated the assets files.
If anyone have ever used yeoman to generate an angular project. You may find a grunt task in the generated Gruntfile called usemin. This task combined with several other task can concatenate your stylesheet and javascript files then minimize those files, at last a file revision task is executed internally. The key of busting cache is the file revision task which will calculate your files’ hash value and use the value to rename your file, this will totally solve the cache issue. whenever your file changes, the hash value changes and you get a different filename of assets.
Back to the hexo theme project, I found it is hard to use the grunt task directly in my project because those task will modify the html or template file to update the script
and link
tag in order to
revision the assets file name. But hexo template file should only be modifed by programmer and generate html file by hexo. So I think it’s time to write a similar procedure by myself using hexo api.
Hexo provide two api helper and generator to generate tags in template and generate files with specified path. I will use this two api to keep the reference in template update and generate the new file with hash prefix file name. Because I can’t ensure the execution order of these two api, the concat and minified operation are abandoned. First let’s write a file hash method. This method accepts two arguments: file absolute path and hash digit length. because we don’t need the entire hash string to name asset file. the return value will be the new file name prefixed with a hash string
1 | var fs = require('fs'), |
This method will be called by helper
and generator
to get the assets files’ hash value file name.
Now let’s begin from the generator
. this api will let me to provide an array which tell hexo to write some files with specified path. I can copy the assets file with hashed file name.
1 | var themeSourcePath = path.resolve(__dirname, '../source'); // we need the theme source path to find the assets folder |
This generator will generate a set of new file with the hash prefix and its original folder structure in public
folder when you run hexo generate
.
With the hash prefixed file generated properly, we have to update the reference in script
and link
tag. here we using helper
to define an ejs helper called usemin
to generate our script
and link
tag.
1 | hexo.extend.helper.register('usemin', function(input) { |
Put this scripts file in scripts folder under my theme directory, the script will be loaded by hexo automatically. Now there are only one step to get the goal, modify template using the usemin
helper
In head.ejs
1 | <head> |
In scripts.ejs
1 | <!-- other tags... --> |
When I run hexo generate
, the index.html will be generated by hexo which contains head.ejs and scripts.ejs part
1 | <head> |