• Stars
    star
    125
  • Rank 285,544 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 6 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A command line tool to prerender Angular Apps.

logo

angular-prerender

A command line tool to prerender Angular Apps.

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 render each route and merges the output with 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. You can run it on the command line by only specifying the browser and server target.

npx angular-prerender --browser-target universe:build --server-target universe:server

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 --project universe
npm install angular-prerender --save-dev
ng build
ng run universe:server
npx angular-prerender --browser-target universe:build --server-target universe:server

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.

In case there is a prerendered page which links to all possible parameter combinations it might be an alternative to use the --recursive flag instead.

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

--recursive

When enabling this flag every prerendered HTML document will be scanned to discover further routes. If some of the found routes were previously unknown they get appended to the list of routes to prerender.

In case any of the newly discovered routes is one of the routes defined with --exclude-routes it will not be prerendered.

--scully-config

⚠️ This is currently very experimental and might not work with every possible Scully config. Please feel free to open an issue if it doesn't accept your config.

This option allows you to specify a path to a Scully config file. @scullyio/scully and the respective plugins need to be installed for this to work. So far only the plugins specified as defaultPostRenderers and plugins of type routeProcess get applied.

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

More Repositories

1

standardized-audio-context

A cross-browser wrapper for the Web Audio API which aims to closely follow the standard.
JavaScript
662
star
2

worker-timers

A replacement for setInterval() and setTimeout() which works in unfocused windows.
JavaScript
566
star
3

web-audio-beat-detector

A beat detection utility which is using the Web Audio API.
JavaScript
559
star
4

extendable-media-recorder

An extendable drop-in replacement for the native MediaRecorder.
JavaScript
253
star
5

midi-json-parser

This module is parsing midi files into a human-readable JSON object.
JavaScript
111
star
6

subscribable-things

A collection of reactive wrappers for various browser APIs.
JavaScript
42
star
7

json-midi-encoder

This module encodes a JSON representation of MIDI data into a binary MIDI file.
JavaScript
40
star
8

timing-object

An implementation of the timing object specification.
JavaScript
37
star
9

extendable-media-recorder-wav-encoder

A Wave file encoder for the extendable-media-recorder package.
JavaScript
36
star
10

timingsrc

A library to synchronize a MediaElement with a TimingObject.
JavaScript
31
star
11

timing-provider

An implementation of the timing provider specification.
JavaScript
30
star
12

midi-player

A MIDI player which sends MIDI messages to connected devices.
JavaScript
27
star
13

rxjs-broker

An RxJS message broker for WebRTC DataChannels and WebSockets.
JavaScript
24
star
14

dynamo-converters

A collection of converter functions to get good old JavaScript key/value objects into a DynamoDB friendly schema and back again.
JavaScript
24
star
15

recorder-audio-worklet

This module provides a loader for the RecorderAudioWorkletProcessor and the corresponding RecorderAudioWorkletNode.
JavaScript
24
star
16

angular-audio-context

An Angular wrapper for the Web Audio API's AudioContext.
JavaScript
21
star
17

standardized-audio-context-mock

A mocked version of the standardized-audio-context module.
JavaScript
20
star
18

limiter-audio-worklet

This module provides a loader for the LimiterAudioWorkletProcessor and the corresponding LimiterAudioWorkletNode.
JavaScript
17
star
19

audio-context-timers

A replacement for setInterval() and setTimeout() which works in unfocused windows.
JavaScript
15
star
20

timing-provider-server

A command line tool to spin up a server which can be used with the timing-provider.
JavaScript
15
star
21

video-synchronization-demo

A website to demo usage of the media-sync package with a TimingObject and a TimingProvider.
JavaScript
13
star
22

dynamo-db-local

A wrapper around Amazon's DynamoDB Local to start and stop it from Node.js.
JavaScript
13
star
23

automation-events

A module which provides an implementation of an automation event list.
JavaScript
12
star
24

audio-fingerprinting-file-reader

A reader for files created by audfprint.
JavaScript
11
star
25

web-audio-beat-detector-worker

The worker which is used by the web-audio-beat-detector package.
JavaScript
8
star
26

midi-file-slicer

This module is slicing a midi representation into parts.
JavaScript
7
star
27

web-codecs

A (not yet) extendable and (not yet) complete drop-in replacement for the native WebCodecs API.
JavaScript
7
star
28

limiter-audio-worklet-processor

The AudioWorkletProcessor which is used by the limiter-audio-worklet package.
JavaScript
7
star
29

recorder-audio-worklet-processor

The AudioWorkletProcessor which is used by the recorder-audio-worklet package.
JavaScript
6
star
30

metadata-detector

A tool to locate and strip metadata from files.
JavaScript
6
star
31

midi-json-parser-worker

The worker which is used by the midi-json-parser package.
TypeScript
6
star
32

karma-virtualbox-ie11-launcher

!!! DEPRECATED !!! A Karma launcher for Internet Explorer 11 on VirtualBox.
JavaScript
6
star
33

web-audio-beat-detector-broker

The broker which is used by the web-audio-beat-detector package.
JavaScript
5
star
34

worker-timers-broker

The broker which is used by the worker-timers package.
JavaScript
4
star
35

metadata-detector-streams

A tool to locate and strip metadata from files.
TypeScript
4
star
36

karma-virtualbox-edge-launcher

!!! DEPRECATED !!! A Karma launcher for Edge on VirtualBox.
JavaScript
4
star
37

standardized-audio-context-demo

A demo page to show how to use standardized-audio-context.
JavaScript
4
star
38

timed-audio-buffer-source-node-audio-worklet

This module provides a loader for the TimedAudioBufferSourceNodeAudioWorkletProcessor and the corresponding TimedAudioBufferSourceNodeAudioWorkletNode.
JavaScript
3
star
39

multi-buffer-data-view

A wrapper around the native DataView which can handle multiple ArrayBuffers.
JavaScript
3
star
40

demuxed-2022

My talk at Demuxed 2022.
HTML
3
star
41

worker-timers-worker

The worker which is used by the worker-timers package.
JavaScript
3
star
42

analog4all-client

This is the client for analog4all.
TypeScript
2
star
43

extendable-media-recorder-wav-encoder-worker

The worker which is used by the extendable-media-recorder-wav-encoder package.
JavaScript
2
star
44

web-timing-demo

A website to demo the Web Timing Object.
JavaScript
2
star
45

web-audio-conference-2017

My talk at the Web Audio Conference 2017.
TypeScript
2
star
46

media-encoder-host

This is a module to load and manage media encoders.
JavaScript
2
star
47

fast-unique-numbers

A module to create a set of unique numbers as fast as possible.
JavaScript
2
star
48

json-midi-message-encoder

This module encodes a JSON representation of a MIDI event into a binary MIDI event.
TypeScript
2
star
49

tonejs-synchronization-demo

A website to demo how to connect Tone.js to a Timing Object.
JavaScript
2
star
50

audio-developer-conference-2022

My talk at the Audio Developer Conference in 2022.
TypeScript
2
star
51

broker-factory

A little factory function to create a broker for a JSON-RPC based Web Worker.
JavaScript
2
star
52

analog4all-provider

This is the provider for analog4all.
TypeScript
2
star
53

web-audio-metronome-demo

A website to demo a Web Audio metronome connected to a Web Timing Object.
JavaScript
2
star
54

user-media-audio-visualizer

A super basic visualizer of user media's audio input.
JavaScript
2
star
55

here-maps-type-guards

A guarded version of the TypeScript type definitions for HERE Maps.
TypeScript
2
star
56

create-s3-object-write-stream

Creates a writable stream which uses Amazon's Multipart Upload API under the hood.
JavaScript
2
star
57

rxjs-connector

A module to accept WebRTC DataChannel connections by using WebSockets.
TypeScript
2
star
58

tpac-2022

My talk at the TPAC 2022.
TypeScript
1
star
59

karma-yakbak-preprocessor

!!! DEPRECATED !!! A Karma preprocessor for yakbak.
JavaScript
1
star
60

dynamo-db-provisioner

A lightweight and promise-based wrapper of the AWS SDK to create and delete tables.
JavaScript
1
star
61

worker-factory

A little factory function to create a JSON-RPC based Web Worker implementation.
JavaScript
1
star
62

synchsafe

A module to decode and encode synchsafe integers.
JavaScript
1
star
63

web-audio-meetup-march-2017

My talk at the Web Audio meetup in March 2017.
TypeScript
1
star
64

timed-audio-buffer-source-node-audio-worklet-processor

The AudioWorkletProcessor which is used by the timed-audio-buffer-source-node-audio-worklet package.
JavaScript
1
star
65

tsconfig-holy-grail

This is my personal collection of tsconfig files.
JavaScript
1
star
66

mse-tests

A collection of MSE tests.
JavaScript
1
star
67

web-audio-meetup-may-2019

My talk at the Web Audio meetup in May 2019.
TypeScript
1
star
68

metadata-detector-broker

The broker which is used by the metadata-detector package.
JavaScript
1
star
69

angular-consistent-contenteditable

AngularJS directive to overcome browser inconsisteny when using line breaks in contenteditable.
JavaScript
1
star
70

extendable-media-recorder-wav-encoder-broker

The broker which is used by the extendable-media-recorder-wav-encoder package.
JavaScript
1
star