1

I have an existing public Angular 20 website with no logins or authentication, and a few pages.

The client has recently asked to introduce Search Engine Optimization features. They mostly want each route to have its own <title>, <meta> and <og> tags. From what I have read, I can't achieve this without turning on SSR (Server Side Rendering) for the whole website.

The architecture is very simple, the website is served on a linux WM via nginx. Next to it runs a Mini Python Job. It collects a .json file containing the websites data and adds it to the website files. The website collects this json file on startup and builds from it on the client side.

All of the the routes title and meta values are constant. Only 1 page needs needs to fill the tags from the .json file

Normally this would be a small change, but with Angular this feels almost like having to rewrite most of my application logic and build. For example I save the language data into the localStorage, but I would probably need to include it in the route now for the server etc. Not to mention the increased server load due to the server side rendering.

I'm feeling that there might be a very simple solution here that I am not seeing, as opposed to just rendering everything on the server side and rewriting a lot of parts for this. same head tags every time, thats it. How can I keep most of the client side rendering while having static <title> and <meta> tags in Angular 20?

1 Answer 1

0

You don't need to rewrite your entire Angular site or activate SSR completely just to improve SEO with <title>, <meta> and <og>. Angular itself already offers a very practical solution called “prerendering”, which basically generates static HTML files for each route of your site during the build. This way, each page is ready with the correct tags, and you can serve everything normally through nginx, without needing Node.js running on the server.

To do this, just run a command to add SSR/prerendering support (ng add @angular/ssr), list the routes you want to generate as static and, in the components of each page, use Angular's Title and Meta services to define the titles and meta tags. Then, just run the build and the prerendering command, and that's it: each route will have its static HTML with the correct tags for SEO.

In the end, you don't need to change your front-end logic, mess with localStorage or language routes, or rewrite anything major. You just need to configure prerendering and make sure the tags are correct in each component. If you want, I can show you an example of a configuration or script for the dynamic page!

For exemple:

import { Component } from '@angular/core';
import { Title, Meta } from '@angular/platform-browser';

@Component({
  selector: 'app-about',
  template: `<h1>About Us</h1>`
})
export class AboutComponent {
  constructor(title: Title, meta: Meta) {
    title.setTitle('About Us - My Website');
    meta.addTags([
      { name: 'description', content: 'Description of the About Us page' },
      { property: 'og:title', content: 'About Us - My Website' }
    ]);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

By this you mean that I should list all routes as "rederMode.Prerender" in the "app.routes.server.ts"? I've tried it and I still get "ReferenceError: localStorage is not defined" when doing "ng serve". Also the services which store context when i move between the routes will be lost on navigation. From what I understand this setting will just render the whole HTML for me once and serve that to all users, which I can't do without the above listed issues. Also, I still don't understand how this will solve the issue where one page-s title/meta tags needs to be filled from the .json file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.