Skip to content

jallajs/cccpurge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cccpurge

npm version build status downloads js-standard-style

Purge Cloudflare cache of all routes served by a Choo app. This is usefull when enabling cache for all content and you later need to purge the cache for html pages due to updated content or when publishing a new version of your app. E.g. one could use this as part of a webhook that is triggerd by changes made to a CMS or as part of a deploy script.

Usage

You'll need to get your Cloudflare Zone ID, it's on the dashboard overview when signing into your Cloudflare account. Just below that is a link to get your API key.

#!/usr/bin/env node

var cccpurge = require('cccpurge')

cccpurge(require('./index'), {
  root: 'https://www.my-blog.com',
  email: 'foo@my-blog.com',
  zone: '7sef78we7hwhefw3hri3uhriu32rwehf',
  key: '0046ffew5f560675hny5765r7gre6005reg05'
}, console.log)

Dynamic routes

Dynamics routes (wildcards/params) are supported but you'll have to supply a function that resolves them to actual urls. The resolve function is given the route (e.g. /posts/:post) and a callback. How you resolve /posts/:post to e.g. /post/my-first-post is completely up to you. Here's an example using Prismic.

#!/usr/bin/env node

var Prismic = require('prismic-javascript')
var cccpurge = require('cccpurge')
var app = require('./index')
var opts = {
  resolve: resolve,
  root: 'https://www.my-blog.com',
  email: 'foo@my-blog.com',
  zone: '7sef78we7hwhefw3hri3uhriu32rwehf',
  key: '0046ffew5f560675hny5765r7gre6005reg05'
}

cccpurge(app, opts, done)

function done (err, response) {
  if (err) console.error(err)
  else console.log('Cache purged!')
  process.exit(0)
}

function resolve (route, done) {
  // only bother purging posts
  if (route !== '/posts/:post') return done(null)

  // fetch posts from prismic api
  Prismic.getApi('https://my-site.cdn.prismic.io/api/v2')).then(function (api) {
    return api.query(
      Prismic.Predicates.at('document.type', 'blog-post')
    ).then(function (response) {
      done(null, response.results.map((post) => `/posts/${post.uid}`))
    })
  }).catch(done)
}

Limit

Cloudinary has a limit of maximum 30 urls per call to the purge endpoint. We respect that limit but it can be overridden by setting opts.limit to any number. Requests are made in parallel with a miximum of limit urls per request.

Options

  • opts.limit: default: 30. Number of urls submited per request.
  • opts.urls: default: []. List of urls (other than routes) you want purged.
  • opts.zone: Cloudinary Zone ID.
  • opts.email: Cloudinary account email address.
  • opts.key: Cloudinary API Key.
  • opts.resolve: Function which resolves dynamic routes to urls.

License

MIT

About

Choo Cloudflare Cache Purge – purge all routes served by choo app

Resources

Stars

Watchers

Forks

Packages

No packages published