Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

browser-sync does not refresh page after changes with Gulp

I'm new to Gulp and I wanted to make use of its automatic scss compiling and browser sync. But I can't get it to work.

I stripped everything down to leave only the contents of the example on the Browsersync website:

http://www.browsersync.io/docs/gulp/#gulp-sass-css

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

// Static Server + watching scss/html files
gulp.task('serve', ['sass'], function() {

    browserSync.init({
        server: "./app"
    });

    gulp.watch("app/scss/*.scss", ['sass']);
    gulp.watch("app/*.html").on('change', browserSync.reload);
});

// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function() {
    return gulp.src("app/scss/*.scss")
        .pipe(sass())
        .pipe(gulp.dest("app/css"))
        .pipe(browserSync.stream());
});

gulp.task('default', ['serve']);

I can call gulp serve. The site is showing and I get a message from Browsersync. When I modify the HTML, the page is reloaded. When however I modify the scss, I can see this:

[BS] 1 file changed (test.css)
[15:59:13] Finished 'sass' after 18 ms

but I have to reload manually. What am I missing?

like image 546
raichu Avatar asked Jul 01 '15 14:07

raichu


4 Answers

I also faced a similar problem when I was new to browser-sync usage, the command-line was saying "reloading browsers" but the browser was not refreshed at all, the problem was I had not included body tag in my HTML page where the browser-sync can inject script for its functionality, make sure your HTML page has body tag.

like image 137
SU15 Avatar answered Sep 19 '22 16:09

SU15


You can just inject the changes instead of having to force a full browser refresh on SASS compile if you like.

browserSync.init({     injectChanges: true,     server: "./app" });  gulp.task('sass', function() {     return gulp.src("app/scss/*.scss")         .pipe(sass())         .pipe(gulp.dest("app/css"))         .pipe(browserSync.stream({match: '**/*.css'})); }); 
like image 33
Dan Gamble Avatar answered Sep 20 '22 16:09

Dan Gamble


This is because you're calling browserSync.reload on the html watch and not on the scss watch.

Try this:

gulp.watch("app/scss/*.scss", ['sass']).on('change', browserSync.reload);
gulp.watch("app/*.html").on('change', browserSync.reload);
like image 22
Felipe Skinner Avatar answered Sep 22 '22 16:09

Felipe Skinner


This is what I use and it work's fine in sass or any other files

gulp.task('browser-sync', function () {
   var files = [
      '*.html',
      'css/**/*.css',
      'js/**/*.js',
      'sass/**/*.scss'
   ];

   browserSync.init(files, {
      server: {
         baseDir: './'
      }
   });
});
like image 35
SimplifyJS Avatar answered Sep 21 '22 16:09

SimplifyJS