Gulp Browsersync causing multiple reloads with each filechange
Asked Answered
C

2

7

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?

Capitoline answered 19/5, 2016 at 6:21 Comment(0)
I
9

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

browserSync.stream({once: true})

this should works fine.

Infarct answered 28/9, 2016 at 11:21 Comment(1)
or look at the debounce option: hereSexless
C
1

The awaitWriteFinish option in Chokidar fixed it for me.

Example:

browserSync.init({
    server: dist.pages,
    files: dist.css + '*.css',
    watchOptions: {
        awaitWriteFinish : true
    }
});
Cosper answered 4/1, 2018 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.