DEV Community

Saunak Surani
Saunak Surani

Posted on

Server-Side Rendering (SSR) in Angular 16

Server-side rendering (SSR) is a technique for rendering web pages on the server before sending them to the client. This has a number of benefits, including:

  • Improved SEO: Search engines can index the rendered pages, which can improve your website's search engine ranking.
  • Faster initial load times: The rendered pages are already loaded when the client receives them, so the initial load time is much faster.
  • Better performance on mobile devices: SSR can improve the performance of your website on mobile devices, which is important for a growing number of users.

Angular 16 has made significant improvements to SSR, making it easier to implement and more performant. In this article, we will take a look at the basics of SSR in Angular 16 and how to get started.

What is SSR in Angular?

In Angular, SSR is implemented using the @nguniversal/express-engine package. This package allows you to render your Angular application on the server using Express, a popular web framework.

To use SSR in Angular, you need to do the following:

  1. Install the @nguniversal/express-engine package.
  2. Enable SSR in your Angular application.
  3. Configure Express to serve your Angular application.

Enabling SSR in Angular

To enable SSR in your Angular application, you need to add the following to your app.module.ts file:

import { enableServerSideRendering } from '@nguniversal/express-engine';

@NgModule({
  ...
  providers: [
    enableServerSideRendering(),
  ],
  ...
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

This will enable SSR for your Angular application.

Configuring Express to serve your Angular application

Once you have enabled SSR in your Angular application, you need to configure Express to serve your application. You can do this by creating a new file called server.js and adding the following code:

const express = require('express');
const app = express();
const port = 3000;

app.engine('html', require('@nguniversal/express-engine').render);
app.set('view engine', 'html');
app.set('views', __dirname + '/dist');

app.get('/', (req, res) => {
  res.render('index.html');
});

app.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});
Enter fullscreen mode Exit fullscreen mode

This code will start an Express server on port 3000 and serve your Angular application.

Testing SSR in Angular

To test SSR in Angular, you can start the Express server and then navigate to http://localhost:3000 in your browser. You should see the rendered version of your Angular application.

Conclusion

SSR is a powerful technique that can improve the performance and SEO of your Angular applications. In Angular 16, SSR has been made easier to implement and more performant. If you are looking for ways to improve the performance of your Angular applications, SSR is a good option to consider.

I hope this article has been helpful. If you have any questions, please feel free to leave a comment below.

Top comments (1)

Collapse
 
armen96work profile image
armen96work

Is there a way to get Hostname with @angular/ssr?