Better SEO & sharing on social media platforms with
Angular Universal
Sam Vloeberghs�@samvloeberghs
Things to learn (or recap) today
2
ngSpain 2019 - 19/10/2019
Sam Vloeberghs
What is the goal of Universal?
anywhere,�framework agnostic
4
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Angular anywhere
Framework agnostic
5
But would it make sense to do it??
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Generic solution
No “native framework support ” available on the backend server?
Use Socket Engine to support any language (via a JavaScript detour)
6
.NET Server
PHP Server
Socket Engine (JS )
CLIENTS�browsers / search engines / ...
JS Server
Python Server
Java Server
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Ultimate goal
7
Retrieve a stable state �of the application
on any platform at run or build time
Prerender that stable state �to static HTML
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
hold on to this thought
Is Angular Universal
the right tool for the job?
Are you starting a new project?
Is your team
skilled in Angular?
Do you have strong SEO requirements?
9
YES
NO
NO REAL ADDED VALUE
but there are valid use cases ..
Are you starting a new project?
Is your team
skilled in Angular?
Do you have strong SEO requirements?
10
NO!
YES!
CONSIDER USING A DIFFERENT SSR FRAMEWORK
Are you starting a new project?
Is your team
skilled in Angular?
Do you have strong SEO requirements?
11
GO FOR IT!
YES!
YES!
Do you really need Universal?
Why & when (not) to use Universal?
Would your application benefit from Universal? Why?
13
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
A few basic rules, IMO
14
Sam Vloeberghs
Blog or�personal website
Informative company website
Product pages
of a webshop
ngSpain 2019 - 19/10/2019
A few basic rules, IMO
Why? Because it adds complexity!
15
Sam Vloeberghs
Backoffice of that same webshop
Enterprise application
Mobile application or PWA?
ngSpain 2019 - 19/10/2019
Complex things about Universal (1/2)
Not all dependencies are available on all platforms �(or via the same way or approach)
window / navigator / document
cookies / Storage API
Check the platform you are running the application on
16
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
17
use the platformId + the isPlatformBrowser or isPlatformServer method to switch functionality depending on environment
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
18
depending on the environment we read out the cookie from the browser or the ExpressJS request
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Complex things about Universal (2/2)
Avoid reloading the data of your application
19
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
20
6) rebuild news page with data from server
Browser
1) request dynamic page /news/1/slug
Server
DB
3) return full html + js + css
= angular application
static page
NG app
5) request news data /api/news/1
2) SSR with dynamic data
4) angular bootstrap
Sam Vloeberghs
can we avoid this extra round trip to the server?
ngSpain 2019 - 19/10/2019
21
1) request dynamic page /news/1/slug
3) return full html + js + css
+ SERIALIZED DATA
= angular application
4) angular bootstrap
5) rebuild news page based on SERIALIZED DATA
Browser
NG app
static page
{}
{}
Server
DB
2) SSR with dynamic data
{}
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Now what was that about stable state?
Stable Application State?
23
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Stable when
isStable: Observable<boolean> on ApplicationRef
24
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
How can Universal help with �SEO and Social sharing?
Different ways to optimise for SEO
& social sharing
26
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Initial content
27
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
28
CLIENT SIDE RENDERING
29
SERVER SIDE RENDERING
Correct usage of meta & other tags
All search engines behave differently
30
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
31
inject the title value as meta/title tags with name or property depending on the social network
use the Meta & Title service provided by Angular to do the heavy lifting of manipulating DOM
cleanup if no value given
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Validate your pages
32
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Watch out for “over-optimization”
of the initial content
not all search engines or crawlers �are equally capable �of parsing or executing HTML / JS
Example: HTML Minification
33
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
34
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
35
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
36
these “optimizations” do
more harm than good
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
37
😮
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
38
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
How does dependency injection help?
39
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Example: JSON - LD
Very good for SEO!
40
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
41
Sam Vloeberghs
The “library” defining the list of types of objects
The data associated with the defined object @type
ngSpain 2019 - 19/10/2019
42
F.A.Q.
REVIEWS / PRICING
Rendering JSON - LD
43
Remember the isStable Observable?��=> extra hooks, named BEFORE_APP_SERIALIZED
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Server vs Browser - App Module
44
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
AppServerModule is basically an extension and overwrite of the AppModule
Server vs Browser - JsonLd Module
45
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
46
provide a serialize function factory�that gets executed during BEFORE_APP_SERALIZED
inject the serialized JsonLd state as the least script tag in the <head>
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
END
RESULT
47
Sam Vloeberghs
plus of course all your content available at time of app serialization!
ngSpain 2019 - 19/10/2019
48
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Conclusion (1/3)
Goal of Angular Universal
50
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Conclusion (2/3)
Different ways to optimise for SEO & social sharing
51
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Conclusion (3/3)
Make use of dependency injection
52
Sam Vloeberghs
ngSpain 2019 - 19/10/2019
Better SEO & sharing on social media platforms with Angular Universal�
@ ngSpain 2019 - Madrid, Spain - 19/10/2019
Thank you for your attention. Feel free to contact me at sam.vloeberghs@gmail.com or directly on Twitter @samvloeberghs