SlideShare a Scribd company logo
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

PDF
Front-end optimisation & jQuery Internals
Artur Cistov
 
PDF
Browser Performance Tests - Internet Explorer 11 vs Firefox 25 vs Google Chro...
MIDAS
 
PPTX
Introduction to Web Components & Polymer Workshop - U of I WebCon
John Riviello
 
PDF
Front-end optimisation & jQuery Internals (Pycon)
Artur Cistov
 
PDF
High Performance JavaScript (Amazon DevCon 2011)
Nicholas Zakas
 
PPTX
High Performance JavaScript (CapitolJS 2011)
Nicholas Zakas
 
PDF
Cordova: APIs and instruments
Ivano Malavolta
 
PDF
Modern Web Application Development Workflow - EclipseCon France 2014
Stéphane Bégaudeau
 
Front-end optimisation & jQuery Internals
Artur Cistov
 
Browser Performance Tests - Internet Explorer 11 vs Firefox 25 vs Google Chro...
MIDAS
 
Introduction to Web Components & Polymer Workshop - U of I WebCon
John Riviello
 
Front-end optimisation & jQuery Internals (Pycon)
Artur Cistov
 
High Performance JavaScript (Amazon DevCon 2011)
Nicholas Zakas
 
High Performance JavaScript (CapitolJS 2011)
Nicholas Zakas
 
Cordova: APIs and instruments
Ivano Malavolta
 
Modern Web Application Development Workflow - EclipseCon France 2014
Stéphane Bégaudeau
 

What's hot (20)

TXT
Link. apache wicket [santi caltabiano]
santi caltabiano
 
PDF
Viktor Turskyi "Effective NodeJS Application Development"
Fwdays
 
KEY
jQuery Conference Boston 2011 CouchApps
Bradley Holt
 
PDF
Testing Mobile JavaScript
jeresig
 
PPT
Review Adobe Wallaby
Julio Cesar Retamal Rojas
 
ZIP
Voiture tech talk
Hoppinger
 
PPTX
Blazor - An Introduction
JamieTaylor112
 
PPTX
Refactoring to a SPA
Marcello Teodori
 
PPTX
S/W Design and Modularity using Maven
Scheidt & Bachmann
 
PDF
Frontend Monoliths: Run if you can!
Jonas Bandi
 
PPTX
Getting started with spfx
Jenkins NS
 
PDF
Apache Flex and the imperfect Web
masuland
 
PPTX
Web worker in your angular application
Suresh Patidar
 
PDF
Play Framework: The Basics
Philip Langer
 
PDF
Enterprise makeover. Be a good web citizen, deliver continuously and change y...
Mateusz Kwasniewski
 
PPTX
CollabSphere 2018 - Java in Domino After XPages
Jesse Gallagher
 
PDF
2012 04-19 theory-of_operation
bobwolff68
 
PDF
Engage 2015 - 10 Mistakes You and Every XPages Developer Make. Yes, I said YOU!
Serdar Basegmez
 
PDF
Joomladay Es 2009 - Nooku Framework
Nooku
 
PDF
Create ReactJS Component & publish as npm package
Andrii Lundiak
 
Link. apache wicket [santi caltabiano]
santi caltabiano
 
Viktor Turskyi "Effective NodeJS Application Development"
Fwdays
 
jQuery Conference Boston 2011 CouchApps
Bradley Holt
 
Testing Mobile JavaScript
jeresig
 
Review Adobe Wallaby
Julio Cesar Retamal Rojas
 
Voiture tech talk
Hoppinger
 
Blazor - An Introduction
JamieTaylor112
 
Refactoring to a SPA
Marcello Teodori
 
S/W Design and Modularity using Maven
Scheidt & Bachmann
 
Frontend Monoliths: Run if you can!
Jonas Bandi
 
Getting started with spfx
Jenkins NS
 
Apache Flex and the imperfect Web
masuland
 
Web worker in your angular application
Suresh Patidar
 
Play Framework: The Basics
Philip Langer
 
Enterprise makeover. Be a good web citizen, deliver continuously and change y...
Mateusz Kwasniewski
 
CollabSphere 2018 - Java in Domino After XPages
Jesse Gallagher
 
2012 04-19 theory-of_operation
bobwolff68
 
Engage 2015 - 10 Mistakes You and Every XPages Developer Make. Yes, I said YOU!
Serdar Basegmez
 
Joomladay Es 2009 - Nooku Framework
Nooku
 
Create ReactJS Component & publish as npm package
Andrii Lundiak
 
Ad

Viewers also liked (6)

PPTX
Jkadien slide share on youtube
jkadien
 
PDF
Nbr9077
Rubem Thales
 
PPT
ENR Global Construction Summit 2013
enrevents
 
PPTX
Fever coach 디지털헬스케어
Jaewon Shin
 
PPT
Headache
thekumar
 
PPT
Headaches presentation at www.eyenirvaan.com
Eyenirvaan
 
Jkadien slide share on youtube
jkadien
 
Nbr9077
Rubem Thales
 
ENR Global Construction Summit 2013
enrevents
 
Fever coach 디지털헬스케어
Jaewon Shin
 
Headache
thekumar
 
Headaches presentation at www.eyenirvaan.com
Eyenirvaan
 
Ad

Similar to JS Module Server (20)

PDF
A team 43 C
aldenustream
 
PPTX
Webpack Introduction
Anjali Chawla
 
PPTX
webpack introductionNotice Demystifyingf
MrVMNair
 
PDF
Developing for Mobile
Remy Sharp
 
PPTX
Webpack
Anjali Chawla
 
PPT
Webpack
Mallikarjuna G D
 
PPTX
Improving build solutions dependency management with webpack
NodeXperts
 
PDF
Webpack: from 0 to 2
Alessandro Bellini
 
PDF
Social Connections VI — IBM Connections Extensions and Themes Demystified
Claudio Procida
 
PPTX
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JSFestUA
 
PPTX
Webpack/Parcel: What’s Happening Behind the React App?
Talentica Software
 
PDF
micro-frontends-with-vuejs
Oleksandr Tserkovnyi
 
PDF
Seven Versions of One Web Application
Yakov Fain
 
PDF
JSFest 2019: Technology agnostic microservices at SPA frontend
Vlad Fedosov
 
PDF
Front-end build tools - Webpack
Razvan Rosu
 
PPTX
[Patel] SPFx: An ISV Insight into latest Microsoft's customization model
European Collaboration Summit
 
PDF
Modern UI Development With Node.js
Ryan Anklam
 
PPTX
Node JS Express : Steps to Create Restful Web App
Edureka!
 
PPTX
Chris OBrien - Pitfalls when developing with the SharePoint Framework (SPFx)
Chris O'Brien
 
PDF
Tech 2 - Introduction to the Code
AidIQ
 
A team 43 C
aldenustream
 
Webpack Introduction
Anjali Chawla
 
webpack introductionNotice Demystifyingf
MrVMNair
 
Developing for Mobile
Remy Sharp
 
Webpack
Anjali Chawla
 
Improving build solutions dependency management with webpack
NodeXperts
 
Webpack: from 0 to 2
Alessandro Bellini
 
Social Connections VI — IBM Connections Extensions and Themes Demystified
Claudio Procida
 
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JSFestUA
 
Webpack/Parcel: What’s Happening Behind the React App?
Talentica Software
 
micro-frontends-with-vuejs
Oleksandr Tserkovnyi
 
Seven Versions of One Web Application
Yakov Fain
 
JSFest 2019: Technology agnostic microservices at SPA frontend
Vlad Fedosov
 
Front-end build tools - Webpack
Razvan Rosu
 
[Patel] SPFx: An ISV Insight into latest Microsoft's customization model
European Collaboration Summit
 
Modern UI Development With Node.js
Ryan Anklam
 
Node JS Express : Steps to Create Restful Web App
Edureka!
 
Chris OBrien - Pitfalls when developing with the SharePoint Framework (SPFx)
Chris O'Brien
 
Tech 2 - Introduction to the Code
AidIQ
 

More from Szabolcs Szabolcsi-Tóth (6)

PDF
Healthy & fit wombats for the greater good
Szabolcs Szabolcsi-Tóth
 
PDF
Cacheredirects
Szabolcs Szabolcsi-Tóth
 
PDF
JavaScript - Hogyan készül
Szabolcs Szabolcsi-Tóth
 
PDF
Next.js & Apollo GraphQL: Using fetchMore()
Szabolcs Szabolcsi-Tóth
 
PDF
Redundant devops
Szabolcs Szabolcsi-Tóth
 
PDF
Cafè Terminal
Szabolcs Szabolcsi-Tóth
 
Healthy & fit wombats for the greater good
Szabolcs Szabolcsi-Tóth
 
Cacheredirects
Szabolcs Szabolcsi-Tóth
 
JavaScript - Hogyan készül
Szabolcs Szabolcsi-Tóth
 
Next.js & Apollo GraphQL: Using fetchMore()
Szabolcs Szabolcsi-Tóth
 
Redundant devops
Szabolcs Szabolcsi-Tóth
 
Cafè Terminal
Szabolcs Szabolcsi-Tóth
 

Recently uploaded (20)

PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Software Development Methodologies in 2025
KodekX
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
The Future of Artificial Intelligence (AI)
Mukul
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 

JS Module Server

  • 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/