#缓存 #资源 #哈希 #静态网站 #破坏者

已删除 缓存破坏者

缓存破坏者帮助静态网站优雅地缓存资源

0.1.2 2021年3月12日
0.1.1 2021年3月12日
0.1.0 2021年3月12日

#46 in #静态网站

自定义许可

13KB
173 代码行

缓存破坏者

crates.io

缓存破坏者 帮助破坏静态网站的陈旧缓存。例如,假设你有以下结构,可能由其他工具(如Jekyll)生成

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

你的索引页面看起来像这样

<!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>

缓存破坏者 可以更新你的HTML以更新所有带有内容哈希的资源

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

这将更新你的网站结构为以下形式

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

并且也会更新所有HTML文件以指向正确的哈希文件

<!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>

安装

使用 cargo 安装

cargo install cache-buster

用法

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

支持的标签

目前,缓存破坏者 作用于所有 <link rel="stylesheet" href="..."><script src="..."></script><img src="..." /> 标签。

待办事项

  • 添加测试
  • 添加处理CSS文件的支持

许可

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.

依赖

~10–20MB
~371K SLoC