Search code examples
javascriptgulp

Gulp browserSync not reloading automatically


I have read every tutorial, stackoverflow question, discussion forum I can find on this topic and still cannot find a solution that works for me. I'm sure it's something simple I'm overlooking which is why I'm asking for help. here's my folder structure for reference.

  • dist
    • css
    • img
    • js
    • index.html
  • src
    • sass
    • img
    • js
    • index.html
  • gulpfile.js
  • package.json

browserSync will initialize correctly and show me the page in the browser. When I make a change to the html, css, or js files though, the terminal will tell me it's reloading the browser but then the browser never reloads. If i manually refresh the browser, then changes show correctly but browserSync is supposed to automatically refresh right?

Do i need to make some kind of return stream or .pipe(browserSync.stream()); at the end of my copy tasks?

var gulp = require("gulp"),
    browserSync = require("browser-sync").create(),
    sass = require("gulp-sass"),
    autoprefixer = require("gulp-autoprefixer"),
    jasmine = require("gulp-jasmine-phantom"),
    concat = require("gulp-concat"),
    uglify = require("gulp-uglify"),
    babel = require("gulp-babel"),
    sourcemaps = require("gulp-sourcemaps"),
    imagemin = require("gulp-imagemin");

gulp.task("default", ["styles", "copy-html", "copy-img", "scripts", "browser-sync", "watch"], function(){
});

gulp.task("watch", ["browser-sync"], function() {

    gulp.watch("src/img/*", ["copy-img", browserSync.reload]);
    gulp.watch("src/index.html", ["copy-html", browserSync.reload]);
    gulp.watch("src/sass/**/*.scss", ["styles", browserSync.reload]);
    gulp.watch("src/js/**/*.js", ["scripts", browserSync.reload]);
});

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

gulp.task("styles", function () {
    return gulp.src("src/sass/**/*.scss")
        .pipe(sass({outputStyle: "compressed"}).on("error", sass.logError))
        .pipe(autoprefixer({
            browsers: ["last 2 versions"],
            cascade: false
        }))
        .pipe(gulp.dest("dist/css"));
});

gulp.task("copy-html", function() {
    return gulp.src("src/index.html")
        .pipe(gulp.dest("./dist"));

});

gulp.task("scripts", function() {
    return gulp.src("src/js/**/*.js")
        .pipe(babel())
        .pipe(sourcemaps.init())
        .pipe(concat("all.js"))
        .pipe(uglify())
        .pipe(sourcemaps.write())
        .pipe(gulp.dest("dist/js"));
});

gulp.task("copy-img", function() {
    return gulp.src("src/img/*")
        .pipe(imagemin())
        .pipe(gulp.dest("dist/img"));
});

gulp.task("tests", function() {
    gulp.src("tests/spec/extraSpec.js")
        .pipe(jasmine({
            integration: true,
            vendor: "src/js/**/*.js"
        }));
});

I'm also wondering if my dependencies in the package.json file are correct.

{
    "title": "Matthew J. Whitney - Portfolio Site",
    "name": "portfolio-site",
    "description": "A smooth scrolling navigation built with Bootstrap",
    "keywords": [
        "css",
        "sass",
        "html",
        "responsive",
        "javascript"
    ],
    "homepage": "https://www.matthewjwhitney.com",
    "author": "Matthew J. Whitney",
    "repository": {
        "type": "git",
        "url": "https://github.com/matthewjwhitney/portfolio-site.git"
    },
    "dependencies": {
        "ajv": "^6.4.0",
        "babel-core": "^6.26.3",
        "bootstrap": "^4.0.0",
        "gulp-clean-css": "^3.9.3",
        "gulp-cssnano": "^2.1.3",
        "gulp-htmlclean": "^2.7.22",
        "gulp-inject": "^4.3.2",
        "gulp-rename": "^1.2.2",
        "gulp-webserver": "^0.9.1",
        "jquery": "3.3.1",
        "jquery.easing": "^1.4.1",
        "phantomjs": "^2.1.7",
        "popper": "^1.0.1",
        "popper.js": "^1.14.3"
    },
    "devDependencies": {
        "browser-sync": "^2.23.6",
        "del": "^3.0.0",
        "eslint": "^4.19.1",
        "eslint-plugin-react": "^7.7.0",
        "gulp": "^3.9.1",
        "gulp-autoprefixer": "^5.0.0",
        "gulp-babel": "^7.0.1",
        "gulp-cache": "^1.0.2",
        "gulp-concat": "^2.6.1",
        "gulp-eslint": "^4.0.2",
        "gulp-imagemin": "^4.1.0",
        "gulp-jasmine-phantom": "^3.0.0",
        "gulp-sass": "^4.0.1",
        "gulp-sourcemaps": "^2.6.4",
        "gulp-uglify": "^3.0.0",
        "gulp-useref": "^3.1.5",
        "run-sequence": "^2.2.1"
    }
}

Here's a link to my GitHub repo if that is helpful. This is really driving me crazy so I hope someone out there can and will help. Thanks! :)


Solution

  • I downloaded your repo to check it and everything is fine with your Gulp installation.

    The problem you have is that your index.html file is malformed. There's a word 'portfolio' at the very beginning of your file, before the document declaration.

    Your index.html file

    I read in some other answers that BrowserSync needs a body tag in the HTML to inject some code that enables the syncing, and I guess that a malformed HTML can break that functionality.

    Thanks,