2018
We're here
@phenomnominal
2018
@phenomnominal
2018
You know some Angular ✅
You've heard of Angular Universal ✅
1️⃣ What problems does Angular Universal solve?
2️⃣ How does it solve those problems?
3️⃣ How do we make our applications work with Universal?
4️⃣ Why would you not want to use Universal?
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
Aiming for mobile-first
Lots of JavaScript
Lots of DOM
Lots of $$watchers
Lots of jank
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
Rolling out to mobile users
682
@NgModules
Too much JavaScript
Making good 💰💰💰
Fully rebuilt in Angular
@phenomnominal
2018
trademe.nz/listing/:id
<html>
<link
rel="stylesheet"
href="/static/styles.css">
<script
src="/static/script.js">
<body>
<trade-me>
<div
class="tm-loading">
</div>
</trade-me>
</body>
</html>
rel="stylesheet"
href="/static/styles.css"
src="/static/script.js"
class="tm-loading"
"stylesheet"
"/static/styles.css"
"/static/script.js"
"tm-loading"
Generic application loading state
Static resources
@phenomnominal
2018
trademe.nz/static/script.js
trademe.nz/static/styles.css
function () {
...
}
html {
...
}
() {
...
}
{
...
}
...
...
@phenomnominal
2018
trademe.nz/api/listing/:id.json
trademe.nz/api/categories.json
{"id":"1234567890", ... }
{"name":"Arts & Crafts", ... }
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
trademe.nz/listing/:id
<html>
<link
rel="stylesheet"
href="/static/styles.css">
<script
src="/static/script.js">
<body>
<trade-me>
<trade-me-homepage>
<trade-me-search>
<trade-me-categories>
</trade-me-homepage>
</trade-me>
</body>
<script
id="server-app-state">
</script>
</html>
rel="stylesheet"
href="/static/styles.css"
src="/static/script.js"
id=
"stylesheet"
"/static/styles.css"
"/static/script.js"
"server-app-state"
Real application content!
Inlined server request data
@phenomnominal
2018
@phenomnominal
2018
Slower "Time to First Byte" due to server processing time
Faster "First Paint" over client-only application, particularly for mobile devices
This can be mitigated with caching - both on the server & on the client with a service worker
Slower "Time to Interactive" due to client-side rehydration
Faster discovery of resources required for your application
Measure, experiment, and find what works for your app
@phenomnominal
2018
Some crawlers have JavaScript capabilities, but who knows how that really works (not me!)
Support for browsers with JavaScript disabled, or old engines.
(Check out Igor Minar's talk from ng-conf 2018 for how angular.io handles SEO without Angular Univeral)
If you're competing against other websites, server rendering can give you an advantage over client-only SEO
@phenomnominal
2018
Pre-rendered content for Facebook, Twitter, etc.
@phenomnominal
2018
@phenomnominal
2018
Angular Universal!
Still too much JavaScript
Making even more 💰💰💰
🚀
🦖
🌎
🦕
@phenomnominal
2018
⭐️
🛰
⭐️
⭐️
⭐️
⭐️
⭐️
⭐️
⭐️
⭐️
🆖
🛸
@phenomnominal
2018
Sam Neil! (He's a kiwi!)
@phenomnominal
2018
https://www.youtube.com/watch?v=wU1HbwC71j4
Flea!
(He's Aussie, close enough)
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
💥
💥
💥
💥
💥
💥
💥
💥
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
⭐️ A new app Angular CLI app
⭐️ Build steps for two different versions of the app and the server
⭐️ All the necessary Angular Universal dependencies
⭐️ A new server.ts file which contains the server code
⭐️ A modified app.module.ts that uses withServerTransition
⭐️ A new app.server.module.ts file for the server application
⭐️ A modified main.ts that waits for DOMContentLoaded before bootstrap
⭐️ A new main.server.ts file for the server bootstrap
@phenomnominal
2018
JS Logo as seen from NZ
@phenomnominal
2018
Require the built Universal app
Set up the engine for running the application
Pass all requests to the engine
Create the Express server
Start the server
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
window is not defined
@phenomnominal
2018
@phenomnominal
2018
https://www.youtube.com/watch?v=_trUBHaUAR0
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
Do not use the DOM directly! There's almost always a way to do the same thing with Angular's abstractions.
@phenomnominal
2018
@Input, @Output, @HostBinding, @ViewChild, @ContentChild, ngStyle, ngClass.
You may occasionally need to reach for the Renderer:
Normal dependency injection
@phenomnominal
2018
@phenomnominal
2018
Be defensive!
Explicit null type is good
Only run code if window is available.
@phenomnominal
2018
Use custom services to abstract around DOM APIs.
Only fall back to the actual DOM if you have to
Optional injection token!
Use the optionally injected value if available
@phenomnominal
2018
Some third-party code won't want to play nicely...
(here's lookin' at you three-trackballcontrols 😅)
Set up globals
Require troublesome module
Clean up after
Directly check for window
@phenomnominal
2018
@phenomnominal
2018
If you really must...
Injectable PLATFORM_ID token
Use the method for the specific platform
@phenomnominal
2018
@phenomnominal
2018
document is not defined
@phenomnominal
2018
@phenomnominal
2018
Add @Optional injection annotation
Trusty if statement
Check for the presence of an injected service:
@phenomnominal
2018
Turn things off at a provider level:
Provide null for specific platform
Corresponding injection token
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
Remember app.module.ts & app.server.module.ts?
@phenomnominal
2018
Provide different implementations for different environments:
Server implementation for Renderer
Server implementation for HttpClient
@phenomnominal
2018
@phenomnominal
2018
The whole application runs twice.
This means duplicate API calls! 🤯
This call happens twice
@phenomnominal
2018
Inject TransferState
Create a state key
Set the state on the first run
Re-use it if it's available
@phenomnominal
2018
The state transfer data is inlined into the HTML response as a blob of JSON. This means...
@phenomnominal
2018
Or just transfer your whole store state!
One state key for your whole store
Read the whole store and serialise
@phenomnominal
2018
Or just transfer your whole store state!
Use the same key on the client
Dispatch a new action with the whole serialised store
@phenomnominal
2018
Use a simple cache layer to prevent duplicated calls
Split the GET into two parts
Mark each API response with a cache time
Only do the call if the cache time has expired
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
Rocket Lab! (Also NZ)
@phenomnominal
2018
It's very slow...
@phenomnominal
2018
💥
💥
💥
💥
💥
💥
💥
💥
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018
@phenomnominal
2018