SailsJs getting hash file names

I recently started experimenting heavily with Sailsjs ( and have had some issues with hash file names. Coming from a Django background — this was new to me. In the Django world we had Django compressor which alleviates almost all of the concern so its a non issue. In sails, that’s hardly the case. I came up with the below solution.

1. I use gulp task runner to compress all of my assets (css, js, html). This allows the assets to be optimized for my users when they are navigating my website.

1. After the compression is over, I use gulp-hash which hashes file names (and allows us to specify the format). But it also outputs to a json file the hash names from the original names. So if I have all.css compressed to all-34jioh2i.css, I will have a json file that shows that. I have a sample of my code below:

return gulp.src(['.tmp/public/**/*.js', '!.tmp/public/lib/**/*.js', '!.tmp/public/**/*.hash.js'])
.pipe(hash({append: true, template: '<%= name %>-<%= hash %>.hash<%= ext %>'}))

Since sailsjs insists on putting all of these files in a .tmp folder, my output (assets.json) will be in the root of that folder.

3. This is the most important part. Here I need a way to reference these assets from the templates. The way I do it is in the http.js file in the config folder. At the very bottom (but within model.exports) I put the below:

locals: {
filters: {

staticname: function (name) {
if (sails.config.http.shouldHaveHash == true) {
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync(sails.config.globals.projectPath + '/.tmp/assets.json', 'utf8'));
if (obj[name] != null) {
if (obj[name] != null) {
return "/" + obj[name];
} else {
Logger.error("Can't find the static file: " + name);
return "/" + name;

This code reads the json file previously outputted, and tries to map an input! So how does that work?

<link rel="stylesheet" href="<%=: "styles/example.css" | staticname %>">

This will find the key mapped to the hashfile name. And thats it!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.