jonkemp / gulp-useref

Parse build blocks in HTML files to replace references to non-optimized scripts or stylesheets.
MIT License
705 stars 93 forks source link

What method can add a version number at the back of the file? #131

Closed rayliao closed 5 years ago

rayliao commented 8 years ago
<html>
<head>
    <!-- build:css css/combined.css -->
    <link href="css/one.css" rel="stylesheet">
    <link href="css/two.css" rel="stylesheet">
    <!-- endbuild -->
</head>
<body>
    <!-- build:js scripts/combined.js -->
    <script type="text/javascript" src="scripts/one.js"></script>
    <script type="text/javascript" src="scripts/two.js"></script>
    <!-- endbuild -->
</body>
</html>

I want the resulting HTML like this:

<html>
<head>
    <link rel="stylesheet" href="css/combined.css?v=20150829"/>
</head>
<body>
    <script src="scripts/combined.js?v=20150829"></script>
</body>
</html>
jonkemp commented 8 years ago

Here's one way to do it.

https://github.com/yeoman/generator-gulp-webapp/blob/master/docs/recipes/asset-revisioning.md

rayliao commented 8 years ago

gulp-rev appends content hashes like this:

unicorn.css → unicorn-d41d8cd98f.css

It change the name of the file, but I can't think of change the file name, is just add the version number: unicorn.css → unicorn.css?v=d41d8cd98f

manuel-di-iorio commented 8 years ago

If you do this with useref, the version hash will be useless, because it will be changed only when you change the file, losing the "no-cache stuff"

One way to do this is to correctly handle the cache headers with the server but sometimes it's not possible, so you could do it yourself. Example with php: echo "unicorn.css?v=" . strval(microtime(true));

Probably there is an automatic lib that can handle that for you

philBrown commented 8 years ago

@jonkemp

Here's one way to do it.

That obviously doesn't work with gulp-useref v3. Got any other suggestions?

jonkemp commented 8 years ago

I submitted a pull request to update that doc last night and it's been merged. I tested that myself and it works. :hand:

philBrown commented 8 years ago

@jonkemp thank you very much, that was definitely helpful.

OXOYO commented 8 years ago

@jonkemp It`s work. Thanks.

henriquechehad commented 7 years ago

Some update about doing:

unicorn.css → unicorn.css?v=d41d8cd98f instead: unicorn.css → unicorn-d41d8cd98f.css ?