How to Build SPAs With Angular Universal

How to Build SPAs With Angular Universal

Single Page Applications (SPAs) have become a popular choice for developers due to their fast performance and dynamic user experiences. Angular, a platform that enables developers to build SPAs efficiently, can be further enhanced with Angular Universal, which adds server-side rendering (SSR) capabilities. This article will guide you through the steps to build SPAs with Angular Universal, ensuring you create a high-performing and SEO-friendly application.

What is Angular Universal?

Angular Universal is a tool that allows developers to render Angular applications on the server side. By doing so, it improves the SEO of your application since search engines can easily crawl and index your content. Furthermore, it provides faster initial loading times, enhancing user experience.

Setting Up Your Angular Application

The first step in building an SPA using Angular Universal is to set up your Angular application. Open your terminal and create a new Angular project by running:

ng new your-project-name

Once the setup is complete, navigate into your project directory:

cd your-project-name

Installing Angular Universal

Next, you need to add Angular Universal to your project. You can do this by running the following command:

ng add @nguniversal/express-engine

This command will configure your Angular app for server-side rendering, adding all necessary dependencies and modifying your application files.

Understanding the Project Structure

After installing Angular Universal, your project structure will include several new files. The most important additions are:

  • server.ts: This file handles the server-side rendering of your application.
  • app.server.module.ts: This module is responsible for bootstrapping the server-side version of the app.
  • index.html: This is the main HTML file for your app, which will be served by the server.

Creating Routes and Components

As with any Angular application, you will have to create components and routes that define the user experience. Use the Angular CLI commands to generate components:

ng generate component example

Implement routing by defining the routes in your app-routing.module.ts file. Ensure you include the RouterModule entries for your components.

Enabling Server-Side Rendering

In the server.ts file, you need to set up the Express server to serve your application. Here’s a basic example of how to do it:

const express = require('express');
const { ngExpressEngine } = require('@nguniversal/express-engine');
const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');
const app = express();
const PORT = process.env.PORT || 4000;
app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(routes)
  ]
}));
app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));
app.get('*', (req, res) => {
  res.render('index', { req });
});
app.listen(PORT, () => {
  console.log(`Node server listening on http://localhost:${PORT}`);
});

Building the Application

Once everything is set up, you can build your application for both server and browser. Use the following command to build your app:

npm run build:ssr

This command compiles your Angular application, making it ready for server-side and client-side rendering.

Testing Your Application

To test your SPA with server-side rendering, run:

npm run serve:ssr

This command starts your server, allowing you to access your application at http://localhost:4000.

Optimizing for SEO

With Angular Universal handling server-side rendering, your application is already more SEO-friendly than standard SPAs. However, remember to include meta tags, titles, and descriptions in your components to enhance visibility on search engines. Utilize Angular's Meta service to dynamically update these tags based on the route and content.

Conclusion

Building SPAs with Angular Universal combines