#cache #assets #hashing #static-website #buster

yanked cache-buster

cache-buster helps static sites keep assets cached gracefully

0.1.2 Mar 12, 2021
0.1.1 Mar 12, 2021
0.1.0 Mar 12, 2021

#46 in #static-website

Custom license

13KB
173 lines

cache-buster

crates.io

cache-buster helps busting stale cache for static websites. For instance, let's suppose you have the following structure, maybe generated by some other tool such as Jekyll:

.
├── assets
│   ├── scripts
│   │   └── main.js
│   └── style
│       └── main.css
└── index.html

And your index page is as beautiful as

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="/assets/style/main.css">
    <script src="http://google.com/foo/bar"></script>
    <script src="https://my.website/assets/scripts/main.js"></script>
    <title>My Website</title>
</head>
<body>
Hello, this is my gorgeous website.
</body>
</html>

cache-buster can update your HTML to update all assets with an hash of its contents:

$ cache-buster --assets assets --base-url https://my.website

Which will update your website to the following structure:

.
├── assets
│   ├── scripts
│   │   └── main_88782689275158862f158fb9030e2a2599bd82bb.js
│   └── style
│       └── main_1ae3e9bf3a50ebd227b33460915a7672e1f94382.css
└── index.html

and also update all your HTML files to point to the correct hashed files:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="assets/style/main_1ae3e9bf3a50ebd227b33460915a7672e1f94382.css">
    <script src="http://google.com/foo/bar"></script>
    <script src="https://my.website/assets/scripts/main_88782689275158862f158fb9030e2a2599bd82bb.js"></script>
    <title>My Website</title>
</head>
<body>
Hello, this is my gorgeous website.
</body>
</html>

Installing

Install using cargo:

cargo install cache-buster

Usage

 # cd to your website output 
 ▲ cd my-website 
 
 # and execute cache-buster
 ▲ cache-buster --assets assets --base-url my.website 
 
 # That's it! 

Supported Tags

Currently, cache-buster acts on all <link rel="stylesheet" href="...">, <script src="..."></script>, and <img src="..." /> tags.

TODO

  • Add Tests
  • Add support to process CSS files

License

MIT License

Copyright (c) 2021 Victor Gama

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Dependencies

~10–20MB
~371K SLoC