Skip to content

Instantly share code, notes, and snippets.

@mariotacke
Last active July 5, 2021 07:44
Show Gist options
  • Save mariotacke/7ebe56e889fed4564ea3a995e585e6bc to your computer and use it in GitHub Desktop.
Save mariotacke/7ebe56e889fed4564ea3a995e585e6bc to your computer and use it in GitHub Desktop.
Gulp replace to invalidate cache

Gulp replace to invalidate cache

This demo replaces the string @@VERSION with the current unix timestamp when building with gulp. @@VERSION is a query parameter to bundle.js. To avoid caching bundle.js, we add the timestamp to it during each build.

Usage

npm install
npm run build
'use strict';
// ...
const gulp = require('gulp');
const replace = require('gulp-replace');
gulp.task('copy', () => {
return gulp.src('bundle.js')
.pipe(gulp.dest('dist'));
});
gulp.task('version', () => {
return gulp.src('index.html')
.pipe(replace('@@VERSION', Date.now()))
.pipe(gulp.dest('dist'));
});
gulp.task('default', ['copy', 'version']);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Gulp Replace Demo</title>
</head>
<body>
<h1>Gulp Replace Demo</h1>
</body>
<script src="bundle.js?v=@@VERSION" charset="utf-8"></script>
</html>
{
"name": "gulp-replace-demo",
"version": "1.0.0",
"description": "Gulp Replace Demo",
"scripts": {
"build": "./node_modules/.bin/gulp"
},
"author": "Mario Tacke <[email protected]>",
"license": "ISC",
"devDependencies": {
"gulp": "^3.9.1",
"gulp-replace": "^0.5.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment