BrowserSync / browser-sync

Keep multiple browsers & devices in sync when building websites. https://browsersync.io
https://discord.gg/2d2xUThp
Apache License 2.0
12.17k stars 755 forks source link

Reload works in the CLI, but not from gulp (Windows & ASP.net MVC) #1236

Open jongunter opened 7 years ago

jongunter commented 7 years ago

Issue details

I've started a BrowserSync instance in my gulpfile. I can call browser-sync reload from the command prompt, but it does not work when I run it as a gulp task. See my example below:

Please provide issue details here.

After running this gulp task, I can call browser-sync reload from the command prompt and the page reloads

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

gulp.task('bs', function(){
    browserSync.init(
        {
           // URL of an ASP.net MVC application running in Visual Studio
            proxy: "localhost:3030"
        }
    );
}

However, if I run my gulp reload, task the page does not reload.

gulp.task('reload', function () {
    return browserSync.reload();
})

Please specify which version of Browsersync, node and npm you're running

yanwsh commented 7 years ago

Hello, I don't think this is an issue.

When you run gulp reload task, the whole code will be executed again(include require('browser-sync').create()), which will create another browsersync instance. And it's not related with your previous one. That's why it's not working.

The CLI is working because it's using http protocol and will communicate your current browsersync server.

If you want to refresh your page manually, please just do CLI browser-sync reload, If you really want to use it in gulp, please consider an alternative way to do that. First, run which browser-sync Then, change the path to browser-sync.

gulp.task('reload', function(done){
    var exec = require('child_process').exec;

    var child = exec("/path/to/browser-sync reload", function (error, stdout, stderr) {
        if (error !== null) {
            console.log('exec error: ' + error);
        }
        done();
    });
});

If you want to refresh your page automatically, please consider to use inside your init function.

Like

gulp.task('bs', function(){
    browserSync.init(
        {
           // URL of an ASP.net MVC application running in Visual Studio
            proxy: "localhost:3030"
        }
    );

    gulp.watch("*.html").on("change", browserSync.reload);
}

Hope it help.