Pre-rendering creates static HTML pages before users visit. This makes pages load faster and helps search engines find your content easily.
Pre-rendering static pages in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Angular
ng add @nguniversal/express-engine
// Then build static pages
npm run prerenderUse Angular Universal to enable pre-rendering in Angular projects.
The command 'npm run prerender' generates static HTML files for your routes.
Examples
Angular
ng add @nguniversal/express-engine
Angular
npm run prerender
Sample Program
This simple Angular component shows static content. When pre-rendered, the HTML is generated ahead of time, so users see content immediately without waiting for JavaScript.
Angular
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: `<h1>Welcome to Pre-rendered Angular!</h1><p>This page is static and loads fast.</p>` }) export class AppComponent {} // In angular.json, configure prerender target with routes // Run 'npm run prerender' to generate static HTML files
Important Notes
Pre-rendering works best for pages that do not need frequent updates.
Use Angular Universal to enable pre-rendering and server-side rendering.
Check generated static files in the 'dist/browser' folder after prerendering.
Summary
Pre-rendering creates static HTML pages before users visit.
This improves load speed and SEO for your Angular app.
Use Angular Universal and the 'npm run prerender' command to generate static pages.
Practice
1. What is the main benefit of pre-rendering static pages in an Angular application?
easy
Solution
Step 1: Understand what pre-rendering does
Pre-rendering generates static HTML pages ahead of time, before users visit the site.Step 2: Identify benefits of static HTML
Static pages load faster and improve SEO because search engines can easily read content.Final Answer:
Improves page load speed and SEO by generating static HTML before user visits -> Option AQuick Check:
Pre-rendering = faster load + better SEO [OK]
Hint: Pre-rendering means static pages ready before visit [OK]
Common Mistakes:
- Confusing pre-rendering with client-side dynamic rendering
- Thinking pre-rendering updates content in real-time
- Assuming pre-rendering fetches data on every request
2. Which command is used to generate static pages with Angular Universal for pre-rendering?
easy
Solution
Step 1: Recall Angular Universal pre-render command
The standard command to generate static pages isnpm run prerender.Step 2: Eliminate incorrect commands
ng build --prodbuilds the app but does not pre-render;ng serve --staticandnpm start prerenderare invalid commands.Final Answer:
npm run prerender -> Option CQuick Check:
Pre-render command = npm run prerender [OK]
Hint: Use npm run prerender to generate static pages [OK]
Common Mistakes:
- Using ng build instead of prerender
- Confusing serve commands with prerender
- Typing npm start prerender which is invalid
3. Given this Angular pre-render setup snippet, what will be the output folder after running
npm run prerender?
"prerender": "ng run my-app:prerender"
medium
Solution
Step 1: Understand Angular Universal output folder
By default, Angular Universal outputs pre-rendered static pages intodist/my-app/prerendered.Step 2: Differentiate output folders
dist/my-app/browseris for client build,dist/my-app/serveris for server bundle, anddist/my-app/staticis not standard.Final Answer:
dist/my-app/prerendered -> Option DQuick Check:
Pre-render output folder = dist/my-app/prerendered [OK]
Hint: Pre-render output is in dist/my-app/prerendered folder [OK]
Common Mistakes:
- Confusing browser build folder with prerender output
- Assuming server folder contains static pages
- Guessing non-standard folder names
4. You run
npm run prerender but get an error: "Cannot find module '@angular/platform-server'". What is the likely fix?medium
Solution
Step 1: Identify missing module error cause
The error means the Angular Universal server package is missing from node_modules.Step 2: Fix by installing missing package
Runnpm install @angular/platform-serverto add the required package.Final Answer:
Install @angular/platform-server package using npm -> Option BQuick Check:
Missing module error = install package [OK]
Hint: Missing module? Install it with npm install [OK]
Common Mistakes:
- Changing prerender script instead of fixing dependencies
- Removing Angular Universal unnecessarily
- Running npm install without specifying package
5. You want to pre-render multiple routes in your Angular app. Which configuration in
angular.json helps specify routes for pre-rendering?hard
Solution
Step 1: Identify prerender routes configuration
Angular Universal uses aroutesarray inside prerender options to list paths to pre-render.Step 2: Differentiate other options
lazyModulesis unrelated to prerender routes,serverTargetdefines server build, andoutputPathsets build output folder.Final Answer:
"routes": ["/home", "/about", "/contact"] inside the prerender options -> Option AQuick Check:
Pre-render routes = routes array in prerender options [OK]
Hint: List routes in prerender options to pre-render multiple pages [OK]
Common Mistakes:
- Using lazyModules instead of routes for prerender
- Confusing serverTarget with prerender routes
- Changing outputPath instead of routes
