2023-10-20 13:34:44 +00:00
# Prerendering (SSG)
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
Prerendering, commonly referred to as Static Site Generation (SSG), represents the method by which pages are rendered to static HTML files during the build process.
2023-10-26 16:17:34 +00:00
Prerendering maintains the same performance benefits of [server-side rendering (SSR) ](/guide/ssr/#why-use-ssr ). But achieves a reduced Time to First Byte (TTFB), ultimately enhancing user experience. The key distinction lies in its approach that pages are served as static content, and there is no request-based rendering.
2023-10-20 13:34:44 +00:00
When the data necessary for server-side rendering remains consistent across all users, the strategy of prerendering emerges as a valuable alternative. Rather than dynamically rendering pages for each user request, prerendering takes a proactive approach by rendering them in advance.
2021-05-30 19:48:23 +00:00
## How to prerender a page
2023-10-20 13:34:44 +00:00
To prerender a static page, add SSR capabilities to your application with the following Angular CLI command:
2021-05-30 19:48:23 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< code-example format = "shell" language = "shell" >
2023-10-20 13:34:44 +00:00
ng add @ angular/ssr
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2021-05-30 19:48:23 +00:00
< / code-example >
2023-10-20 13:34:44 +00:00
< div class = "alert is-helpful" >
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
To create an application with prerendering capabilities from the beginning use the [ng new --ssr ](cli/new ) command.
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
< / div >
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
Once SSR is added, you can generate the static pages by running the build command:
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
< code-example format = "shell" language = "shell" >
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
ng build
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2023-10-20 13:34:44 +00:00
< / code-example >
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
### Build options for prerender
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
The application builder `prerender` option can be either a Boolean or an Object for more fine-tuned configuration.
When the option is `false` , no prerendering is done. When it's is `true` , all options use the default value. When it's is an Object, each option can be individually configured.
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2023-10-20 13:34:44 +00:00
| Options | Details | Default Value |
| :--------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :------------ |
| `discoverRoutes` | Whether the builder should process the Angular Router configuration to find all unparameterized routes and prerender them. | `true` |
| `routesFile` | The path to a file that contains a list of all routes to prerender, separated by newlines. This option is useful if you want to prerender routes with parameterized URLs. | |
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
< code-example format = "json" language = "json" >
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
…
"architect": {
"build": {
"builder": "@ angular-devkit/build-angular:application",
"options": {
"prerender": {
"discoverRoutes": false
},
},
…
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
< / code-example >
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
### Prerendering parameterized routes
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
You can prerender parameterized routes using the `routesFile` option. An example of a parameterized route is `product/:id` , where `id` is dynamically provided. To specify these routes, they should be listed in a text file, with each route on a separate line.
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
For an app with a large number of parameterized routes, consider generating this file using a script before running `ng build` .
2021-05-30 19:48:23 +00:00
< code-example language = "none" header = "routes.txt" >
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
/products/1
/products/555
2021-05-30 19:48:23 +00:00
< / code-example >
2023-10-20 13:34:44 +00:00
With routes specified in the `routes.txt` file, use the `routesFile` option to configure the builder to prerender the product routes.
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
< code-example format = "json" language = "json" >
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2023-10-20 13:34:44 +00:00
…
"architect": {
"build": {
"builder": "@ angular-devkit/build-angular:application",
"options": {
"prerender": {
"routesFile": "routes.txt"
},
},
…
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
2021-05-30 19:48:23 +00:00
< / code-example >
2023-10-20 13:34:44 +00:00
This configures `ng build` to prerender `/products/1` and `/products/555` at build time.
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
<!-- links -->
<!-- external links -->
<!-- end links -->
2021-05-30 19:48:23 +00:00
2023-10-20 13:34:44 +00:00
@reviewed 2023-10-23