【问题标题】:Browser-sync (under gulp) doesn't refresh browser浏览器同步(在 gulp 下)不刷新浏览器
【发布时间】:2015-01-28 16:32:34
【问题描述】:

我的配置完成了它应该做的一切,但它从不刷新浏览器。一旦我手动刷新它,就会有变化。我正在连接到默认的 localhost:3000。任何想法为什么会这样或如何调试它?

gulpfile.js:

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

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

gulp.task('sass', function() {
  return gulp.src('./app/scss/style.scss')
    .pipe(sass())
    .pipe(gulp.dest('./app/css'))
    .pipe(browserSync.reload({ stream:true }));
});

gulp.task('serve', function() {
  browserSync({
    server: {
      baseDir: 'app'
    }
  });
});

gulp.task('default', ['serve'], function () {
    gulp.watch('./app/scss/*.scss', ['sass', browserSync.reload]);
    gulp.watch('./app/*.html', ['html', browserSync.reload]);

});

控制台输出示例:

[BS] Local URL: http://localhost:3000
[BS] External URL: http://192.168.1.3:3000
[BS] Serving files from: app
[17:10:32] Starting 'html'...
[BS] Reloading Browsers...
[17:10:32] Finished 'html' after 829 μs
[BS] Reloading Browsers...
[17:10:42] Starting 'sass'...
[BS] 1 file changed (style.css)
[17:10:42] Finished 'sass' after 22 ms
[BS] Reloading Browsers...
[17:11:02] Starting 'html'...
[BS] Reloading Browsers...
[17:11:02] Finished 'html' after 472 μs
[BS] Reloading Browsers...

【问题讨论】:

  • 即使我在浏览器同步使用新手时也遇到过类似的问题,命令行说“重新加载浏览器”但浏览器根本没有刷新,问题是我没有包含 body 标签在浏览器同步可以为其功能注入脚本的我的 html 页面中,确保您的 html 页面具有正文标记。

标签: javascript gulp gulp-watch gulp-livereload browser-sync


【解决方案1】:

我想通了:浏览器同步不喜欢隐式 html 标签,所以这(虽然有效的 HTML5)不起作用:

<!doctype html>
<title>implicit</title>

但这会:

<!doctype html>
<html>
    <head>  
        <title>full doc</title>
    </head>
    <body></body>
</html>

【讨论】:

  • 实际上它只需要 body 标签(并且它是记录在案的行为)
  • 这是我一直在寻找的答案。但是我似乎在 BrowserSync 的文档中找不到这个。或者这是 Gulp 的事情?
  • 应该在主文档中,但这里提到了github.com/shakyShane/browser-sync#requirements
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-08
  • 2015-04-15
  • 2017-10-31
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
  • 2017-06-07
相关资源
最近更新 更多