• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

bustardcelly/gulp-rev-append: gulp task to cache-busting files using file hash

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

bustardcelly/gulp-rev-append

开源软件地址(OpenSource Url):

https://github.com/bustardcelly/gulp-rev-append

开源编程语言(OpenSource Language):

JavaScript 76.6%

开源软件介绍(OpenSource Introduction):

gulp-rev-append

gulp plugin for cache-busting files using query string file hash

Build Status

installation

$ npm install gulp-rev-append --save-dev

how?

gulpfile.js

var rev = require('gulp-rev-append');

gulp.task('rev', function() {
  gulp.src('./index.html')
    .pipe(rev())
    .pipe(gulp.dest('.'));
});

terminal

$ gulp rev

what?

The gulp-rev-append plugins allows for appending a query-string file hash to dependencies declared in html files defined using the following regex: (?:href|src)="(.*)[\?]rev=(.*)[\"]

That's fancy talk for any stylesheet or script declarations that are declared in an html file such as the following:

<!doctype html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="style/style-one.css?rev=@@hash">
    <script src="script/script-one.js?rev=@@hash"></script>
    <script src="script/script-two.js"></script>
  </head>
  <body>
    <div><p>hello, world!</p></div>
    <script src="script/script-three.js?rev=@@hash"></script>
  </body>
</html>

will turn into something similar as the following after running gulp-rev-append:

<!doctype html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="style/style-one.css?rev=d65aaba987e9c1eefeb4be9cfd34e0de">
    <script src="script/script-one.js?rev=17a5da6c8a2d875cf48aefb722eefa07"></script>
    <script src="script/script-two.js"></script>
  </head>
  <body>
    <div><p>hello, world!</p></div>
    <script src="script/script-three.js?rev=5cadf43edba6a97980d42331f9fffd17"></script>
  </body>
</html>

Any subsequent runs of the gulp-rev-append file will change the output only if the target file(s) declared have been modified. This is because the revision hash is computed using the target file contents.

The only requirement is that the dependency to be appended with the hash be declared using ?rev=. The @@hash is not required, and any value will be overriden as the dependency file contents change.

why?

I wanted to easily define dependencies that require simple cache-busting by using a query-string hash. The hash is based on file content, so any modification to the file dependency would result in a change to the generated and appended hash - effectively cache-busting the dependency in simple scenarios.

Be Warned: Using query strings to cache-bust dependencies isn't fool proof.

Google | Leverage Proxy Cache Article
Steve Souders | Revving Filenames: don't use querystring

Other plugins that may work for your situation:

There are several gulp plugins that already support revisioning and cache-busting: https://www.npmjs.org/search?q=gulp-rev

I created this plugin as it fit my needs more clearly in:

  • enabling cache-busting by appending a file hash on query string
  • not requiring additionally markup commenting to declare dependencies to be modified
  • not generating an additional manifest to be a dependency in file access for production-level application

For its particular use, I am not concerned with firewalls or proxy cache; I was trying to develop a simple web-based mobile site on a desktop while testing on devices and cache-ing was giving me a headache. I did not want to modify my workflow and build to accommodate an addition cache-manifest, nor did I foresee my work being in production in which I needed to support a more robust cache-busting technique.

If the intent of this plugin does not meet your needs, please checkout the other possible solutions made by some awesome developers.

Tests

$ npm run test

License

Copyright (c) 2014 Todd Anderson

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
jgable/gulp-cache: A cache proxy task for Gulp发布时间:2022-06-21
下一篇:
fourkitchens/emulsify: DEPRECATED (see README for new version)发布时间:2022-06-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap