How can I control which routes Angular decides to Prerender?

1.4k Views Asked by At

I'm trying to prerender my angular app using, ng run [myAppName]:prerender. I only want to prerender the content of / as the other routes don't need to be prerendered. Although it finds a way to prerender other routes that are not included in my angular.json config.

Angular.json

    "prerender": {
      "builder": "@nguniversal/builders:prerender",
      "options": {
        "browserTarget": "[myApp]:build:production",
        "serverTarget": "[myApp]:server:production",
        "routes": [
          "/" // <--- One route listed...
        ]
      },
      "configurations": {
        "production": {}
      }
    }

With the config above it will still prerender 16 other routes, (e.g. /my-other-route)

Is there a way I can only prerender the / page?

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.901.10
@angular-devkit/build-angular     0.901.10
@angular-devkit/build-optimizer   0.901.10
@angular-devkit/build-webpack     0.901.10
@angular-devkit/core              9.1.10
@angular-devkit/schematics        9.1.10
@angular/cdk                      9.2.4
@angular/cli                      9.1.10
@angular/fire                     6.0.2
@angular/google-maps              10.0.2
@angular/material                 9.2.4
@angular/platform-server          9.1.12
@ngtools/webpack                  9.1.10
@nguniversal/builders             9.1.1
@nguniversal/common               9.1.1
@nguniversal/express-engine       9.1.1
@schematics/angular               9.1.10
@schematics/update                0.901.10
rxjs                              6.5.5
typescript                        3.8.3
webpack                           4.42.0
1

There are 1 best solutions below

1
On

Try updating to >= Angular 11.1 and adding "guessRoutes": false to the angular.json.

...
"routes": [
    "/"
],
"guessRoutes": false

See: https://github.com/angular/universal/blob/3e0efbeb2ad490caead49d53809af97ace6e03a7/modules/builders/src/prerender/schema.json#L30:L34