Coder Social home page Coder Social logo

Comments (23)

shinnn avatar shinnn commented on July 28, 2024

This would work.

gulp.task('serve', function () {
  browserSync({
    server: "./_site",
    files: ['./_site/public/css/*.css', './_site/public/js/*.js', './_site/**/*.html{,.gz}'],
    middleware: require("connect-gzip-static")("./_site")
  });
});

from recipes.

realph avatar realph commented on July 28, 2024

@shinnn I've used the above code, but Chrome is still picking up my 67KB file rather than my 28KB .gz file.

screen shot 2015-06-01 at 13 18 37
screen shot 2015-06-01 at 13 18 19

from recipes.

shinnn avatar shinnn commented on July 28, 2024

@realph I thought "pick up my .html.gz files" means "BrowserSync watches my .html.gz files". What do you mean by "pick up"?

from recipes.

realph avatar realph commented on July 28, 2024

@shinnn I want BrowserSync to serve my .html.gz files to Chrome, so Chrome renders the compressed html files.

from recipes.

shinnn avatar shinnn commented on July 28, 2024

@realph OK, thanks. I have no experience in serving gzipped files with Browsersync, so I'd like to leave this issue to @shakyShane.

from recipes.

realph avatar realph commented on July 28, 2024

@shinnn Thanks for your help. @shakyShane ...?

from recipes.

shakyShane avatar shakyShane commented on July 28, 2024

@realph the problem here is that the middleware is too late in the stack when added via the options for .html files as serve-static ends the request prematurely thinking that the index file doesn't exist.

The following will work perfectly though because override will put your middleware at the front of the stack.

gulp.task('serve', function () {
    browserSync({
        server: "./_site",
        files: ['./_site/public/css/*.css', './_site/public/js/*.js', './_site/*.html', './_site/**/*.html']
    }, function (err, bs) {
        bs.addMiddleware("*", require('connect-gzip-static')('./_site'), {
            override: true
        });
    });
});

from recipes.

shakyShane avatar shakyShane commented on July 28, 2024

Updated the recipe to reflect this

from recipes.

realph avatar realph commented on July 28, 2024

Thanks for this @shakyShane! Works perfectly now.

from recipes.

shakyShane avatar shakyShane commented on July 28, 2024

tumblr_m0osrrhqmk1qzcv7no4_250

from recipes.

realph avatar realph commented on July 28, 2024

@shakyShane Sorry to nag. I've just realised that I don't know how to add back my BrowserSync reload. I'm calling BrowserSync's reload at the end of my tasks like so:

.pipe(reload({stream: true}))

Any idea what I've got to do to get it back?

from recipes.

shakyShane avatar shakyShane commented on July 28, 2024

Can you show us your gulpfile and explain what you expect to happen :)

from recipes.

realph avatar realph commented on July 28, 2024

@shakyShane

My gulpfile.js looks like this:

    var gulp = require('gulp');

    var browserSync = require('browser-sync');
    var browserSyncCreate = browserSync.create();
    var middleware = require('connect-gzip-static')('./_site');
    var reload = browserSync.reload;

    // Start server
    gulp.task('serve', function () {
      browserSyncCreate.init({
        server: './_site',
        files: [
          './_site/**/*.html',
          './_site/public/css/*.css',
          './_site/public/js/*.js'
        ],
      }, function (err, bs) {
          bs.addMiddleware("*", middleware, {
            override: true
         });
      });
    });

    // Jekyll task
    gulp.task('jekyll', function() {
      return gulp.src('./index.html')
        .pipe(shell(['jekyll build']))
        .pipe(reload({stream: true}))
    });

    // Default task
    gulp.task('default', ['jekyll'], function() {
      gulp.watch(['./index.html', './*.md', '_includes/**/*.html', '_layouts/**/*.html', 'posts/*.html', '_posts/**/*'], ['jekyll']);
    });

When changes are made to the watch directory in the default task, I want to run my jekyll task, at the end of the jekyll task, you'll see I'm using .pipe(reload({stream: true})). I want BrowserSync to reload my browser when a change is detected.

Ever since I changed the server task to browserSyncCreate.init() the reloads haven't worked the same, I was previously using simply browserSync() for my my server task, and reloads worked.

Hope that explains it. Thanks!

from recipes.

shakyShane avatar shakyShane commented on July 28, 2024

You can use .stream() for that

var gulp = require('gulp');

var browserSync = require('browser-sync').create();
var middleware = require('connect-gzip-static')('./_site');

// Start server
gulp.task('serve', function () {
    browserSync.init({
        server: './_site',
        files: [
            './_site/**/*.html',
            './_site/public/css/*.css',
            './_site/public/js/*.js'
        ],
    }, function (err, bs) {
        bs.addMiddleware("*", middleware, {
            override: true
        });
    });
});

// Jekyll task
gulp.task('jekyll', function() {
    return gulp.src('./index.html')
        .pipe(shell(['jekyll build']))
        .pipe(browserSync.stream())
});

// Default task
gulp.task('default', ['jekyll'], function() {
    gulp.watch(['./index.html', './*.md', '_includes/**/*.html', '_layouts/**/*.html', 'posts/*.html', '_posts/**/*'], ['jekyll']);
});

from recipes.

shakyShane avatar shakyShane commented on July 28, 2024

or try this, (which i prefer, as is keeps Browsersync related stuff out of the jekyll task

var gulp = require('gulp');

var browserSync = require('browser-sync').create();
var middleware = require('connect-gzip-static')('./_site');

// Start server
gulp.task('serve', function () {
    browserSync.init({
        server: './_site',
        files: [
            './_site/**/*.html',
            './_site/public/css/*.css',
            './_site/public/js/*.js'
        ],
    }, function (err, bs) {
        bs.addMiddleware("*", middleware, {
            override: true
        });
    });
});

// Jekyll + reload
gulp.task('jekyll-reload', ['jekyll'], browserSync.reload);

// Jekyll task
gulp.task('jekyll', function() {
    return gulp.src('./index.html')
        .pipe(shell(['jekyll build']));
});

// Default task
gulp.task('default', ['jekyll'], function() {
    gulp.watch(['./index.html', './*.md', '_includes/**/*.html', '_layouts/**/*.html', 'posts/*.html', '_posts/**/*'], ['jekyll-reload']);
});

from recipes.

shakyShane avatar shakyShane commented on July 28, 2024

^ note that I call jekyll-reload in the watch task there

from recipes.

realph avatar realph commented on July 28, 2024

@shakyShane I'll give these a try. Any reason why you like to keep BrowserSync away from main task and its own separate task? Do you also do this for Sass, JS tasks?

from recipes.

shakyShane avatar shakyShane commented on July 28, 2024

because, sometimes you only want to run gulp jekyll or a similar command without the whole watching/reloading setup. In this context, whilst it will still work, it doesn't make sense to pipe anything to Browsersync.

from recipes.

realph avatar realph commented on July 28, 2024

@shakyShane I'm using your second example, but BrowserSync doesn't seem to be running the .reload meth. Manual reload works just fine.

from recipes.

schiz avatar schiz commented on July 28, 2024

@realph did you succeed? can you paste the code, how did you manage to do it? I use the same stack - and found out that only when using connect-gzip-static reload doesn't work. None of fixes listed above didn't work for me.

from recipes.

johnpapa avatar johnpapa commented on July 28, 2024

@shakyShane How would i serve gzip files when I am using the config file instead of gulp?

      middleware: [
        require('connect-gzip-static')('./dist'), {override: true }
      ]

does not seem to work like this ... obviously the array element needs to be one thing ..., usually it is the function. but in this case i am not sure how to pass the override

from recipes.

shakyShane avatar shakyShane commented on July 28, 2024

@johnpapa 2 solutions.

On recent versions of BS, (2.15 + I think), you can provide an array of objects/functions as middleware definitions and on any object you can specify the override prop

modules.exports = {
    server: './app',
    middleware: [
        {
            route: "", // empty 'route' will apply this to all paths
            handle: require('connect-gzip-static')('./app'), // the callable
            override: true
        }
    ]
};

As mentioned above, the override boolean will cause this middleware to be applied to the FRONT of the stack.

Alternatively, when using the configuration file, you can still get hold of the running instance through callbacks

const middleware = require('connect-gzip-static')('./app');

modules.exports = {
    server: './app',
    callbacks: {
        ready: function (err, bs) {
            // access bs methods as normal
            bs.addMiddleware("*", middleware, {
                override: true
            });

            // also have access to 'options' here which contains
            // info available only to a running instance, such as 'urls'
           console.log(bs.options.get('urls').toJS())
        }
    }
};

Hope that helps :)

from recipes.

shakyShane avatar shakyShane commented on July 28, 2024

@johnpapa assuming your files are already gzipped of course! to gzip on the fly look at 'compression' module

from recipes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.