Hi Finbarr, I'm interested in learning more about this strategy. I've been using the Garber-Irish method (http://viget.com/inspire/extending-paul-irishs-comprehensive...) for running scripts based on controller/actions, but everything is still always served to every page...With your solution, only truly global scripts seems to be served. I'll try to throw up a test app on github to test out what you've done here, but do you have any existing app that uses this paradigm? I'd love to take a closer look.
Please do test it and post the results. It seems a real point of contention whether a single JS script can outperform page-specific scripts. The team behind the Rails assets pipeline has pushed the single script approach. It'll take some testing to lay the debate to rest.
I think there are a number of factors which will affect the outcome, including:
- number of $(document).ready() callbacks that check whether the current file should run within the single application.js approach. This could grow to be an issue with much larger apps.
- how often the application.js file gets invalidated by a deploy (if it is every deploy this may be annoying for users if there's always a large performance penalty on the first download - especially for apps where the usage isn't regular).
- in the 2 scripts version it's very unlikely that the heavyweight application.js global file will get invalidated regularly, but it is likely that several of the smaller specific page js files will get invalidated with each deploy.
I guess the question is whether downloading several smaller files for each deploy beats downloading the whole application.js file for each deploy (network overhead being the likely bottleneck here).