Skip to content

Commit d905115

Browse files
committed
Docs: Add recipe for static asset revisioning
1 parent b7b70b8 commit d905115

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

docs/recipes/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@
2828
* [Rollup with rollup-stream](rollup-with-rollup-stream.md)
2929
* [Run gulp task via cron job](cron-task.md)
3030
* [Running shell commands](running-shell-commands.md)
31+
* [Static asset revisioning](static-asset-revisioning.md)
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Sass and Nunjucks with static asset revisioning
2+
3+
Static asset revisioning by appending content hash to filenames. Make sure to set the files to [never expire](http://developer.yahoo.com/performance/rules.html#expires) for this to have an effect.
4+
5+
Sass and Nunjucks are here just to demonstrate how to implement static asset revisioning in a more real-world scenario. The content has will be appended in production, so it's recommended to add the following npm scripts to `package.json`:
6+
7+
```json
8+
{
9+
"scripts": {
10+
"dev": "gulp-dev",
11+
"build": "NODE_ENV=production gulp build"
12+
}
13+
}
14+
```
15+
16+
Now to install the required dependencies:
17+
18+
```sh
19+
npm install --save-dev gulp gulp-rev gulp-rev-rewrite gulp-sass gulp-nunjucks gulp-if del
20+
```
21+
22+
And, finally, `gulpfile.js`:
23+
24+
```js
25+
const gulp = require('gulp')
26+
const sass = require('gulp-sass')
27+
const nunjucks = require('gulp-nunjucks')
28+
const rev = require('gulp-rev')
29+
const revRewrite = require('gulp-rev-rewrite')
30+
const gulpIf = require('gulp-if')
31+
const del = require('del')
32+
33+
const isProd = process.env.NODE_ENV === 'production'
34+
35+
function clean() {
36+
return del('dist')
37+
}
38+
39+
function styles() {
40+
return gulp
41+
.src('styles/style.scss')
42+
.pipe(sass().on('error', sass.logError))
43+
// appends the content hash
44+
.pipe(gulpIf(isProd, rev()))
45+
.pipe(gulp.dest('dist'))
46+
// output rev-manifest.json
47+
.pipe(gulpIf(isProd, rev.manifest()))
48+
.pipe(gulp.dest('dist'))
49+
}
50+
51+
function views() {
52+
const manifest = gulp.src(
53+
'dist/rev-manifest.json',
54+
// in development the manifest doesn't exist
55+
{ allowEmpty: !isProd }
56+
)
57+
return gulp
58+
.src('views/**/*.njk')
59+
.pipe(nunjucks.compile({ title: 'Hello world!' }))
60+
// this updates the reference(s) to revisioned assets
61+
.pipe(gulpIf(isProd, revRewrite({ manifest })))
62+
.pipe(gulp.dest('dist'))
63+
}
64+
65+
function watch() {
66+
gulp.watch('styles/**/*.scss')
67+
gulp.watch('views/**/*.njk')
68+
}
69+
70+
// after everything is done, we no longer need the manifest file
71+
function deleteRevManifest() {
72+
return del('dist/rev-manifest.json')
73+
}
74+
75+
exports.dev = gulp.series(clean, gulp.parallel(styles, views), watch)
76+
// in build it's important that "views" runs AFTER "styles"
77+
// if it runs before, the manifest file won't exist yet
78+
exports.build = gulp.series(clean, styles, views, deleteRevManifest)
79+
```

0 commit comments

Comments
 (0)