

Context Require - Load JS assets based on page content - benjaminbenben
http://benjaminbenben.com/2013/10/01/context-require/

======
untog
I think the benefits of this approach are outweighed by the number of HTTP
requests you have to send for individual JS files. In the majority of cases
you'll find that all of your JS, bundled up and minified, loads faster than
getting each file as it is required.

~~~
benjaminbenben
Yeah, there is a bit of an overhead with that. Module server -
[https://github.com/google/module-server](https://github.com/google/module-
server) seems like quite a good solution. Though I'd agree, for a large number
of sites it's far better to bundle them up.

------
jammus
We do a similar thing at Last.fm. We bundle our most commonly used modules in
a single file. The rest all pulled in at runtime.

Paul wrote about it here: [http://www.paulrhayes.com/2013-09/a-light-
progressive-framew...](http://www.paulrhayes.com/2013-09/a-light-progressive-
framework/)

------
benjaminbenben
I wrote this post - feels like quite a simple approach. Would love to hear if
anyone has done anything similar.

~~~
shoebappa
We're doing something similar with YepNope that is included in Modernizr but
it's not as simple as yours. We ran into problems where if there were multiple
elements on the page with that dependency (usually a jquery plugin), only one
would end up getting initialized. We ended up coming up with a complicated
queuing system that queues up all of the initialization requests when it
detects that it is in the process of being loaded, and fires for each in the
queue after the dependency is loaded. I'm definitely curious to look into your
method in terms of multiple items that need to each be initialized and how
this is handled with your context require method or require in general.

~~~
benjaminbenben
Ah, I've not had to deal with sharing between multiple elements yet - I'm kind
of hoping that when I do require.js would give quite a solid base for it.
YepNope seems like a good choice for this kind of thing, especially that it
can bring in css assets too. Would be awesome to bring in all related assets
for a element

