Browser-sync: support gzip ?

Created on 15 Feb 2015  路  9Comments  路  Source: BrowserSync/browser-sync

I try use a middleware to gzip ,like express's compression middleware ,but it did'nt work :(

Most helpful comment

var compress = require('compression');
var browserSync = require('browser-sync');
browserSync({
    server: {
        baseDir: './',
        middleware: [compress()]
    }
})

All 9 comments

done with:

var compress = require('compression');
var browserSync = require('browser-sync');
browserSync({
    server: {
        baseDir: './',
        middleware: function(req,res,next){
             var gzip = compress();
              gzip(req,res,next);
        }
    }
})

Thanks @suhaotian :)

var compress = require('compression');
var browserSync = require('browser-sync');
browserSync({
    server: {
        baseDir: './',
        middleware: [compress()]
    }
})

@ngryman :)

@shakyShane nice work!!

@shakyShane This seems to compress everything except for html files. My settings is through a proxy though:

var compress = require('compression');
var browserSync = require('browser-sync').create();

browserSync.init({
  proxy: {
    target: "localhost:3000",
    middleware: [compression()]
  },
  port: 10000
});

Sorry, I don鈥檛 understand how to think in order to work. Help.

browserSync: {
      serverSyncDev: {
        bsFiles: {
          src: ['build/*.html', 'build/css/*.css', 'build/js/*.js'],
        },
        options: {
          server: 'build/',
          watchTask: true,
        },
      },
    },

    compress: {
      dist: {
        options: {
          mode: 'gzip'
        },
        expand: true,
        cwd: 'build/',
        src: ['**/*'],
        dest: 'build/'
      }
    },
// Multiple Global Middlewares
middleware: [
    function (req, res, next) {
        /** First middleware handler **/
    },
    function (req, res, next) {
        /** Second middleware handler **/
    }
]

// Per-route middleware
// NOTE: requires v2.12.1
middleware: [
    {
        route: "/api",
        handle: function (req, res, next) {
            // handle any requests at /api
        }
    }
]

// Per-route + global middleware
// NOTE: requires v2.12.1
middleware: [
    require("compression")(), // global
    {
        route: "/api", // per-route
        handle: function (req, res, next) {
            // handle any requests at /api
        }
    }
]
var compress = require('compression');
var browserSync = require('browser-sync');
browserSync({
    server: {
        baseDir: './',
        middleware: [compress()]
    }
})

Hi, @shakyShane. Can you explain why you put the function compress() inside the brackets or recommend some articles about it?

Thank you!

var compress = require('compression');
var browserSync = require('browser-sync');
browserSync({
    server: {
        baseDir: './',
        middleware: [compress()]
    }
})

Hi, @shakyShane. Can you explain why you put the function compress() inside the brackets or recommend some articles about it?

Thank you!

@vicckm Probably you already know the answer by now, but that's because you can pass multiple middlewares to the server. It's inside brackets because it's an array.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jitendravyas picture jitendravyas  路  4Comments

sedubois picture sedubois  路  3Comments

ericmorand picture ericmorand  路  3Comments

ronilaukkarinen picture ronilaukkarinen  路  4Comments

Hurtak picture Hurtak  路  3Comments