Gzip in nodeJS and in Gulp (double gzip)

I'm working on the performance of an project. It is an node application and I use the npm package 'compression' to gzip the whole app. I'm also gzipping the minified CSS using gulp-gzip.

So this is the Gzip compression in my server.js

const compression = require('compression');
app.use(compression());

And this is the part where I gzip the CSS in my gulpfile.js

    gulp.task('cssStyles', function () {
     return gulp.src(['./src/dist/css/docs.css', './src/dist/css/fonts.css'])
     .pipe(cleanCSS())
     .pipe(gzip())
     .pipe(rename("docs.min.css"))
     .pipe(gulp.dest('./src/dist/css/'));
});

When I only used the compression in server.js, the load time of the website was faster than before. But after also gzipping my CSS via Gulp, the load time was even more faster.

My question: Is it good practice to do double gzipping (in app and with gulp), and is it possible that this will cause any problems?

Upvotes: 0

Views: 508

Answers (1)

robertklep
robertklep

Reputation: 203286

Gzipping a second time won't substantially shrink the already gzipped data, and it may even cause issues with clients not expecting data to be gzipped twice (in fact, I'm surprised to read it worked for you).

You should choose one method, but be aware that if you use the Gulp method (using "pre-compressed" files), you need to make sure that the correct Content-Encoding header is set for those resources, otherwise the client won't know the data is compressed.

Since compression overhead usually is negligible, I would just use the middleware.

Upvotes: 1

Related Questions