docs(platform-server): added commands for universal and add prerendering info (#36005)

Fixes #36004

PR Close #36005
This commit is contained in:
Santosh Yadav 2021-05-31 01:18:23 +05:30 committed by Jessica Janiuk
parent f3fde4bbb5
commit 4975a106dc
4 changed files with 109 additions and 10 deletions

View File

@ -560,6 +560,7 @@ groups:
- >
contains_any_globs(files, [
'packages/platform-server/**',
'aio/content/guide/prerendering.md',
'aio/content/guide/universal.md',
'aio/content/examples/universal/**'
])

View File

@ -0,0 +1,69 @@
# Prerendering static pages
Angular Universal allows you to prerender the pages of your application. Prerendering is the process where a dynamic page is processed at build time generating static HTML.
## How to prerender a page
To prerender a static page make sure to add SSR capabilities to your application.
For more information see the [universal guide](guide/universal).
Once SSR is added, run the following command:
<code-example language="sh">
npm run prerender
</code-example>
### Build options for prerendering
When you add prerendering to your application, the following build options are available:
* `browserTarget`: Specify the target to build.
* `serverTarget`: Specify the Server target to use for prerendering the application.
* `routes`: Define an array of additional routes to prerender.
* `guessRoutes`: Whether builder should extract routes and guess which paths to render. Defaults to `true`.
* `routesFile`: Specify a file that contains a list of all routes to prerender, separated by newlines. This option is useful if you have a large number of routes.
* `numProcesses`: Specify the number of CPUs to be used while running the prerendering command.
### Prerendering dynamic routes
You can prerender dynamic routes. An example of a dynamic route is `product/:id`, where `id` is dynamically provided.
To prerender dynamic routes, choose one from the following options:
* Provide additional routes in the command line
* Provide routes using a file
* Prerender specific routes
#### Provide additional routes in the command line
While running the prerender command, you can provide additional routes. For example:
<code-example language="sh">
ng run &lt;app-name&gt;:prerender --routes /product/1 /product/2
</code-example>
#### Providing additonal routes using a file
You can provide routes using a file to generate static pages. This method is useful if you have a large number of routes to generate, such as product details for an e-commerce application, which may come from an external source (Database or CMS).
To provide routes using a file, use the `--routes-file` option with the name of a `.txt` file containing the routes.
For example, you could generate this file by using a script to extract IDs from a database and save them to a `routes.txt` file:
<code-example language="none" header="routes.txt">
/products/1
/products/555
</code-example>
When your `.txt` file is ready, run the following command to prerender the static files with dynamic values:
<code-example language="sh">
ng run &lt;app-name&gt;:prerender --routes-file routes.txt
</code-example>
#### Prerendering specific routes
You can also pass specific routes to the prerender command. If you choose this option, make sure to disable the `guessRoutes` option.
<code-example language="sh">
ng run &lt;app-name&gt;:prerender --no-guess-routes --routes /product/1 /product/1
</code-example>

View File

@ -290,3 +290,27 @@ Here, "request URL" refers to the URL of the request as a response to which the
For example, if the client requested `https://my-server.com/dashboard` and you are rendering the application on the server to respond to that request, `options.url` should be set to `https://my-server.com/dashboard`.
Now, on every HTTP request made as part of rendering the application on the server, Angular can correctly resolve the request URL to an absolute URL, using the provided `options.url`.
### Useful scripts
- `npm run dev:ssr`
This command is similar to [`ng serve`](cli/serve), which offers live reload during development, but uses server-side rendering. The application will run in watch mode and refresh the browser after every change.
This command is slower than the actual `ng serve` command.
- `ng build && ng run app-name:server`
This command builds both the server script and the application in production mode.
Use this command when you want to build the project for deployment.
- `npm run serve:ssr`
This command starts the server script for serving the application locally with server-side rendering.
It uses the build artifacts created by `ng run build:ssr`, so make sure you have run that command as well.
Note that `serve:ssr` is not intended to be used to serve your application in production, but only for testing the server-side rendered application locally.
- `npm run prerender`
This script can be used to prerender an application's pages.
You can read more about prerendering [here](guide/prerendering).

View File

@ -446,6 +446,21 @@
"tooltip": "Configuring service worker caching behavior."
}
]
},
{
"url": "guide/web-worker",
"title": "Web Workers",
"tooltip": "Using web workers for background processing."
},
{
"url": "guide/universal",
"title": "Server-side Rendering",
"tooltip": "Render HTML server-side with Angular Universal."
},
{
"url": "guide/prerendering",
"title": "Prerendering",
"tooltip": "Prerender HTML server-side with Angular Universal."
}
]
},
@ -536,11 +551,6 @@
"title": "CLI Builders",
"tooltip": "Using builders to customize Angular CLI."
},
{
"url": "guide/universal",
"title": "Server-side Rendering",
"tooltip": "Render HTML server-side with Angular Universal."
},
{
"url": "guide/language-service",
"title": "Language Service",
@ -629,11 +639,6 @@
"title": "Building a Template-driven Form",
"tooltip": "Create a template-driven form using directives and Angular template syntax."
},
{
"url": "guide/web-worker",
"title": "Web Workers",
"tooltip": "Using web workers for background processing."
},
{
"title": "Angular Libraries",
"tooltip": "Extending Angular with shared libraries.",