question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gulp: Browser reloads before code was syncing

See original GitHub issue

Hi, sometimes i have this issue with BrowserSnc + Gulp: the Browser reloads before the code is synced.

Here my typical gulpfile:

var gulp = require('gulp');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var cssnext = require('cssnext');
var precss = require('precss');
var dirSync = require('gulp-directory-sync');
var browserSync = require('browser-sync').create();

gulp.task('css', function () {
    var processors = [
        autoprefixer,
        cssnext,
        precss
    ];
    return gulp.src('./src/*.css')
            .pipe(postcss(processors))
            .pipe(gulp.dest('./build'));
});

gulp.task('sync', function () {
    return gulp.src('src')
            .pipe(dirSync('src', 'build', {ignore: ['stuff', 'css']}, 'nodelete'));
});

gulp.task('browserSync', function () {
    browserSync.init({
        proxy: "webwork/clients/wordpress/website"
    });
});

gulp.task('default', ['browserSync'], function () {
    gulp.watch('src/*.css', ['css']).on('change', browserSync.reload);
    gulp.watch('src/**/*.php', ['sync']).on('change', browserSync.reload);
});

After the second change in code, the reload will start before syncing. This makes the changes only visible after the next change/relaod…

Only this workaround works:

gulp.task('browserSync', function () {
    browserSync.init({
        reloadDelay: 1000,
        proxy: "webwork/clients/wordpress/website"
    });
});

If i use the reloadDelay option. Only this will make the Browser reloading after syncing.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
GuyPaddockcommented, Feb 5, 2017

One other thing to look at: make sure all of the tasks that compile code return the task result (promise).

In other words, make sure this:

gulp.task('sass:dev', function () {
  gulp.src([sassSrc])
    .pipe(sourcemaps.init())
    .pipe(sass(sassConfig).on('error', sass.logError))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest(sassDest))
    .pipe(browserSync.stream());
});

Becomes:

gulp.task('sass:dev', function () {
  return gulp.src([sassSrc])
    .pipe(sourcemaps.init())
    .pipe(sass(sassConfig).on('error', sass.logError))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest(sassDest))
    .pipe(browserSync.stream());
});

This helps ensure that BrowserSync is only invoked when the compilation “promise” is resolved.

0reactions
Binaryhatcommented, Mar 9, 2017

Thank you GuyPaddock. return keyword…! that is it…!

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Browsersync with gulp refreshing before finished
Now, whenever there is a change in my code, my "some-task-name" gets executed and then the browser page is reloaded. I don't need...
Read more >
Gulp.js browser-sync not reloading my browser #1489 - GitHub
When I hit save on my project it builds everything fine. My browser blinks and says "Connected to Browser-sync" on the upper right...
Read more >
How To Use Gulp - Browser Sync Live Reload Tutorial
In this video I show you How To Use BrowserSync to Live Reload your Web Browser whenever you make changes to your code....
Read more >
Gulp from Scratch: Browser-Sync Auto Reload HTTPS
Support Me ::https://www.patreon.com/alecadddhttp://www.alecaddd.com/support-me/https://amzn.to/2Hcp5moCheck out Elementor: ...
Read more >
Browsersync + Gulp.js
Browsersync makes your browser testing workflow faster by synchronising URLs, interactions and code changes across multiple devices.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found