SlideShare a Scribd company logo
1 of 37
Loading Javascript
Szabolcsi-Tóth Szabolcs
Senior Frontend Developer at Ustream.
@_Nec
nec@shell8.net
Loading Javascript
These slides are
not about:
detailing defer/FIF js
embedding on site
or putting the js after the
body or in the head
These slides are
supposed to be
about:
detailing why static js
loading, packaging and
building leads to nowhere
on the long run
and a possible solution
Inline scripting
● Quick sandboxing
● Small demos
● Learning
● Testing http://www.flickr.com/photos/pfly/399346124/
Javascript Files
● Separate code from markup
● Serve assets from a server
http://www.flickr.com/photos/seiho/2183406722/
Code organization
Webapp complexity
increases
Loadtime becomes
an issue :(
Use of functions,
classes, modules
Codebase grows
http://www.flickr.com/photos/chiotsrun/4390949664/
Code compilation / build
Much less http requests
Tools:
● concatenation
● uglify / yuicompressor
http://www.flickr.com/photos/halfbisqued/2353845688/
Code compilation / build
Compile all the js that the
current page might need:
Several smaller files
Compile all js that we need:
One huge file
Create js groups according to
page needs
http://www.flickr.com/photos/halfbisqued/2353845688/
Code compilation / build
Compile all the js that the
current page might need:
Several smaller files
Compile all js that we need:
One huge file
Create js groups according to
page needs
What is the problem with that?
http://www.flickr.com/photos/halfbisqued/2353845688/
Take any page, coded, optimized, built,
deployed
Uh-Oh.. The Product / UX / Design division
enters the room!
The new killer feature, that saves us!
Click here to
close
Code compilation / build
The given feature is used on the page,
or
The visitor might use it on the page
"the current page might need"
One page gets n+1 new feature:
The js compiled group for that page grows even more
heavy
Do we really need it onLoad?
Lots of unused code, that waits for the user:
overhead, slows load time.
http://www.flickr.com/photos/halfbisqued/2353845688/
Async loading!
Load only the most necessary js onLoad!
Then, for every feature the user wants,
load the js runtime.
http://www.flickr.com/photos/thenationalguard/8029811025/
● Feature based code, not page based
code
● Small lag in UX, but faster page start
● Loose module coupling, better code
Code compilation / build
Compile all the js that the
current page might need:
Several smaller files
Compile all js that we need:
One huge file
Create js groups according to
page needs
And what is the problem with that?
http://www.flickr.com/photos/halfbisqued/2353845688/
Dependency handling
The problem with predefined js
groups:
http://www.flickr.com/photos/wongjunhao/2761709029/
● add js by planned use
(add a feature, that can be used)
● add js by failsafe use
("this might come handy" or "make sure to have
this")
● group is built at deploy
Dependency handling
The group is created for a
planned usage
The group contents has nothing
to do with the real functioning
site or app
http://www.flickr.com/photos/wongjunhao/2761709029/
Dependency handling
Let the js codebase define it's own
dependency
Here come the modules!
AMD
(Asynchronous Module Definition)
Designed for the
web
CommonJS
Designed for the
server (nodejs)
http://www.flickr.com/photos/wongjunhao/2761709029/
Dependency handling
AMD structure
define (
// optional, used in build code
'moduleName',
// dependencies
['modules/Module1', 'modules/Module2'],
// module definition
function (module1, module2) {
});
http://www.flickr.com/photos/wongjunhao/2761709029/
Async loading + Modules
Module loader
a javascript utility, that:
● handles module definitions and
requirements
● loads modules from a remote
server
require.js, curl.js, etc
http://www.flickr.com/photos/redux/4298421692/
Async loading + Modules
Require.js example
http://www.flickr.com/photos/redux/4298421692/
Every dependency starts a new request
Lots of requests - not very good
Async loading + Modules
Need fewer requests?
Introduce async packaging!
http://www.flickr.com/photos/redux/4298421692/
Request and load packaged javascripts
async, instead of each file.
But how will the client know of the
available package list?
Async loading + Modules
Possible solution:
expose the built package list, and contents
to the client
http://www.flickr.com/photos/redux/4298421692/
Why not?
In case of a large codebase, this can be an
uncomfortably large amount of data. Also,
exposes the whole codebase to the client.
Async loading + Modules
Another problem with pre-built packages,
prepared for async load:
Overlapping dependencies are inevitable
http://www.flickr.com/photos/redux/4298421692/
Pack_1 Pack_2 Pack_3
Foo.js Foo.js
Bar.js Bar.js
Either package every common
dependency, every time it's needed...
Async loading + Modules
Overlapping dependencies...
Or put those commons to the page
onLoad
http://www.flickr.com/photos/redux/4298421692/
Pack_1 Pack_2 Pack_3
Pack_onLoad Foo.js Bar.js
Codebase at onLoad starts grow fast,
getting slower pageload :(
Async loading + Modules
So...
http://www.flickr.com/photos/redux/4298421692/
Either we have lots of
requests
Or big files, with massive
redundancy and
duplicated code.
Pack_1
Pack_2
Pack_3
Foo.js
Foo.js
Bar.js
Bar.js
Async loading + Modules
Are we keep circling around the
same issue?
http://www.flickr.com/photos/redux/4298421692/
JS Module Server
Time to rethink the way of
serving javascript instead of
loading it as a fully static asset.
Put the dependency handling to
the server side.
http://www.flickr.com/photos/alca_impenne/4295937972/
JS Module Server
Credit where it's due!
Malte Ubl & John Hjelmstad
http://www.youtube.com/watch?v=mGENRKrdoGY
Two guys at Google/Gmail
http://www.flickr.com/photos/alca_impenne/4295937972/
JS Module Server
Request just one module, the
server finds it's dependencies,
packs and serves.
/js/+app/Foo.js
On the client side, we keep track
of what modules have been
requested already.
http://www.flickr.com/photos/alca_impenne/4295937972/
JS Module Server
At the next requested module, we
tell the server which modules
were requested already
(note: not all the received, just the requested)
These are "subtracted" from the
request
/js/+app/Bar-app/Foo.js
http://www.flickr.com/photos/alca_impenne/4295937972/
JS Module Server
/js/+app/Bar-app/Foo.js
The server takes the requested module
dependencies, and subtracts all those, that
have been served for the client already.
The new package only contains code
that haven't been loaded by the client.
http://www.flickr.com/photos/alca_impenne/4295937972/
JS Module Server
/js/ +app/Bar - app/Foo.js
No redundant modules.
app/Bar
class/Lorem
class/Ipsum
class/Sit
...
app/Foo
class/Lorem
class/Sit
class/Dolor
...
app/Bar
class/Ipsum
...
JS Module Server
Providing solution for:
" lots of requests "
Thinking in feature-oriented code, instead of the page-
oriented code, these async loads can refer to only the
requested feature. One request per feature.
" big files, with massive redundancy and duplicated code "
Only that code is packaged, that haven't been served
already.
http://www.flickr.com/photos/alca_impenne/4295937972/
JS Module Server
Additional gains
● full i18n support (can require groups by locale)
● cacheable urls
● sourcemap support (via uglify2)
● the full codebase is loaded by the
server, it can run tasks on it, debug
output, etc. (strip all console.* for example)
● could handle CSS later (with supporting
preprocessors like SASS/LESS)
http://www.flickr.com/photos/alca_impenne/4295937972/
JS Module Server
● Parallel requests
(add a counter-callback parameter,
which determines the right parse order
for the module loader, like JSONP)
● Handle browser cache properly
(this one is a tricky one, but we're on
the way. Changing the url is 100% sure,
but If-Modified-Since would be nicer)
Things to be done
http://www.flickr.com/photos/alca_impenne/4295937972/
JS Module Server
Things to be done
● Put it to production
(soon on Ustream)
● Open source it
(soon on GitHub)
http://www.flickr.com/photos/alca_impenne/4295937972/
Questions?
Thank you!

More Related Content

What's hot

Play Framework: The Basics
Play Framework: The BasicsPlay Framework: The Basics
Play Framework: The Basics
Philip Langer
 
2012 04-19 theory-of_operation
2012 04-19 theory-of_operation2012 04-19 theory-of_operation
2012 04-19 theory-of_operation
bobwolff68
 
Joomladay Es 2009 - Nooku Framework
Joomladay Es 2009  - Nooku FrameworkJoomladay Es 2009  - Nooku Framework
Joomladay Es 2009 - Nooku Framework
Nooku
 

What's hot (20)

Link. apache wicket [santi caltabiano]
  Link. apache wicket [santi caltabiano]  Link. apache wicket [santi caltabiano]
Link. apache wicket [santi caltabiano]
 
Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"
 
jQuery Conference Boston 2011 CouchApps
jQuery Conference Boston 2011 CouchAppsjQuery Conference Boston 2011 CouchApps
jQuery Conference Boston 2011 CouchApps
 
Testing Mobile JavaScript
Testing Mobile JavaScriptTesting Mobile JavaScript
Testing Mobile JavaScript
 
Review Adobe Wallaby
Review Adobe WallabyReview Adobe Wallaby
Review Adobe Wallaby
 
Voiture tech talk
Voiture tech talkVoiture tech talk
Voiture tech talk
 
Blazor - An Introduction
Blazor - An IntroductionBlazor - An Introduction
Blazor - An Introduction
 
Refactoring to a SPA
Refactoring to a SPARefactoring to a SPA
Refactoring to a SPA
 
S/W Design and Modularity using Maven
S/W Design and Modularity using MavenS/W Design and Modularity using Maven
S/W Design and Modularity using Maven
 
Frontend Monoliths: Run if you can!
Frontend Monoliths: Run if you can!Frontend Monoliths: Run if you can!
Frontend Monoliths: Run if you can!
 
Getting started with spfx
Getting started with spfxGetting started with spfx
Getting started with spfx
 
Apache Flex and the imperfect Web
Apache Flex and the imperfect WebApache Flex and the imperfect Web
Apache Flex and the imperfect Web
 
Web worker in your angular application
Web worker in your angular applicationWeb worker in your angular application
Web worker in your angular application
 
Play Framework: The Basics
Play Framework: The BasicsPlay Framework: The Basics
Play Framework: The Basics
 
Enterprise makeover. Be a good web citizen, deliver continuously and change y...
Enterprise makeover. Be a good web citizen, deliver continuously and change y...Enterprise makeover. Be a good web citizen, deliver continuously and change y...
Enterprise makeover. Be a good web citizen, deliver continuously and change y...
 
CollabSphere 2018 - Java in Domino After XPages
CollabSphere 2018 - Java in Domino After XPagesCollabSphere 2018 - Java in Domino After XPages
CollabSphere 2018 - Java in Domino After XPages
 
2012 04-19 theory-of_operation
2012 04-19 theory-of_operation2012 04-19 theory-of_operation
2012 04-19 theory-of_operation
 
Engage 2015 - 10 Mistakes You and Every XPages Developer Make. Yes, I said YOU!
Engage 2015 - 10 Mistakes You and Every XPages Developer Make. Yes, I said YOU!Engage 2015 - 10 Mistakes You and Every XPages Developer Make. Yes, I said YOU!
Engage 2015 - 10 Mistakes You and Every XPages Developer Make. Yes, I said YOU!
 
Joomladay Es 2009 - Nooku Framework
Joomladay Es 2009  - Nooku FrameworkJoomladay Es 2009  - Nooku Framework
Joomladay Es 2009 - Nooku Framework
 
Create ReactJS Component & publish as npm package
Create ReactJS Component & publish as npm packageCreate ReactJS Component & publish as npm package
Create ReactJS Component & publish as npm package
 

Viewers also liked (6)

Jkadien slide share on youtube
Jkadien   slide share on youtubeJkadien   slide share on youtube
Jkadien slide share on youtube
 
Nbr9077
Nbr9077Nbr9077
Nbr9077
 
ENR Global Construction Summit 2013
ENR Global Construction Summit 2013ENR Global Construction Summit 2013
ENR Global Construction Summit 2013
 
Fever coach 디지털헬스케어
Fever coach 디지털헬스케어Fever coach 디지털헬스케어
Fever coach 디지털헬스케어
 
Headache
HeadacheHeadache
Headache
 
Headaches presentation at www.eyenirvaan.com
Headaches presentation at www.eyenirvaan.comHeadaches presentation at www.eyenirvaan.com
Headaches presentation at www.eyenirvaan.com
 

Similar to JS Module Server

webpack introductionNotice Demystifyingf
webpack introductionNotice Demystifyingfwebpack introductionNotice Demystifyingf
webpack introductionNotice Demystifyingf
MrVMNair
 
Tech 2 - Introduction to the Code
Tech 2 - Introduction to the CodeTech 2 - Introduction to the Code
Tech 2 - Introduction to the Code
AidIQ
 

Similar to JS Module Server (20)

A team 43 C
A team 43 CA team 43 C
A team 43 C
 
Webpack Introduction
Webpack IntroductionWebpack Introduction
Webpack Introduction
 
webpack introductionNotice Demystifyingf
webpack introductionNotice Demystifyingfwebpack introductionNotice Demystifyingf
webpack introductionNotice Demystifyingf
 
Developing for Mobile
Developing for MobileDeveloping for Mobile
Developing for Mobile
 
Webpack
WebpackWebpack
Webpack
 
Webpack
WebpackWebpack
Webpack
 
Improving build solutions dependency management with webpack
Improving build solutions  dependency management with webpackImproving build solutions  dependency management with webpack
Improving build solutions dependency management with webpack
 
Webpack: from 0 to 2
Webpack: from 0 to 2Webpack: from 0 to 2
Webpack: from 0 to 2
 
Social Connections VI — IBM Connections Extensions and Themes Demystified
Social Connections VI — IBM Connections Extensions and Themes DemystifiedSocial Connections VI — IBM Connections Extensions and Themes Demystified
Social Connections VI — IBM Connections Extensions and Themes Demystified
 
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
 
micro-frontends-with-vuejs
micro-frontends-with-vuejsmicro-frontends-with-vuejs
micro-frontends-with-vuejs
 
Seven Versions of One Web Application
Seven Versions of One Web ApplicationSeven Versions of One Web Application
Seven Versions of One Web Application
 
JSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendJSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontend
 
Front-end build tools - Webpack
Front-end build tools - WebpackFront-end build tools - Webpack
Front-end build tools - Webpack
 
[Patel] SPFx: An ISV Insight into latest Microsoft's customization model
[Patel] SPFx: An ISV Insight into latest Microsoft's customization model[Patel] SPFx: An ISV Insight into latest Microsoft's customization model
[Patel] SPFx: An ISV Insight into latest Microsoft's customization model
 
Modern UI Development With Node.js
Modern UI Development With Node.jsModern UI Development With Node.js
Modern UI Development With Node.js
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
 
Chris OBrien - Pitfalls when developing with the SharePoint Framework (SPFx)
Chris OBrien - Pitfalls when developing with the SharePoint Framework (SPFx)Chris OBrien - Pitfalls when developing with the SharePoint Framework (SPFx)
Chris OBrien - Pitfalls when developing with the SharePoint Framework (SPFx)
 
Tech 2 - Introduction to the Code
Tech 2 - Introduction to the CodeTech 2 - Introduction to the Code
Tech 2 - Introduction to the Code
 
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
 

More from Szabolcs Szabolcsi-Tóth

More from Szabolcs Szabolcsi-Tóth (6)

Healthy & fit wombats for the greater good
Healthy & fit wombats for the greater goodHealthy & fit wombats for the greater good
Healthy & fit wombats for the greater good
 
Cacheredirects
CacheredirectsCacheredirects
Cacheredirects
 
JavaScript - Hogyan készül
JavaScript - Hogyan készülJavaScript - Hogyan készül
JavaScript - Hogyan készül
 
Next.js & Apollo GraphQL: Using fetchMore()
Next.js & Apollo GraphQL: Using fetchMore()Next.js & Apollo GraphQL: Using fetchMore()
Next.js & Apollo GraphQL: Using fetchMore()
 
Redundant devops
Redundant devopsRedundant devops
Redundant devops
 
Cafè Terminal
Cafè TerminalCafè Terminal
Cafè Terminal
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 

JS Module Server

  • 1. Loading Javascript Szabolcsi-Tóth Szabolcs Senior Frontend Developer at Ustream. @_Nec nec@shell8.net
  • 2. Loading Javascript These slides are not about: detailing defer/FIF js embedding on site or putting the js after the body or in the head These slides are supposed to be about: detailing why static js loading, packaging and building leads to nowhere on the long run and a possible solution
  • 3. Inline scripting ● Quick sandboxing ● Small demos ● Learning ● Testing http://www.flickr.com/photos/pfly/399346124/
  • 4. Javascript Files ● Separate code from markup ● Serve assets from a server http://www.flickr.com/photos/seiho/2183406722/
  • 5. Code organization Webapp complexity increases Loadtime becomes an issue :( Use of functions, classes, modules Codebase grows http://www.flickr.com/photos/chiotsrun/4390949664/
  • 6. Code compilation / build Much less http requests Tools: ● concatenation ● uglify / yuicompressor http://www.flickr.com/photos/halfbisqued/2353845688/
  • 7. Code compilation / build Compile all the js that the current page might need: Several smaller files Compile all js that we need: One huge file Create js groups according to page needs http://www.flickr.com/photos/halfbisqued/2353845688/
  • 8. Code compilation / build Compile all the js that the current page might need: Several smaller files Compile all js that we need: One huge file Create js groups according to page needs What is the problem with that? http://www.flickr.com/photos/halfbisqued/2353845688/
  • 9. Take any page, coded, optimized, built, deployed
  • 10. Uh-Oh.. The Product / UX / Design division enters the room!
  • 11. The new killer feature, that saves us! Click here to close
  • 12. Code compilation / build The given feature is used on the page, or The visitor might use it on the page "the current page might need" One page gets n+1 new feature: The js compiled group for that page grows even more heavy Do we really need it onLoad? Lots of unused code, that waits for the user: overhead, slows load time. http://www.flickr.com/photos/halfbisqued/2353845688/
  • 13. Async loading! Load only the most necessary js onLoad! Then, for every feature the user wants, load the js runtime. http://www.flickr.com/photos/thenationalguard/8029811025/ ● Feature based code, not page based code ● Small lag in UX, but faster page start ● Loose module coupling, better code
  • 14. Code compilation / build Compile all the js that the current page might need: Several smaller files Compile all js that we need: One huge file Create js groups according to page needs And what is the problem with that? http://www.flickr.com/photos/halfbisqued/2353845688/
  • 15. Dependency handling The problem with predefined js groups: http://www.flickr.com/photos/wongjunhao/2761709029/ ● add js by planned use (add a feature, that can be used) ● add js by failsafe use ("this might come handy" or "make sure to have this") ● group is built at deploy
  • 16. Dependency handling The group is created for a planned usage The group contents has nothing to do with the real functioning site or app http://www.flickr.com/photos/wongjunhao/2761709029/
  • 17. Dependency handling Let the js codebase define it's own dependency Here come the modules! AMD (Asynchronous Module Definition) Designed for the web CommonJS Designed for the server (nodejs) http://www.flickr.com/photos/wongjunhao/2761709029/
  • 18. Dependency handling AMD structure define ( // optional, used in build code 'moduleName', // dependencies ['modules/Module1', 'modules/Module2'], // module definition function (module1, module2) { }); http://www.flickr.com/photos/wongjunhao/2761709029/
  • 19. Async loading + Modules Module loader a javascript utility, that: ● handles module definitions and requirements ● loads modules from a remote server require.js, curl.js, etc http://www.flickr.com/photos/redux/4298421692/
  • 20. Async loading + Modules Require.js example http://www.flickr.com/photos/redux/4298421692/ Every dependency starts a new request Lots of requests - not very good
  • 21. Async loading + Modules Need fewer requests? Introduce async packaging! http://www.flickr.com/photos/redux/4298421692/ Request and load packaged javascripts async, instead of each file. But how will the client know of the available package list?
  • 22. Async loading + Modules Possible solution: expose the built package list, and contents to the client http://www.flickr.com/photos/redux/4298421692/ Why not? In case of a large codebase, this can be an uncomfortably large amount of data. Also, exposes the whole codebase to the client.
  • 23. Async loading + Modules Another problem with pre-built packages, prepared for async load: Overlapping dependencies are inevitable http://www.flickr.com/photos/redux/4298421692/ Pack_1 Pack_2 Pack_3 Foo.js Foo.js Bar.js Bar.js Either package every common dependency, every time it's needed...
  • 24. Async loading + Modules Overlapping dependencies... Or put those commons to the page onLoad http://www.flickr.com/photos/redux/4298421692/ Pack_1 Pack_2 Pack_3 Pack_onLoad Foo.js Bar.js Codebase at onLoad starts grow fast, getting slower pageload :(
  • 25. Async loading + Modules So... http://www.flickr.com/photos/redux/4298421692/ Either we have lots of requests Or big files, with massive redundancy and duplicated code. Pack_1 Pack_2 Pack_3 Foo.js Foo.js Bar.js Bar.js
  • 26. Async loading + Modules Are we keep circling around the same issue? http://www.flickr.com/photos/redux/4298421692/
  • 27. JS Module Server Time to rethink the way of serving javascript instead of loading it as a fully static asset. Put the dependency handling to the server side. http://www.flickr.com/photos/alca_impenne/4295937972/
  • 28. JS Module Server Credit where it's due! Malte Ubl & John Hjelmstad http://www.youtube.com/watch?v=mGENRKrdoGY Two guys at Google/Gmail http://www.flickr.com/photos/alca_impenne/4295937972/
  • 29. JS Module Server Request just one module, the server finds it's dependencies, packs and serves. /js/+app/Foo.js On the client side, we keep track of what modules have been requested already. http://www.flickr.com/photos/alca_impenne/4295937972/
  • 30. JS Module Server At the next requested module, we tell the server which modules were requested already (note: not all the received, just the requested) These are "subtracted" from the request /js/+app/Bar-app/Foo.js http://www.flickr.com/photos/alca_impenne/4295937972/
  • 31. JS Module Server /js/+app/Bar-app/Foo.js The server takes the requested module dependencies, and subtracts all those, that have been served for the client already. The new package only contains code that haven't been loaded by the client. http://www.flickr.com/photos/alca_impenne/4295937972/
  • 32. JS Module Server /js/ +app/Bar - app/Foo.js No redundant modules. app/Bar class/Lorem class/Ipsum class/Sit ... app/Foo class/Lorem class/Sit class/Dolor ... app/Bar class/Ipsum ...
  • 33. JS Module Server Providing solution for: " lots of requests " Thinking in feature-oriented code, instead of the page- oriented code, these async loads can refer to only the requested feature. One request per feature. " big files, with massive redundancy and duplicated code " Only that code is packaged, that haven't been served already. http://www.flickr.com/photos/alca_impenne/4295937972/
  • 34. JS Module Server Additional gains ● full i18n support (can require groups by locale) ● cacheable urls ● sourcemap support (via uglify2) ● the full codebase is loaded by the server, it can run tasks on it, debug output, etc. (strip all console.* for example) ● could handle CSS later (with supporting preprocessors like SASS/LESS) http://www.flickr.com/photos/alca_impenne/4295937972/
  • 35. JS Module Server ● Parallel requests (add a counter-callback parameter, which determines the right parse order for the module loader, like JSONP) ● Handle browser cache properly (this one is a tricky one, but we're on the way. Changing the url is 100% sure, but If-Modified-Since would be nicer) Things to be done http://www.flickr.com/photos/alca_impenne/4295937972/
  • 36. JS Module Server Things to be done ● Put it to production (soon on Ustream) ● Open source it (soon on GitHub) http://www.flickr.com/photos/alca_impenne/4295937972/