custom-dev-tools / grunt-cache-killer

Grunt cache killer is a Grunt plugin that circumvents http(s) cache problems when asset files (such as css, js and img files) are updated. This plugin automatically updates the specified asset filename(s) and any reference to them within your specified (template) files.
MIT License
2 stars 0 forks source link
asset-management cache-busting grunt grunt-plugins gruntjs http-requests plugin

grunt-cache-killer

Node version Node version

NPM version Maintained NPN downloads

GitHub version GitHub License GitHub release date GitHub open issues GitHub closed issues


Ever wanted to ensure that your most recently deployed asset files (eg: css, js, img, etc) are being used instead of the old long-term cached versions?

Then look no further...!

This Grunt plugin inserts a cache avoiding string into your asset filename, then looks for and updates any reference to it within your template file(s).

Find this plugin at:

Table Of Contents

Getting Started

This plugin requires Grunt ^1.1.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as how to install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install --save-dev grunt-cache-killer

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-cache-killer');

Tip: Alternatively, install the load-grunt-tasks plugin to have cacheKiller load automatically.

The cacheKiller Task

Overview

In your project's Gruntfile, add a section named cacheKiller to the data object passed into grunt.initConfig().

grunt.initConfig({
  cacheKiller: {
    options: {
      prepend: '',
      append: '',
      mask: '{md5}',
      length: -1
    },
    taskName: {
      files: {
        'path/to/asset/filename[mask].min.css': ['path/to/template/using/asset/filename-1.html',
                                                 'path/to/template/using/asset/filename-2.html']
      }
    }
  }
});

Options

Usage

If this is the first time you are running cacheKiller, it will insert the mask value into the asset filename and update any reference to it within your nominated template file(s).

Running cacheKiller again will refresh the previously inserted mask value mask value of the asset filename with an updated value and again update any reference to it within your nominated template file(s).

Whether this is the 1st or 1,000th time your asset and template files have been updated, cacheKiller will insert the mask in the specified location each and every time.

Implementation is simple. Within the cacheKiller's files: node, place the [mask] placeholder within the asset filename where you would like the mask to be added.

// Here...
'public/css/website[mask].min.css'

// Or here...
'public/css/website.min[mask].css'

// Or anywhere else you like within the asset filename, pursuant to the below limitations.

Limitations

Usage Examples

Default Options

In this example, the default options are used.

grunt.initConfig({
  cacheKiller: {
    // options: {
    //   prepend: '',
    //   append: '',
    //   mask: '{md5}',
    //   length: -1
    // },
    taskName: {
      files: {
        'public/css/app[mask].min.css': ['app/views/templates/master.html']
      }
    }
  }
});

Before:

// Asset file.

public/css/app.min.css
<!-- master.html -->

<link href="https://www.site.com/css/app.min.css" rel="stylesheet">

After:

// Asset file.

public/css/app70a1d7fe6502fa887f5b810d9063da07.min.css
<!-- master.html -->

<link href="https://www.site.com/css/app70a1d7fe6502fa887f5b810d9063da07.min.css" rel="stylesheet">

Simple Custom Options

In this example, simple custom options are used.

grunt.initConfig({
  cacheKiller: {
    options: {
      mask: 's-rule' // String used instead of a cacheKiller mask function.
    },
    taskName: {
      files: {
        'public/css/app[mask].min.css': ['app/views/templates/master.html']
      }
    }
  }
});

Before:

// Asset file.

public/css/app.min.css
<!-- master.html -->

<link href="https://www.site.com/css/app.min.css" rel="stylesheet">

After:

// Asset file.

public/css/apps-rule.min.css
<!-- master.html -->

<link href="https://www.site.com/css/apps-rule.min.css" rel="stylesheet">

Complex Custom Options

In this example, complex custom options are used.

grunt.initConfig({
  cacheKiller: {
    options: {
      prepend: '-',
      append: '.prod',
      mask: '{datetimestamp}'
    },
    taskName: {
      files: {
        'public/css/app[mask].min.css': ['app/views/templates/master.html'],
        'public/js/app[mask].min.js': ['app/views/templates/master.html']
      }
    }
  }
});

Before:

// Asset files.

public/css/app.min.css
public/js/app.min.js
<!-- master.html -->

<link href="https://www.site.com/css/app.min.css" rel="stylesheet">
<script src="https://www.site.com/js/app.min.js"></script>

After:

// Asset files.

public/css/app-20190228123639.prod.min.css
public/js/app-20190228123639.prod.min.js
<!-- master.html -->

<link href="https://www.site.com/css/app-20190228123639.prod.min.css" rel="stylesheet">
<script src="https://www.site.com/js/app-20190228123639.prod.min.js"></script>

Hierarchical Custom Options

In this example, custom options are used in a hierarchical order.

grunt.initConfig({
  cacheKiller: {
    options: {
      mask: '{datetimestamp}'
    },
    taskName1: {
      options: {
        prepend: '-v1.1-'
      },
      files: {
        'public/css/app[mask].min.css': ['app/views/templates/master.html']
      }
    },
    taskName2: {
      options: {
        prepend: '-v1.2-'
      },
      files: {
        'public/css/web[mask].min.css': ['app/views/templates/master.html']
      }
    }
  }
});

Before:

// Asset files.

public/css/app.min.css
public/css/web.min.css
<!-- master.html -->

<link href="https://www.site.com/css/app.min.css" rel="stylesheet">
<link href="https://www.site.com/css/web.min.css" rel="stylesheet">

After:

// Asset files.

public/css/app-v1.1-20190228123639.min.css
public/css/web-v1.2-20190228123639.min.css
<!-- master.html -->

<link href="https://www.site.com/css/app-v1.1-20190228123639.min.css" rel="stylesheet">
<link href="https://www.site.com/css/web-v1.2-20190228123639.min.css" rel="stylesheet">

Bug Reporting

If you happen to come across any issues or bugs whilst using cacheKiller, please run your cacheKiller task with the cli --debug option enabled. The resulting report can then be copied and pasted inside your issue request. Adding this report to your issue request will reduce the time needed to identify and fix any bugs.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

Find more on the release history at:

https://github.com/custom-dev-tools/grunt-cache-killer/blob/master/CHANGELOG.md.