Share
Embed
2019-10-09-SPA_Critique_JS_Days.pdf
A rant about single-page apps (SPAs), their hostility towards users, their architectural problems, and the “classical” alternative.
Stefan Tilkov
October 09, 2019
Transcript
72 slides
-
What’s wrong with Single-Page Apps (and what to do about
it) Stefan Tilkov [email protected] @stilkov JavaScript Days 2019, Berlin "Science Museum, ceiling" by Elecé is licensed under CC BY 2.0 -
@stilkov 2. Send them to the home page when they
hit “refresh” … -
@stilkov 3. … or at least ensure the browser pops
up a warning window -
@stilkov 8. Show users a picture of your app –
it’s surely better than nothing -
@stilkov 1) in the SOAP/WSDL sense “Web app”2) 2) built
as a careless SPA “Web service”1) > Uses HTTP as transport > Ignores HTTP verbs > Ignores URIs > Exposes single “endpoint” > Fails to embrace the Web > Uses browser as runtime > Ignores forward, back, refresh > Does not support linking > Exposes monolithic “app” > Fails to embrace the browser -
@stilkov The web-native way of distributing logic Process Flow Presentation
Domain Logic Data Server Client > Rendering, layout, styling on an unknown client > Logic & state machine on server > Client user-agent extensible via code on demand -
@stilkov HTML & Hypermedia • In REST, servers expose a
hypermedia format • Option 1: Just invent your own JSON-based, incomplete clone • Option 2: Just use HTML • Clients need to be RESTful, too • Option 1: Invent your own, JS-based, buggy, incomplete implementation • Option 2: Use the browser -
@stilkov A great REST hypermedia API is very similar to
a simple, server-sided rendered web application -
@stilkov State Business Logic Routing Rendering Logic Look & Presentation
Logic Server Client HTML -
@stilkov State Business Logic Routing Rendering Logic Look & Presentation
Logic Server Client JSON -
@stilkov State Business Logic Routing Rendering Logic Look & Presentation
Logic Server Client JSON JSON API JSON Client -
@stilkov State Business Logic Routing Rendering Logic Look & Presentation
Logic Server Client JSON JSON API JSON Client -
@stilkov Single Page Apps – Why Routing? Solution: Store some
app state in the URI! Bookmarks? Deep links? Reload? -
@stilkov State Business Logic Routing Rendering Logic Look & Presentation
Logic Server Client JSON JSON API JSON Client -
@stilkov State Business Logic Routing Rendering Logic Look & Presentation
Logic Server Client JSON JSON API JSON Client (Pre-)Rendering Logic Routing HTML -
@stilkov Hydration How to simulate readiness? What about Events (Clicks
etc)? How to match server-side HTML to client-side DOM? -
@stilkov State Business Logic Routing Rendering Logic Look & Presentation
Logic Server Client JSON JSON API JSON Client (Pre-)Rendering Logic Routing HTML -
@stilkov State Business Logic Routing Rendering Logic Look & Presentation
Logic Server Client JSON JSON API JSON Client (Pre-)Rendering Logic Routing HTML Hydration -
@stilkov State Routing Rendering Logic Look & Presentation Logic Server
Client JSON JSON Client (Pre-)Rendering Logic Routing HTML Hydration Business Logic JSON API State Business Logic Same functionality, different languages! -
@stilkov State Routing Rendering Logic Look & Presentation Logic Server
Client JSON JSON Client (Pre-)Rendering Logic Routing HTML Hydration Business Logic JSON API State Business Logic Much, much more JavaScript -
@stilkov Everyone has JavaScript, right? All your users are non-JS
while they're downloading your JS If they're on a train and their net connection goes away before your JavaScript loads, then there's no JavaScript. Did the HTTP request for the JavaScript complete? Does the corporate firewall block JavaScript? Does their ISP or mobile operator interfere with downloaded JavaScript? Does their browser support the JavaScript you’ve written? Do your users get the JavaScript? -
@stilkov ROCA RESTful Server-side HTML (SSR) Application logic only on
server No duplicated logic on client + No application logic on client! Client-side (self contained) JavaScript components = -
@stilkov Client Side Logic is generic Presentation logic only. It
enhances HTML HTML CSS Content Layout JavaScript Presentation logic -
@stilkov Progressive Enhancement A web page needs to work without
graphical elements, CSS and JS This ensures our page will even work under unfavorable circumstances We also lay the foundation for accessibility Does not mean that everything needs to work without CSS and JavaScript It means that the fundamental functionality of our web page relies on HTML only Every thing that goes beyond that is seen as an enhancement -
@stilkov $('.multiselect', context).each(function() { $(this).multiselect({ selectedList: 2, checkAllText: "Alle", uncheckAllText:
"Keinen" }).multiselectfilter({label:"", width:"200px"}); }); <div class="filter-column"> <label for="project">Project</label> <select class="multiselect" id="project" name="project" size="5" multiple> <option>DISCOVER</option> <option>IMPROVE</option> <option >MAGENTA</option> <option>ROCA</option> <option>ROCKET</option> </select> </div> -
@stilkov Component <audio src="meow.mp4" controls> Browser Platform Component Component
Component Application -
@stilkov <div class="tabs"> <ul> <li> <a href="#about">About</a> … <li> <a
href="#comments">Discussion</a> </ul> <p id="about">lorem ipsum dolor sit amet</p> … <ol id="comments"> … </ol> </div> $(".tabs").tabs(); -
@stilkov <div class="tabs"> <ul> <li> <a href="#about">About</a> … <li> <a
href="#comments">Discussion</a> </ul> <p id="about">lorem ipsum dolor sit amet</p> … <ol id="comments"> … </ol> </div> $(".tabs").tabs(); -
@stilkov <div class="tabs"> <ul> <li> <a href="#about">About</a> … <li> <a
href="#comments">Discussion</a> </ul> <p id="about">lorem ipsum dolor sit amet</p> … <ol id="comments"> … </ol> </div> $(".tabs").tabs(); Unobtrusive JavaScript -
@stilkov Component Browser Platform Component Component Component Glue Code HTML
JS CSS HTML JS CSS HTML JS CSS HTML JS CSS ✓ Progressive Enhancement -
@stilkov Component Browser Platform Component Component Component Glue Code HTML
JS CSS HTML JS CSS HTML JS CSS HTML JS CSS ✓ Progressive Enhancement Progressive enhancement is not about dealing with old browsers, it's about dealing with new browsers. — Jeremy Keith (@adactio) -
@stilkov <div class="tabs"> <ul> <li> <a href="#about">About</a> … <li> <a
href="#comments">Discussion</a> </ul> <p id="about">lorem ipsum dolor sit amet</p> … <ol id="comments"> … </ol> </div> $(".tabs").tabs(); -
@stilkov <tab-nav> <ul> <li> <a href="#about">About</a> … <li> <a href="#comments">Discussion</a>
</ul> </tab-nav> <p id="about">lorem ipsum dolor sit amet</p> … <ol id="comments"> … </ol> $(".tabs").tabs(); -
@stilkov <tab-nav> <ul> <li> <a href="#about">About</a> … <li> <a href="#comments">Discussion</a>
</ul> </tab-nav> <tab-contents> <p id="about">lorem ipsum dolor sit amet</p> … <ol id="comments"> … </ol> </tab-contents> $(".tabs").tabs(); -
@stilkov <tab-nav> <ul> <li> <a href="#about">About</a> … <li> <a href="#comments">Discussion</a>
</ul> </tab-nav> <tab-contents> <p id="about">lorem ipsum dolor sit amet</p> … <ol id="comments"> … </ol> </tab-contents> $(".tabs").tabs(); Custom Elements -
@stilkov Classic, server-side rendered web applications are much simpler, use
less bandwidth, scale better, are more resilient, and a good match for most user needs -
@stilkov Many single-page apps are built carelessly and improve developer
experience (if at all) at the cost of decreased usability and architectural complexity -
@stilkov Any sufficiently complicated JavaScript client application contains an ad
hoc, informally- specified, bug-ridden, slow implementation of half a browser. (Me, with apologies to Phillip Greenspun) Why choose a monolith if you don’t have to? -
@stilkov In-page Cross-page JavaScript method calls Links & redirects Shared
abstractions & frameworks Micro-architecture Common language runtime HTTP HTML 5 JS platform Standard Browser -
@stilkov Sometimes, JS-centric applications/SPAs are the right choice, but very
rarely for every part of your system -
@stilkov If you’re a fan of single page apps, at
least build more than one • Don’t reinvent browser integration features • Accept some inefficiency • Trade-off for framework independence • Avoid modularity à la Java EE, OSGi etc. -
@stilkov Use your favorite SPA framework – when it’s appropriate.
Stick to basic, simpler stuff, when it’s sufficient – likely, most of the time. -
@stilkov Stefan Tilkov @stilkov [email protected] Phone: +49 170 471 2625
innoQ Deutschland GmbH Krischerstr. 100 40789 Monheim am Rhein Germany Phone: +49 2173 3366-0 innoQ Schweiz GmbH Gewerbestr. 11 CH-6330 Cham Switzerland Phone: +41 41 743 0116 www.innoq.com Ohlauer Straße 43 10999 Berlin Germany Phone: +49 2173 3366-0 Ludwigstr. 180E 63067 Offenbach Germany Phone: +49 2173 3366-0 Kreuzstraße 16 80331 München Germany Phone: +49 2173 3366-0 @stilkov That’s all I have. Thanks for listening! -
@stilkov www.innoq.com OFFICES Monheim Berlin Offenbach Munich Hamburg Zurich FACTS
~150 employees Privately owned Vendor-independent SERVICES Strategy & technology consulting Digital business models Software architecture & development Digital platforms & infrastructures Knowledge transfer, coaching & trainings CLIENTS Finance Telecommunications Logistics E-commerce Fortune 500 SMBs Startups