</>
Skip to content
Angular lessons (41/43)

Angular — SSR

What is SSR?

Server-Side Rendering renders the Angular app on the server before sending to the client.

Enable SSR

ng new my-app --ssr

How SSR Works

  1. Request comes to server
  2. Angular renders on server
  3. HTML sent to client
  4. JavaScript bootstraps app

Benefits

BenefitDescription
SEOSearch engines can crawl
PerformanceFaster first paint
Social SharingBetter meta tags
AccessibilityContent visible without JS

Server-Side Data

// Resolve server data
export const userResolver: ResolveFn<User> = (route) => {
  const http = inject(HttpClient);
  return http.get<User>(`/api/users/${route.params['id']}`);
};

Transfer State

// Transfer data from server to client
export const userResolver: ResolveFn<User> = (route, state) => {
  const transferState = inject(TransferState);
  const key = makeStateKey<User>('user');
  
  if (transferState.hasKey(key)) {
    return transferState.get(key, null)!;
  }
  
  return inject(HttpClient).get<User>(`/api/users/${route.params['id']}`)
    .pipe(tap(user => transferState.set(key, user)));
};

Mini Practice

  1. Enable SSR in a project
  2. Add data resolvers
  3. Use transfer state
  4. Test SSR output

Up Next

Continue with TypeScript — TypeScript in Angular.

Related Topics

Frequently Asked Questions about SSR

What is SSR in Angular?

SSR is a fundamental concept in Angular. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn SSR?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn SSR.

Why is SSR important in Angular?

SSR is essential for Angular development. Understanding this concept will help you write better code and solve real-world problems more effectively.