<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Keanu William Ellwood on Medium]]></title>
        <description><![CDATA[Stories by Keanu William Ellwood on Medium]]></description>
        <link>https://medium.com/@kellwood_94274?source=rss-30fbf3a95c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*NkELHKbXNBYAikC7M9TWFw.jpeg</url>
            <title>Stories by Keanu William Ellwood on Medium</title>
            <link>https://medium.com/@kellwood_94274?source=rss-30fbf3a95c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 04:00:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kellwood_94274/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Angular Universal: Server-Side Rendering and Pre-Rendering for Angular 9+ Applications]]></title>
            <link>https://medium.com/dvt-engineering/angular-universal-server-side-rendering-and-pre-rendering-for-angular-9-applications-f311e8f545bb?source=rss-30fbf3a95c------2</link>
            <guid isPermaLink="false">https://medium.com/p/f311e8f545bb</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[serverside-rendering]]></category>
            <category><![CDATA[angular-universal]]></category>
            <category><![CDATA[comparison]]></category>
            <category><![CDATA[angular-9]]></category>
            <dc:creator><![CDATA[Keanu William Ellwood]]></dc:creator>
            <pubDate>Wed, 27 May 2020 21:12:19 GMT</pubDate>
            <atom:updated>2020-05-27T21:12:19.036Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="An ultra-wide monitor displaying code inside an editor alongside a cup of coffee, sits on a wooden desk." src="https://cdn-images-1.medium.com/max/1024/0*mtfaFhxfBfDWjfNU" /><figcaption>Photo by <a href="https://unsplash.com/@casparrubin?utm_source=medium&amp;utm_medium=referral">Caspar Camille Rubin</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>What Is Angular Universal?</h3><p>Angular Universal is a toolkit that allows us to do server-side rendering (SSR) and pre-rendering for our Angular applications.</p><h4>Why Use Angular Universal?</h4><p>Angular applications are single-paged apps (SPAs) - this means a single HTML document is served to the client, which can then be dynamically rewritten on the client browser-based on data received from the server, instead of serving a static page when routing. Angular Universal allows the app to be executed on the server, which lets us generate and serve static HTML to the client in response to navigation. This means that when a user opens your Angular app, the server will show the user a pre-rendered version of the app which they can browse while the client-side application loads behind-the-scenes. When the client-side application has finished loading, the application switches from the pre-rendered app to the fully interactive client-side app, without the user even noticing.</p><h4>Why Use Server-Side Rendering?</h4><ul><li><strong>Improve Search Engine Optimization (</strong><a href="https://support.google.com/webmasters/answer/7451184?hl=en"><strong>SEO</strong></a><strong>)</strong> — Companies like Google and Facebook make use of web crawlers to understand the content of your app so that a search engine can index that content and makes it easier for users to find on the web. Most web crawlers have a hard time extracting content from SPAs, which can pose problems for SEO. Luckily, Angular Universal generates a static app, which allows web crawlers to seamlessly traverse our app.</li><li><strong>Show the first page quickly </strong>— The time it takes the browser to render the first piece of DOM content (Images, SVGs, etc.) is known as First Contentful Paint (FCP). In <a href="https://tech.ebayinc.com/engineering/speed-by-a-thousand-cuts/">2019, eBay</a> found that for each 100-millisecond improvement in search page load time, they saw a 0.5% increase to their ‘Add to cart’ count.</li><li><strong>Improve performance for low-powered devices</strong> — Many devices are unable to execute JavaScript reliably, which becomes a major issue when we do a lot of scripting on the client, such as rendering a PWA. <a href="https://angular.io/guide/universal#improve-performance-on-mobile-and-low-powered-devices">If this is the case, we might have to serve the client with a server-rendered, no-JavaScript version of the app.</a></li></ul><figure><img alt="A modern laptop with a code editor opened, on a white table." src="https://cdn-images-1.medium.com/max/1024/0*SLj0z9COvvBvZVpY" /><figcaption>Photo by <a href="https://unsplash.com/@clemhlrdt?utm_source=medium&amp;utm_medium=referral">Clément H</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>What’s new in Angular Universal 9?</h4><ul><li><strong>Live code reload for SSR</strong>— The power of ng serve comes to SSR with Dev server! Now every time you make a change to your SSR app in development, it will automatically recompile the client and server bundles and live-reload the browser.</li><li><strong>Pre-rendering out of the box </strong>— It is now possible for us to generate and save static pages to the file system at build time, which can then be served to the client. All routes can be pre-rendered during build time.</li><li><strong>Easy to add</strong> — Before the 9.0 release, adding Universal to your app could be a confusing matter, with additional parameters and setup required. However, adding server-side rendering to your project is now easier than ever! Just run ng add @nguniversal/express-engine and your app is set for SSR.</li></ul><h3>Add Angular Universal To Your App</h3><p>Let’s start by creating a new Angular project using the <a href="https://cli.angular.io/">Angular CLI</a>.</p><pre>ng new universal-app<br><em>**? Would you like to add Angular routing? No<br>**? Which stylesheet format would you like to use? CSS</em></pre><pre>cd universal-app</pre><p>Next, we install the Angular Universal schematic to set our app up for server-side rendering and pre-rendering.</p><pre>ng add @nguniversal/express-engine </pre><p>If you are running an older Universal schematic and you want to upgrade, run:</p><pre>ng update @nguniversal/express-engine</pre><h4>Comparing First Contentful Paint and Search Engine Optimization between the client-side and server-side apps.</h4><p>First, we add a script inside index.html to log FCP in the browser.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/601ed0d1090571e53942eba9967a8067/href">https://medium.com/media/601ed0d1090571e53942eba9967a8067/href</a></iframe><p>Let&#39;s start the client-side rendered (CSR) version of our app using npm run start, and the server-side rendered (SSR) version with npm run build:ssr &amp;&amp; npm run serve:ssr. The CSR and SSR apps can be opened concurrently, as they load on separate ports by default. The CSR app is available on port <a href="http://localhost:4200/">4200</a>, which can be changed in package.json. The SSR app is available on port <a href="http://localhost:4000/">4000</a>, which can be changed in the generated server.ts file.</p><p>The First Contentful Paint (FCP) of the client-side app on initial load is <em>770ms</em> and on subsequent reloads ~<em>440ms</em>. The FCP of the server-side app on initial load is <em>110ms</em> and on subsequent reloads ~<em>75ms</em>. Already, with this simple example, we can see that the server-side app starts showing page content to the user in a fraction of the time it takes the client-side app to do the same.</p><p>We can also see how server-side rendering helps SEO, by comparing the page-source for both the client-side and server-side apps. The client-side application only shows <em>&lt;app-root&gt;&lt;/app-root&gt; </em>with nothing else inside it, whereas the server-side app displays the page CSS and all the app content coming from <em>&lt;app-root&gt;&lt;/app-root&gt;. </em>This makes it much easier for a web-crawler to scrape our app’s content.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*mE-EY5VJ1WXkJHEiRofPIw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uA21VwnUUpZ5hOPn1i1jVw.png" /><figcaption>Client-side rendered source (left) vs Server-side rendered source (right).</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*IrR8MW2gvGkWP8VnoF7ztA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*Wv6rkU6ciwYJAGGdJ6vQ0A.png" /><figcaption>The SEO scores for the CSR app (left) and the SSR app (right). These scores were determined with the auditing tool, Lighthouse, which is included in Chrome DevTools.</figcaption></figure><p>That’s it for the basics of Angular Universal and Server-side Rendering!</p><h4>Easy development of SSR apps with Dev Server</h4><p>As a developer, you can now easily see the SSR app reload when making changes to the app by using the new Dev server.</p><pre>npm run dev:ssr</pre><p>This allows for the same fast reload capabilities we have with using <strong>npm run start</strong>when developing client-side apps.</p><h4>Pre-render any page with Angular Universal</h4><p>Angular Universal 9 brings us a powerful tool in pre-rendering. Let’s say you have a bunch of simple pages that don’t need to be rendered server-side or rendered on the client every time a user makes a request. Angular Universal 9 now allows you to cache these pages as static files, which can then be served to the client via your CDN or a simple server. How?</p><p>In your Angular application, navigate to the angular.json file and look for the prerender builder (at the very end of the default angular.json). The builder comes with a routes option, which allows you to specify the routes of the app pages you wish to pre-render.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3cfb7f8750db29d5479a41fc4c9c9d3f/href">https://medium.com/media/3cfb7f8750db29d5479a41fc4c9c9d3f/href</a></iframe><p>If you want a bunch of your app pages to pre-render, but you don’t want to provide all the routes manually, you can change the routes option to the routesFile option. The routesFile option allows you to point to a relative url that contains all the predefined routes for the pages you want to pre-render at build time. You can fill this file with the desired routes using a script or any other method you prefer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa313d11e49272c9bce33f752891f829/href">https://medium.com/media/fa313d11e49272c9bce33f752891f829/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be0a73670fa193ad641b8169284a35b3/href">https://medium.com/media/be0a73670fa193ad641b8169284a35b3/href</a></iframe><p>Then just build the app and serve!</p><pre>npm run prerender<br>npm run serve:ssr</pre><p>You can find the pre-rendered files in your dist/&lt;project-name&gt;/browser . If you want to verify that your page has been pre-rendered just navigate to localhost:4000/&lt;route-name&gt;/index.html or if you are using a cdn just navigate to the route and inspect the page-source. At the bottom of the page you will see a small comment.</p><blockquote>This page was pre-rendered with Angular Universal</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*f0YjPQ8ou5oAQjB1hfis8g.png" /><figcaption>Pre-rendered page source.</figcaption></figure><h3>When To Use Server-side Rendering and Pre-Rendering</h3><p>If you have decided to implement Angular Universal into your application, you might want to consider which approach is better for the current situation.</p><h4><strong>When to use Server-side Rendering?</strong></h4><ul><li>The application or web pages you want to render on the server are very dynamic.</li><li>If you have content fetched asynchronously on pages where SEO is important.</li></ul><h4>When to use Pre-Rendering?</h4><ul><li>The route you want to pre-render is more-or-less completely static.</li><li>If your application does not need to be updated frequently, you can run the build script after you’ve made changes to the static pages and just publish the /dist folder again.</li></ul><h3>Other Angular Universal Packages</h3><p>Angular Universal has been integrated with ASP.NET Core. The ASP.NET Core Engine can be used for running Angular Apps on the server for server-side rendering. <a href="https://github.com/angular/universal/tree/master/modules/aspnetcore-engine">ASP.NET Core Engine</a></p><p>The <a href="https://github.com/angular/universal/tree/master/modules/socket-engine">Angular Universal Socket Engine</a> provides Angular Universal rendering for any language, framework or platform you choose to use. As long as it can connect to a TCP Socket (which all frameworks can) then you’re good to go.</p><p>The Angular Universal team has also provided a <a href="https://github.com/angular/universal/tree/master/modules/hapi-engine">Hapi Engine</a> package for running Angular Apps on the server for server-side rendering.<a href="https://hapi.dev/"> Hapi </a>(<a href="https://github.com/hapijs/hapi.dev">Hapi.js</a>) is most commonly used to build API servers, websites, and HTTP proxy applications.</p><p><strong>You can find all other available Angular Universal packages on </strong><a href="https://github.com/angular/universal">https://github.com/angular/universal</a></p><h3>Conclusion</h3><p>As we have seen, server-side rendering is mainly used to improve the load time of our applications. It does this by converting HTML files on the server into static application pages, which is then sent to the browser where it can be bootstrapped on the client.</p><p>Most web crawlers are unable to properly scrape content on apps that rely heavily on JavaScript, thus server-side rendering is a must if you are optimizing your Angular app for SEO. Server-side rendering allows web crawlers to scrape app content and index that content to make it searchable on the web, and can provide faster pages, which in turn can give your app an SEO ranking boost.</p><p>I hope that this post was of help to you if you were looking to integrate server-side rendering or pre-rendering into your Angular application.</p><h3><strong>Additional Resources</strong></h3><ol><li><a href="https://overcast.fm/+GbgiuJrHQ">AngularAir — What’s new in Angular Universal</a> with Vikram Subramanian (<a href="https://www.youtube.com/watch?v=D_fREyRnETM">YouTube</a>)</li><li><a href="https://angular.io/guide/universal">Angular Universal Documentation</a></li><li><a href="https://nitayneeman.com/posts/all-talks-from-ng-conf-hardwired-2020/">ngConf Hardwired 2020</a></li><li><a href="https://angular.io/guide/aot-compiler">Angular Ahead-of-time (AOT) compilation</a></li><li><a href="https://github.com/vikerman/toh-universal">Tour of Heroes + Angular Universal 9</a> (Vikram Subramanian)</li><li><a href="https://www.thinkwithgoogle.com/marketing-resources/data-measurement/mobile-page-speed-new-industry-benchmarks/">Google Mobile Page Speed Benchmarks</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f311e8f545bb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dvt-engineering/angular-universal-server-side-rendering-and-pre-rendering-for-angular-9-applications-f311e8f545bb">Angular Universal: Server-Side Rendering and Pre-Rendering for Angular 9+ Applications</a> was originally published in <a href="https://medium.com/dvt-engineering">DVT Software Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>