Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

angular-prerender

4.3.3 • Public • Published

logo

angular-prerender

An experimental command line tool to prerender an Angular App.

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.

--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[];
 
}

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

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

--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

586

Version

4.3.3

License

MIT

Unpacked Size

73.4 kB

Total Files

126

Last publish

Collaborators

  • avatar