WRANGLING LARGE SCALE FRONTEND WEB APPLICATIONS @ryan_roemer | surge2015.formidablelabs.com
The web is massively moving to the frontend
Users want rich and seamless experiences
Product owners want fast and nimble apps
Browser apps are now business critical
And, yes, even for the enterprise
... which means
LOTS OF JAVASCRIPT IN THE BROWSER WRITTEN BY LARGE TEAMS
Let’s dig into some large frontends at a high-traffic, top-five e-commerce site
The Numbers $10 billion FY 2014, $13 billion FY 2015 (est) 1.5 billion page views for Thanksgiving Cyber Monday 2014
The Code 50+ JS applications 650K lines, 2500 files of JavaScript source More JavaScript lines & files than Java
The Technologies
The Dev Team A 2+ year website rewrite 50+ core frontend engineers 14 vertical teams / "tracks" ... and many external teams / partners
A TOUR THROUGH THE TRENCHES
My wrangling adventures as the JavaScript lead for the website & dev teams
A few battle-tested tips from the field...
... with a focus on four personas
ARCHITECTS GUIDES GATEKEEPERS LIFEGUARDS
ARCHITECTS
Plan & build a strong foundation
Architects A real build Code organization
THE FUNDAMENTAL CHALLENGE: JAVASCRIPT IS THE WILD, WILD WEST
Architecture Challenges Browser is a single execution environment Code size / duplication is critical Hard to manage, easy to do wrong "Cowboy" legacy vs. large scale organization
Let's look at the architectural complexities of just one page...
THE HOMEPAGE
The Homepage Multiple, independent JS apps Code from 8 different teams Exemplary "in transition" page, somewhere between raw JavaScript & a SPA
JavaScript ON PAGE Direct page scripts 10 remote scripts 18 inline scripts LAZY LOADED Injected script tags 6 app entry points
On Page 2 application code 2 CDN infrastructure 2 internal ads 2 Google ads 1 fonts 1 IE-conditional polyfill 18 inline scripts
Lazy (Code) wno.etyfnto( { idw_nr(ucin) rqie[hae/edr] fnto ( { eur("edrhae", ucin ) rqie[hae/edrdfre") eur("edrhae-eerd]; }; ) / .. / . rqie[hmpg/oeae]; eur("oeaehmpg") / .. / . }; )
Lazy (App) Asynchronously loaded 1 shared library 4 primary entry points 2 deferred entry points
THE TAKEAWAY? IT'S COMPLICATED.
A "REAL" BUILD
Build Challenges Modern JS apps are complicated (compression, polyfills, transpiling, etc.) Multiple JS apps on the same page is even more complex Supporting development & production / CDN
Use a Build Tool Choose an paradigm: AMD, CommonJS Choose a build tool / loader: RequireJS, Browserify, Webpack Take time to learn & evaluate the tradeoffs
Our Build Tools Legacy: AMD + RequireJS Modern: CommonJS + Webpack Transition: AMD & CommonJS + Webpack
Keep prod & dev builds blazingly fast
Make development identical to production
Build Complexities Sharing / caching common code (l b j ) i.s Varying repository structures Sharing frontend & backend code Combining application, OSS, & 3rd party code
CODE ORGANIZATION
Plan your repository structure 1 → 14 → many repos
Have a bias for small & flexible
GUIDES
Coordinate the chaos, level up the development teams
Guides & Guidance The Meta team Educate Review everything
Guidance Challenges Thinking of the project as a whole Onboarding unfamiliar / junior developers Helping teams be consistent / complementary
THE META TEAM
Code from multiple teams all combined on one page
These folks represent the whole page
The Meta Team Set conventions & best practices Lead code reviews for consistency / DRY Develop common utilities & the deployment infrastructure
YOUR META TEAM TASK: HAVE ONE.
Our Meta JS Team 1.5 dedicated developers 6 "loaned" track developers
Our Meta JS Duties ~1 day of code review / week Chat channel participation Cross-track JS initiatives (i18n, multi-tenancy, PCI, etc.)
Meta JS Benefits Project-wide consistency & support Help tracks consider other teams Represent track interests in the core More engineers to support the whole project
EDUCATION
Continually write living documentation, close to the code
Take a "hands on approach"
Invest in rising developers & spread knowledge back to teams
CODE REVIEW
All code gets substantive & meta review
Including all third party & internal to the org vendor code
GATEKEEPERS
Protect the frontend through process & architecture
Gates & Gatekeepers Automate quality Minimize risks / exposure Require performance
Gatekeeping Challenges Feature pressure Poor quality code Unknown included code
AUTOMATE QUALITY
Static checking (eslint, jshint, jscs, etc.)
Clientside unit tests
Integration / E2E tests
Code coverage
Continuous integration (CI)
MINIMIZE RISK
Learn / identify your biggest risk areas
Protect yourself wherever possible
Architecture risks: Injected HTML/CSS/JS
Code pattern risks: Defer & pray / Imgesn.. / ' usig. vrHPFLYEOG_IE=20; a OEUL_NUHTM 00 / Wi utlrayfrnx se. / at ni ed o et tp stieu(ucin( { eTmotfnto ) teettp) hNxSe(; } HPFLYEOG_IE; , OEUL_NUHTM)
Deployment / process risks: "Hotfeatures"
REQUIRE PERFORMANCE
Frontend code must be small & fast
Teams are "feature-driven" unless performance is enforced & required
Enforce with audits "Web App Performance Measurement, Monitoring and Resiliency" www.webpagetest.org
Enforce with process
Enforce with automation "Automating Web Performance Measurement"
Build your own tools where necessary
LIFEGUARDS
Create escape hatches & lifelines in production
Lifeguards & Life Savers Logging, Monitoring Debugging helpers
Lifeguarding Challenges Code executes remotely on different browsers Frontend errors are costly & often invisible
LOGGING & MONITORING
Your code runs & fails on a variety of browsers out in the wild
Log & capture everything
Get errors & messages to a remote store
And then aggregate, report, & alert
Providers Rollbar Loggly Sentry Airbrake
DEBUGGING SUPPORT
Give developers life lines when things go wrong
You ship this: !ucin)vre"el Sre"a$"h /"; fnto({a =Hlo ug!,=(<1 >) atx()$"oy)apn()(; .ete,(bd".peda})
Your devs want this: (ucin( { fnto ) vrmsae="el Sre" a esg Hlo ug!; vr$edn =$"h /"; a haig (<1 >) $edn.etmsae; haigtx(esg) $"oy)apn(haig; (bd".ped$edn) }); ()
Source Maps / ..LT MR CD ..* * . OS OE OE . / [,idw_nr=}) ]wno.etyc(; /#sucMpigR=tp/dvwlatcm97/sds-rned / oreapnULht:/e.amr.o:83j-itfotn
Publish in VPN on deployment
Hidden from end users Available to developers
All Together Now A "real" build Organized code The Meta team Education Code Review Automate quality Minimize risks Require performance Logging, Monitoring Debugging helpers
Some parting thoughts on the future
Embrace change
Reevaluate & refactor your infrastructure & organization
Have a transition strategy
HAPPY WRANGLING
THANKS! surge2015.formidablelabs.com
Or sign in using your account
Do you have the account? Create account