Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
grunt-version-assets
Advanced tools
Use node-version-assets to rename static assets and update references
This Grunt plug-in renames static assets (JavaScript files, CSS, ...) and at the same time updates references to these assets in other files (for example, HTML files). This enables you to deliver static assets with HTTP caching headers telling the browser to cache them forever, so the file will only be downloaded once. If the file changes, its file name changes and the new version will be downloaded, so users never see stale JavaScript or CSS files.
This plugin requires Grunt.
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 install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-version-assets --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-version-assets');
In your project's Gruntfile, add a section named versioning
to the data object passed into grunt.initConfig()
.
grunt.initConfig({
versioning: {
options: {
grepFiles: [
'public/**/*.html',
]
},
js: {
src: [
'public/js/app.min.js',
'public/js/bundle.min.js',
]
},
css: {
src: [
'public/css/master.css',
'public/css/bundle.min.js',
]
},
},
});
Type: An array of file names, paths and patterns.
Default value: []
The list of files in which the plug-in replaces/updates the references to the asset files.
Type: boolean
Default value: true
If true
, the original source file will be kept, otherwise it will be deleted.
node-version-assets actually offers a number of other options, that have not been implemented in this plug-in. If you need any of these options, file an issue or better yet, a pull request (delegating options to node-version-assets is actually trivial).
In this example, a copy of the file public/js/app.js
is created with a versioned file name (something like public/js/app.17781b077d5a9c60c6504e4d1467e2b0.js
). The same is done for public/css/app.css
. The file index.html
is searched for references to app.js
and app.css
. The references will be updated to match the renamed files. The original files (app.js
and app.css
) will not be deleted. If there are versioned asset files from a former run of this plug-in, those will be deleted.
grunt.initConfig({
versioning: {
options: {
grepFiles: [
'public/index.html',
]
},
app: {
src: [
'public/js/app.js',
'public/css/app.css'
]
},
})
In this example, a all JavaScript files under public/js
are renamed to a versioned file name (something like public/js/whatever.17781b077d5a9c60c6504e4d1467e2b0.js
). The same is done for all CSS files under public/css
. A couple of HTML files are searched for references to the renamed asset files. The references will be updated to match the renamed files. The original asset files will be deleted. If there are versioned asset files from a former run of this plug-in, those will also be deleted.
grunt.initConfig({
versioning: {
options: {
grepFiles: [
'public/index.html',
'public/index2.html',
'public/pages/*.html',
],
keepOriginal: false
},
js: {
src: [
'public/js/**/*.js'
]
},
css: {
src: [
'public/css/**/*.css'
]
},
})
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.
Copyright (c) 2014 Bastian Krol. Licensed under the MIT license.
FAQs
Use node-version-assets to rename static assets and update references
We found that grunt-version-assets demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.