wrangling large scale frontend web applications

110
@ryan_roemer | surge2015.formidablelabs.com WRANGLING LARGE SCALE FRONTEND WEB APPLICATIONS

Upload: ryan-roemer

Post on 21-Apr-2017

18.212 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Wrangling Large Scale Frontend Web Applications

@ryan_roemer | surge2015.formidablelabs.com

WRANGLINGLARGE SCALEFRONTEND WEB APPLICATIONS

Page 2: Wrangling Large Scale Frontend Web Applications

The web ismassively moving

to the frontend

Page 3: Wrangling Large Scale Frontend Web Applications

Users want richand seamlessexperiences

Page 4: Wrangling Large Scale Frontend Web Applications

Product ownerswant fast andnimble apps

Page 5: Wrangling Large Scale Frontend Web Applications

Browser apps arenow business

critical

Page 6: Wrangling Large Scale Frontend Web Applications

And, yes, even forthe enterprise

Page 7: Wrangling Large Scale Frontend Web Applications

... which means

Page 8: Wrangling Large Scale Frontend Web Applications

LOTS OF JAVASCRIPTIN THE BROWSERWRITTEN BY LARGE TEAMS

Page 9: Wrangling Large Scale Frontend Web Applications

Let’s dig into some largefrontends at a high-traffic,top-five e-commerce site

Page 12: Wrangling Large Scale Frontend Web Applications

The Code50+ JS applications650K lines, 2500 files of JavaScript sourceMore JavaScript lines & files than Java

Page 14: Wrangling Large Scale Frontend Web Applications

The Dev TeamA 2+ year website rewrite50+ core frontend engineers14 vertical teams / "tracks"... and many external teams / partners

Page 15: Wrangling Large Scale Frontend Web Applications

A TOUR THROUGHTHE TRENCHES

Page 17: Wrangling Large Scale Frontend Web Applications

My wrangling adventures

as the JavaScript lead for

the website & dev teams

Page 18: Wrangling Large Scale Frontend Web Applications

A few battle-tested

tips from the field...

Page 19: Wrangling Large Scale Frontend Web Applications

... with a focus on

four personas

Page 20: Wrangling Large Scale Frontend Web Applications

ARCHITECTS

GUIDES

GATEKEEPERS

LIFEGUARDS

Page 21: Wrangling Large Scale Frontend Web Applications

ARCHITECTS

Page 22: Wrangling Large Scale Frontend Web Applications

Plan & build a strongfoundation

Page 23: Wrangling Large Scale Frontend Web Applications

ArchitectsA real buildCode organization

Page 24: Wrangling Large Scale Frontend Web Applications

THE FUNDAMENTAL CHALLENGE:JAVASCRIPT IS THEWILD, WILD WEST

Page 25: Wrangling Large Scale Frontend Web Applications

Architecture ChallengesBrowser is a single execution environmentCode size / duplication is criticalHard to manage, easy to do wrong"Cowboy" legacy vs. large scale organization

Page 26: Wrangling Large Scale Frontend Web Applications

Let's look at the

architectural complexities

of just one page...

Page 27: Wrangling Large Scale Frontend Web Applications

THE HOMEPAGE

Page 29: Wrangling Large Scale Frontend Web Applications

The HomepageMultiple, independent JS appsCode from 8 different teamsExemplary "in transition" page, somewherebetween raw JavaScript & a SPA

Page 30: Wrangling Large Scale Frontend Web Applications

JavaScript

ON PAGE

Direct page scripts10 remote scripts18 inline scripts

LAZY LOADED

Injected script tags6 app entry points

Page 31: Wrangling Large Scale Frontend Web Applications

On Page

2 application code2 CDN infrastructure2 internal ads2 Google ads

1 fonts1 IE-conditional polyfill18 inline scripts

Page 32: Wrangling Large Scale Frontend Web Applications

Lazy (Code)window._entry(function() { require(["header/header"], function () { require(["header/header-deferred"]); }); // ... require(["homepage/homepage"]); // ...});

Page 33: Wrangling Large Scale Frontend Web Applications

Lazy (App)Asynchronously loaded1 shared library4 primary entry points2 deferred entry points

Page 34: Wrangling Large Scale Frontend Web Applications

THE TAKEAWAY?IT'S COMPLICATED.

Page 35: Wrangling Large Scale Frontend Web Applications

A "REAL" BUILD

Page 36: Wrangling Large Scale Frontend Web Applications

Build ChallengesModern JS apps are complicated(compression, polyfills, transpiling, etc.)Multiple JS apps on the same page is evenmore complexSupporting development & production / CDN

Page 37: Wrangling Large Scale Frontend Web Applications

Use a Build ToolChoose an paradigm: AMD, CommonJSChoose a build tool / loader: RequireJS,Browserify, WebpackTake time to learn & evaluate the tradeoffs

Page 38: Wrangling Large Scale Frontend Web Applications

Our Build ToolsLegacy: AMD + RequireJSModern: CommonJS + WebpackTransition: AMD & CommonJS + Webpack

Page 39: Wrangling Large Scale Frontend Web Applications

Keep prod & dev buildsblazingly fast

Page 40: Wrangling Large Scale Frontend Web Applications

Make development

identical to production

Page 41: Wrangling Large Scale Frontend Web Applications

Build ComplexitiesSharing / caching common code (lib.js)Varying repository structuresSharing frontend & backend codeCombining application, OSS, & 3rd party code

Page 42: Wrangling Large Scale Frontend Web Applications

CODE ORGANIZATION

Page 43: Wrangling Large Scale Frontend Web Applications

Plan your repositorystructure

1 → 14 → many repos

Page 44: Wrangling Large Scale Frontend Web Applications

Have a bias for small &flexible

Page 45: Wrangling Large Scale Frontend Web Applications

GUIDES

Page 46: Wrangling Large Scale Frontend Web Applications

Coordinate the chaos,

level up the developmentteams

Page 47: Wrangling Large Scale Frontend Web Applications

Guides & GuidanceThe Meta teamEducateReview everything

Page 48: Wrangling Large Scale Frontend Web Applications

Guidance ChallengesThinking of the project as a wholeOnboarding unfamiliar / junior developersHelping teams be consistent /complementary

Page 49: Wrangling Large Scale Frontend Web Applications

THE META TEAM

Page 50: Wrangling Large Scale Frontend Web Applications

Code from multiple teams

all combined on one page

Page 51: Wrangling Large Scale Frontend Web Applications

These folks represent thewhole page

Page 52: Wrangling Large Scale Frontend Web Applications

The Meta TeamSet conventions & best practicesLead code reviews for consistency / DRYDevelop common utilities & the deploymentinfrastructure

Page 53: Wrangling Large Scale Frontend Web Applications

YOUR META TEAM TASK:

HAVE ONE.

Page 54: Wrangling Large Scale Frontend Web Applications

Our Meta JS Team1.5 dedicated developers6 "loaned" track developers

Page 55: Wrangling Large Scale Frontend Web Applications

Our Meta JS Duties~1 day of code review / weekChat channel participationCross-track JS initiatives (i18n, multi-tenancy,PCI, etc.)

Page 56: Wrangling Large Scale Frontend Web Applications

Meta JS BenefitsProject-wide consistency & supportHelp tracks consider other teamsRepresent track interests in the coreMore engineers to support the whole project

Page 57: Wrangling Large Scale Frontend Web Applications

EDUCATION

Page 58: Wrangling Large Scale Frontend Web Applications

Continually write livingdocumentation,

close to the code

Page 59: Wrangling Large Scale Frontend Web Applications

Take a "hands on approach"

Page 60: Wrangling Large Scale Frontend Web Applications

Invest in rising developers &

spread knowledge back toteams

Page 61: Wrangling Large Scale Frontend Web Applications

CODE REVIEW

Page 62: Wrangling Large Scale Frontend Web Applications

All code gets substantive &

meta review

Page 63: Wrangling Large Scale Frontend Web Applications

Including all third party &

internal to the org vendorcode

Page 64: Wrangling Large Scale Frontend Web Applications

GATEKEEPERS

Page 65: Wrangling Large Scale Frontend Web Applications

Protect the frontend through

process & architecture

Page 66: Wrangling Large Scale Frontend Web Applications

Gates & GatekeepersAutomate qualityMinimize risks / exposureRequire performance

Page 67: Wrangling Large Scale Frontend Web Applications

GatekeepingChallenges

Feature pressurePoor quality codeUnknown included code

Page 68: Wrangling Large Scale Frontend Web Applications

AUTOMATE QUALITY

Page 69: Wrangling Large Scale Frontend Web Applications

Static checking

(eslint, jshint, jscs, etc.)

Page 70: Wrangling Large Scale Frontend Web Applications

Clientside unit tests

Page 71: Wrangling Large Scale Frontend Web Applications

Integration / E2E tests

Page 72: Wrangling Large Scale Frontend Web Applications

Code coverage

Page 73: Wrangling Large Scale Frontend Web Applications

Continuous integration (CI)

Page 74: Wrangling Large Scale Frontend Web Applications

MINIMIZE RISK

Page 75: Wrangling Large Scale Frontend Web Applications

Learn / identify your biggestrisk areas

Page 76: Wrangling Large Scale Frontend Web Applications

Protect yourself whereverpossible

Page 77: Wrangling Large Scale Frontend Web Applications

Architecture risks:

Injected HTML/CSS/JS

Page 78: Wrangling Large Scale Frontend Web Applications

Code pattern risks:

Defer & pray

// I'm guessing...var HOPEFULLY_ENOUGH_TIME = 2000;

// Wait until ready for next step.setTimeout(function () { theNextStep();}, HOPEFULLY_ENOUGH_TIME);

Page 79: Wrangling Large Scale Frontend Web Applications

Deployment / process risks:

"Hotfeatures"

Page 80: Wrangling Large Scale Frontend Web Applications

REQUIRE PERFORMANCE

Page 81: Wrangling Large Scale Frontend Web Applications

Frontend code must be small& fast

Page 82: Wrangling Large Scale Frontend Web Applications

Teams are "feature-driven"

unless performance isenforced & required

Page 83: Wrangling Large Scale Frontend Web Applications

Enforce with audits

"Web App PerformanceMeasurement, Monitoring and

Resiliency"

www.webpagetest.org

Page 84: Wrangling Large Scale Frontend Web Applications

Enforce with process

Page 85: Wrangling Large Scale Frontend Web Applications

Enforce with automation

"Automating WebPerformance Measurement"

Page 86: Wrangling Large Scale Frontend Web Applications

Build your own tools wherenecessary

Page 87: Wrangling Large Scale Frontend Web Applications

LIFEGUARDS

Page 88: Wrangling Large Scale Frontend Web Applications

Create escape hatches &

lifelines in production

Page 89: Wrangling Large Scale Frontend Web Applications

Lifeguards & LifeSavers

Logging, MonitoringDebugging helpers

Page 90: Wrangling Large Scale Frontend Web Applications

LifeguardingChallenges

Code executes remotely on differentbrowsersFrontend errors are costly & often invisible

Page 91: Wrangling Large Scale Frontend Web Applications

LOGGING & MONITORING

Page 92: Wrangling Large Scale Frontend Web Applications

Your code runs & fails

on a variety of browsers

out in the wild

Page 93: Wrangling Large Scale Frontend Web Applications

Log & capture everything

Page 94: Wrangling Large Scale Frontend Web Applications

Get errors & messages

to a remote store

Page 95: Wrangling Large Scale Frontend Web Applications

And then aggregate,

report, & alert

Page 97: Wrangling Large Scale Frontend Web Applications

DEBUGGING SUPPORT

Page 98: Wrangling Large Scale Frontend Web Applications

Give developers life lines

when things go wrong

Page 99: Wrangling Large Scale Frontend Web Applications

You ship this:

!function(){var e="Hello Surge!",a=$("<h1 />");a.text(e),$("body").append(a)}();

Page 100: Wrangling Large Scale Frontend Web Applications

Your devs want this:

(function () { var message = "Hello Surge!"; var $heading = $("<h1 />");

$heading.text(message);

$("body").append($heading);}());

Page 101: Wrangling Large Scale Frontend Web Applications

Source Maps

/* ... LOTS MORE CODE ... */[],window._entry=c}();//# sourceMappingURL=http://dev.walmart.com:9873/js-dist-frontend/core-bundle.js.map

Page 102: Wrangling Large Scale Frontend Web Applications

Publish in VPN ondeployment

Page 103: Wrangling Large Scale Frontend Web Applications

Hidden from end users

Available to developers

Page 104: Wrangling Large Scale Frontend Web Applications

All Together Now

A "real" buildOrganized codeThe Meta teamEducationCode Review

Automate qualityMinimize risksRequire performanceLogging, MonitoringDebugging helpers

Page 105: Wrangling Large Scale Frontend Web Applications

Some partingthoughts on the

future

Page 106: Wrangling Large Scale Frontend Web Applications

Embrace change

Page 107: Wrangling Large Scale Frontend Web Applications

Reevaluate & refactor your

infrastructure & organization

Page 108: Wrangling Large Scale Frontend Web Applications

Have a transition strategy

Page 109: Wrangling Large Scale Frontend Web Applications

HAPPYWRANGLING

Page 110: Wrangling Large Scale Frontend Web Applications

surge2015.formidablelabs.com

THANKS!