Chiqui Esteban
Chiqui Esteban

Reputation: 421

browserSync not working

I have this code to reload the browser with any change:

var gulp          = require('gulp');
var browserSync   = require('browser-sync');

gulp.task('browser-sync', function() {
    browserSync({
        server: {
            baseDir: "./"
        }
    });

});

I'm triggering browserSync here, through html.js:

var gulp        = require('gulp');
var browserSync = require('browser-sync');
var fileinclude = require('gulp-file-include');
var rename      = require('gulp-rename');
var util        = require('gulp-util');

gulp.task('html', function() {

    var filename = 'middle.html';

    return gulp.src(filename)
        .pipe(fileinclude())
        .pipe(rename('index.html'))
        .pipe(gulp.dest('./'))
        .pipe(browserSync.reload({stream:true}));
});

For some reason, html.js seems to work (fileinclude, rename, etc... is done) but browserSync doesn't. I don't get any error message, but the browser does not reload automatically. Any ideas?

Thanks!

Upvotes: 2

Views: 18249

Answers (2)

MrGreen
MrGreen

Reputation: 147

Below technique worked for me.

For Automatic Reloading of

  • Html you should use browserSync.reload
  • and for CSS use browserSync.stream().

var gulp = require('gulp');
var sass = require('gulp-sass');
var watch = require('gulp-watch');

var browserSync = require('browser-sync').create(); // for live-editing

gulp.task('default', function(){

	// Serve files from the root of this project
	browserSync.init({
	    	server: "./"
	});
	
	gulp.watch('sass/**/*.scss', ['styles']);

	//-----*****   RELOADING of HTML   *****-----
	gulp.watch("*.html").on("change", browserSync.reload);
	
});


gulp.task('styles', function(){

	gulp.src('sass/**/*.scss')
	
	.pipe(sass().on('error', sass.logError))     	
 
 	.pipe( gulp.dest('./css'))

	//-----*****   RELOADING of CSS   *****-----
	.pipe(browserSync.stream());	
});

Upvotes: 3

nils
nils

Reputation: 27174

So, I'm not 100% sure if this is the issue, but try this. Add the browser-sync task to the watch task, to load the configuration.

var gulp = require('gulp');
gulp.task('watch', ['browser-sync'], function() { // Add your browser-sync task to the watch task
    gulp.watch([ 'html/*' ], ['html']);
    gulp.watch([ '.html' ], ['html']);
    gulp.watch([ 'js/' ], ['html']);
    gulp.watch([ 'css/*' ], ['html']);
    gulp.watch([ 'css/*', ], ['sass']);
});

And then amend your browser-sync configuration to inject css changes into the browser:

var gulp          = require('gulp');
var browserSync   = require('browser-sync');

gulp.task('browser-sync', function() {
    browserSync({
        server: {
            baseDir: "./",
            injectChanges: true // this is new
        }
    });

});

Let me know how it goes.

Upvotes: 0

Related Questions