Created
October 12, 2015 06:42
-
-
Save fouad/a80f17b34698eebd856c to your computer and use it in GitHub Desktop.
Purge CloudFlare cache on deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"email": "[email protected]", | |
"zone": "CLOUDFLARE_ZONE", | |
"token": "CLOUDFLARE_TOKEN" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp') | |
var request = require('request') | |
gulp.task('purge-cache', function () { | |
var cf = require('./cloudflare.json') | |
request({ | |
url: 'https://api.cloudflare.com/client/v4/zones/' + cf.zone + '/purge_cache', | |
method: 'DELETE', | |
headers: { | |
'X-AUTH-EMAIL': cf.email, | |
'X-Auth-Key': cf.token, | |
'Content-Type': 'application/json' | |
}, | |
body: { | |
purge_everything: true | |
}, | |
json: true | |
}, function (err, response, body) { | |
if (err) { | |
throw err | |
} | |
console.log('CloudFlare cache purge response') | |
console.log(body) | |
}) | |
}) |
how about purging just one file @fouad?
@hadifarnoud - here's how. files
can be just an array of URLs or it can be objects to purge selectively by origin etc. Both included here.
gulp.task('purge-cache', function () {
let config = require(path.join(__dirname, 'config.json'));
let cf = config.cloudflare;
request({
url: 'https://api.cloudflare.com/client/v4/zones/' + cf.zone + '/purge_cache',
method: 'DELETE',
headers: {
'X-AUTH-EMAIL': cf.email,
'X-Auth-Key': cf.token,
'Content-Type': 'application/json'
},
body: {
purge_everything: false
files: [
"http://www.example.com/css/styles.css",
{
"url": "http://www.example.com/cat_picture.jpg",
"headers": {
"Origin": "cloudflare.com",
"CF-IPCountry": "US",
"CF-Device-Type": "desktop"
}
}
]
},
json: true
}, function (err, response, body) {
if (err) { console.error("!!! Error purging Cloudflare cache"); return; }
console.log('Cloudflare cache purge response: ');
console.log(body);
});
});
Nice, thanks for that! :)
Btw, I had to use the Global API instead of a new Token, didn't work otherwise not sure why.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can get zone id by listing zones https://api.cloudflare.com/#zone-list-zones and your api key from https://www.cloudflare.com/a/account/my-account