Miss any of our Open RFC calls?Watch the recordings here! »

angular-prerender

6.0.1 • Public • Published

logo

angular-prerender

A command line tool to prerender Angular Apps.

tests dependencies version

This command line tool is meant to simplify the build process of static Angular apps. It works by analyzing the config file created by the Angular CLI. It looks for a client and server app target defined in the angular.json file. It does then execute the server side rendering for each route and merges the output into the static build for the client.

Usage

angular-prerender is available on npm. It will most likely be a dev dependency of your project. The command to install it will then look like this:

npm install angular-prerender --save-dev

In case you used all the default settings of the CLI angular-prerender will be able to pick up all the necessary information on its own and can be executed by simply calling it on the command line.

npx angular-prerender

It is also possible to skip the explicit installation of angular-prerender.

The following is a complete example which will generate a very basic static Angular app called "universe".

npx @angular/cli new universe --routing
cd universe
ng generate universal --client-project universe
npm install angular-prerender --save-dev
ng build
ng run universe:server
npx angular-prerender

Arguments

In some scenarios angular-prerender will not be able to grab all the information by analyzing the angular.json file alone. In that case you can help it by specifying some command line arguments.

--browser-target

This lets you specify the name of the target of your client app. The Angular CLI will normally call it "build" and this is also used as a default value. It is also possible to use a full target specifier which does also include the project and an optional configuration separated by colons. This works similar as the target parameter of the ng run command.

--config

The config option expects a path (including the filename) to the angular.json file of your project. By default it will look for it in the current working directory.

--exclude-routes

This option can be used to tell angular-prerender not to render specified routes. The given routes can't contain any parameters.

npx angular-prerender --exclude-routes /do-not-render-1 /do-not-render-2

Alternatively routes can also be excluded when setting the status code as described below.

--ignore-status-code

When set to false this flag will make sure that status codes set on the response will not be ignored. An example of a component which sets the status code looks as follows:

import { Component, Inject } from '@angular/core';
import { RESPONSE } from '@nguniversal/express-engine/tokens';
import { Response } from 'express';
 
@Component({
    // ...
})
class NotFoundComponent {
 
    constructor(@Inject(RESPONSE) response: Response) {
        response.status(404);
    }
 
}

If status codes are not ignored any route which sets the status code to 300 or above will be excluded.

--include-routes

This option can be used to tell angular-prerender to explicitly render the given routes even though they could not be detected automatically.

npx angular-prerender --include-routes /render-even-if-not-detected

--parameter-values

Some URLs of your app might accept parameters. This option can be used to tell angular-prerender about the possible values those parameters could have. It expects a stringified JSON value which can be described with this TypeScript interface:

interface IParameterValuesMap {
 
    [ segment: string ]: string | string[] | IParameterValuesMap | IParameterValuesMap[];
 
}

Lets imagine your app has a route with a :name parameter in it: /team/:name. A call to angular-prerender like this would render two routes by replacing the parameter with the given values:

npx angular-prerender --parameter-values '{":name":["amelia","oliver"]}'
/team/amelia
/team/oliver

By default all possible combinations of all given parameter values will be rendered. If there is a route like this /blog/:slug/comments/:id and we render it with two values for each parameter angular-prerender will render four routes.

npx angular-prerender --parameter-values '{":id":["comment-a","comment-b"],":slug":["story-a","story-b"]}'
/blog/story-a/comments/comment-a
/blog/story-a/comments/comment-b
/blog/story-b/comments/comment-a
/blog/story-b/comments/comment-b

If this is not intended and comment-a exclusively belongs to story-a and comment-b belongs to story-b respectively the parameter values can be grouped.

npx angular-prerender --parameter-values '[{":id":"comment-a",":slug":"story-a"},{":id":"comment-b",":slug":"story-b"}]'

In this case angular-prerender will only renderer two routes.

/blog/story-a/comments/comment-a
/blog/story-b/comments/comment-b

It's also possible to scope parameter values by routes. This comes in handy if the same name is used for different parameters in different routes. If your app has two routes (/shirts/:id/:size and /shoes/:id/:size) and they both use the same parameters (:id and :size) it is possible to nest the parameter values to specify a different set of values for each route.

npx angular-prerender --parameter-values '{"/shirts":[{":id":"polo-shirt",":size":"m"},{":id":"t-shirt",":size":"xl"}],"/shoes":{":id":"slipper",":size":["10","12"]}}'

It is not necessary to specify the full route as long as a part of the route is already enough to distinguish it from the other routes.

The command above will render two routes.

/shirts/polo-shirt/m
/shirts/t-shirt/xl
/shoes/slipper/10
/shoes/slipper/12

Please note that it might be necessary to escape the string differently dependending on the command-line interface you use.

--preserve-index-html

Setting this flag to true will preserve the index.html file generated by the browser build. It will be saved in the same directory as start.html. Additionally an existing ngsw.json file will be updated as well to reference the preserved start.html file instead of the prerendered index.html file.

--server-target

This lets you specify the name of the target of your server app. The Angular CLI will normally call it "server" and this is also used as a default value. It is also possible to use a full target specifier which does also include the project and an optional configuration separated by colons. This works similar as the target parameter of the ng run command.

--verbose (-v)

This flag enables more detailed log messages.

Acknowledgement

This command line tool is only possible by bringing together the great power of Angular Universal (which is now on its way into the main Angular repository) and Guess.js (which provides an excellent parser to retrieve the routes of an Angular app).

Install

npm i angular-prerender

DownloadsWeekly Downloads

429

Version

6.0.1

License

MIT

Unpacked Size

133 kB

Total Files

203

Last publish

Collaborators

  • avatar