Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp Browsersync causing multiple reloads with each filechange

I'm using browsersync with Gulp, running some tasks on particular filechanges. Whenever I save a file, I get 10+ [BS] Reloading Browsers... in my terminal and performance is understandably laggy.

Here are my gulpfile:

gulp.task('bowerJS', function() {
  gulp.src(lib.ext('js').files)
    .pipe(concat('lib.min.js'))
    .pipe(uglify())
    .pipe(gulp.dest('app/assets/js'));
});

gulp.task('bowerCSS', function() {
  gulp.src(lib.ext('css').files)
    .pipe(concat('lib.min.css'))
    .pipe(gulp.dest('app/assets/css/'));
});


// Compile sass into CSS & auto-inject into browsers
gulp.task('less', function() {
    gulp.src('./app/css/*.less')
        .pipe(less())
        .pipe(autoprefixer({
          browsers: ['last 2 versions'],
          cascade: false
        }))
        .pipe(gulp.dest('app/assets/css'))
        .pipe(browserSync.stream());
});

// Render Jade templates as HTML files

gulp.task('templates', function() {
  gulp.src('views/**/*.jade')
    .pipe(jade({
      pretty: true
    }))
    .pipe(gulp.dest('app/src/'))
});

gulp.task('php', function() {
    php.server({ base: 'app', port: 8123, keepalive: true});
});
gulp.task('serve', ['bowerJS', 'bowerCSS', 'less', 'templates', 'php'], function() {


    var proxyOptions1 = url.parse('http://some-site:1234');
        proxyOptions1.route = '/api/hi';

    browserSync({
        port: 8999,
        proxy: '127.0.0.1:8123',
        middleware: [
                proxy(proxyOptions1),
                history()
        ],
        notify: false
    });


    gulp.watch("app/assets/css/*.less", ['less']);
    gulp.watch("app/**/*.html").on('change', browserSync.reload);
    gulp.watch("app/assets/js/*.js").on('change', browserSync.reload);
    gulp.watch("views/**/*.jade", ['templates']);
});

What am I doing wrong here?

like image 585
JVG Avatar asked May 19 '16 06:05

JVG


2 Answers

use only browserSync.stream (replace browserSync.reload then) and pass option once: true like this

browserSync.stream({once: true})

this should works fine.

like image 54
TeoMatthew Avatar answered Nov 11 '22 12:11

TeoMatthew


The awaitWriteFinish option in Chokidar fixed it for me.

Example:

browserSync.init({
    server: dist.pages,
    files: dist.css + '*.css',
    watchOptions: {
        awaitWriteFinish : true
    }
});
like image 35
Luke Avatar answered Nov 11 '22 11:11

Luke