ForbesLindesay / browserify-middleware

express middleware for browserify, done right
http://browserify.org
MIT License
381 stars 66 forks source link

Slow startup time #99

Closed mkapnick closed 7 years ago

mkapnick commented 7 years ago

Is there a way to speed up the browserify compilation step on startup?

For example, when my app makes the first GET request to get the bundle, browserify-middleware goes through the process of compiling and minifying the code into a bundle. All other subsequent requests for this resource are fast, since the bundle has been compiled and is cached.

Since the first GET request for this resource tries to browserify and minify 10MB of data, my startup time is really slow, close to a minute. How can I make this faster?

Basically, I may have to abandon this awesome module and compile things offline to avoid a huge delay in startup time. And I'm trying to avoid that.

ForbesLindesay commented 7 years ago

If you need fast startup time in production, your best bet is probably not using this module. The code I have for one of my projects where I need faster startup time looks like:

let CLIENT_URL = '/static/client.js';
if (process.env.NODE_ENV === 'production') {
  const clientSrc = fs.readFileSync(__dirname + '/bundle.js');
  const clientResponse = require('prepare-response')(clientSrc, {'content-type': 'js', 'cache-control': '1 year'});
  CLIENT_URL = '/static/' + require('crypto').createHash('md5').update(clientSrc).digest("hex") + '/client.js';
  app.get(CLIENT_URL, (req, res, next) => {
    clientResponse.send(req, res, next);
  });
} else {
  app.get(CLIENT_URL, require('browserify-middleware')(__dirname + '/client.js'));
}

Then as a build step I run:

browserify client.js | uglify > bundle.js

It would be great to set up a proper way to make this self contained within the module somehow.

N.B. that prepare-response here is handling all the caching/gzip, and is what gets used internally within browserify-middleware for the same purpose. Making the hash of the bundle be part of the URL lets us set a cache timeout of 1 year, rather than browserify-middleware's default of 1 minute.

gilbert commented 7 years ago
mkapnick commented 7 years ago

Sorry, meant to close this issue. @ForbesLindesay I'm following that workflow on production. @mindeavor Not worried about it on production if I'm compiling a priori now. Maybe for development purposes I can look into bundle splitting if that will improve the speed