Angular 17 预渲染在更新后不起作用

问题描述 投票:0回答:1

Angular 16 使用

@nguniversal/builders:prerender
作为预渲染的构建器,工作正常,升级到 Angular 17 ng 更新后,将此构建器更改为
@angular-devkit/build-angular:prerender
,运行
ng run <project-name>:prerender
后,会出现一条警告,提示
An unhandled exception occurred: Configuration 'production' is not set in the workspace.
我无法修复此问题错误,但在此错误后它继续运行并写入
✔ Browser application bundle generation complete.
✔ Copying assets complete.
✔ Index html generation complete.
但没有生成预渲染页面。这是我的 angular.json 文件:

    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "version": 1,
    "newProjectRoot": "projects",
    "projects": {
        "karlancer": {
            "projectType": "application",
            "schematics": {},
            "root": "",
            "sourceRoot": "src",
            "prefix": "app",
            "architect": {
                "build": {
                    "builder": "@angular-devkit/build-angular:browser",
                    "options": {
                      "outputPath": "dist/karlancer",
                      "allowedCommonJsDependencies": [
                        "rxjs-compat",
                        "rxjs",
                        "hammerjs"
                      ],
                      "index": "src/index.html",
                      "main": "src/main.ts",
                      "polyfills": "src/polyfills.ts",
                      "tsConfig": "tsconfig.app.json",
                      "aot": true,
                      "assets": [
                        "src/assets",
                        "src/robots.txt"
                      ],
                      "styles": [
                        "src/styles.css",
                        "node_modules/bootstrap/dist/css/bootstrap.min.css"
                      ],
                      "scripts": [
                        "./node_modules/jquery/dist/jquery.js",
                        "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"
                      ]
                    },
                    "configurations": {
                        "production": {
                            "fileReplacements": [{
                                "replace": "src/environments/environment.ts",
                                "with": "src/environments/environment.prod.ts"
                            }],
                            "optimization": true,
                            "outputHashing": "all",
                            "sourceMap": false,
                            "namedChunks": false,
                            "extractLicenses": true,
                            "budgets": [{
                                    "type": "initial",
                                    "maximumWarning": "2mb",
                                    "maximumError": "6mb"
                                },
                                {
                                    "type": "anyComponentStyle",
                                    "maximumWarning": "6kb",
                                    "maximumError": "20kb"
                                }
                            ]
                        },
                        "development": {
                            "buildOptimizer": false,
                            "optimization": false,
                            "vendorChunk": true,
                            "extractLicenses": false,
                            "sourceMap": true,
                            "namedChunks": true
                          }
                    }
                },
                "serve": {
                    "builder": "@angular-devkit/build-angular:dev-server",
                    "options": {
                      "buildTarget": "karlancer:build"
                    },
                    "configurations": {
                        "production": {
                          "buildTarget": "karlancer:build:production"
                        },
                        "development": {
                          "buildTarget": "karlancer:build:development"
                        }
                    }
                },
                "extract-i18n": {
                    "builder": "@angular-devkit/build-angular:extract-i18n",
                    "options": {
                      "buildTarget": "karlancer:build"
                    }
                },
                "test": {
                    "builder": "@angular-devkit/build-angular:karma",
                    "options": {
                        "main": "src/test.ts",
                        "polyfills": "src/polyfills.ts",
                        "tsConfig": "tsconfig.spec.json",
                        "karmaConfig": "karma.conf.js",
                        "assets": [
                            "src/assets",
                            "src/robots.txt"
                        ],
                        "styles": [
                            "src/styles.css"
                        ],
                        "scripts": []
                    }
                },
                "e2e": {
                    "builder": "@angular-devkit/build-angular:protractor",
                    "options": {
                        "protractorConfig": "e2e/protractor.conf.js",
                        "devServerTarget": "karlancer:serve"
                    },
                    "configurations": {
                        "production": {
                            "devServerTarget": "karlancer:serve:production"
                        }
                    }
                },
                "server": {
                    "builder": "@angular-devkit/build-angular:server",
                    "options": {
                        "outputPath": "dist/karlancer/server",
                        "main": "server.ts",
                        "tsConfig": "tsconfig.server.json",
                        "optimization": false,
                        "buildOptimizer": false
                    }
                },
                "serve-ssr": {
                    "builder": "@angular-devkit/build-angular:ssr-dev-server",
                    "options": {
                      "browserTarget": "karlancer:build",
                      "serverTarget": "karlancer:server"
                    },
                    "configurations": {
                      "production": {
                        "browserTarget": "karlancer:build:production",
                        "serverTarget": "karlancer:server:production"
                      }
                    }
                },
                "prerender": {
                    "builder": "@angular-devkit/build-angular:prerender",
                    "options": {
                        "browserTarget": "karlancer:build:production",
                        "serverTarget": "karlancer:server:production",
                        "routes": [
                            "/"
                          ]
                    },
                    "configurations": {
                        "production": {
                            "browserTarget": "karlancer:build:production",
                            "serverTarget": "karlancer:server:production"
                        }
                    },
                    "defaultConfiguration": "production"
                }
            }
        }
    },
    "cli": {
        "analytics": false
    }
}

我发现构建器已从 Angular 17 更改为Angular 文档

中所述

可以使用

ng update @angular/cli --name use-application-builder
命令进行更新,该命令将构建器更改为应用程序,在这种情况下,预渲染是此构建器中使用的一个选项。当您将该选项设置为 true 或在构建后在其中声明路由时,会出现此错误代码
Error: NG0908
它已在其他资源中被告知,这是由于
polyfills.js
main.js
之后加载,但这些加载顺序超出了我的控制范围。 我需要在 Angular 17 上运行预渲染,但我在 github 上找不到示例或任何有效的东西。

angular server-side-rendering angular-universal prerender
1个回答
1
投票

将项目升级到 Angular 17 后,我在预渲染方面遇到了同样的错误。我在 github 上查看了这个工作示例,其中预渲染与 Angular 17 和应用程序构建器一起使用:https://github.com/ganatan/angular-ssr

Error: NG0908
的实际原因是当您在
src/polyfills.ts
“polyfills”选项中指定
Angular.json
文件时。在示例中,
'zone.js'
直接在 Angular.json 的“polyfills”选项中声明。

要解决该问题,请在 Angular.json 文件中更改:

"polyfills": "src/polyfills.ts"

对此:

"polyfills": ["zone.js"]

我还从

src/polyfills.ts
“文件”列表中删除了
tsconfig.app.json

© www.soinside.com 2019 - 2024. All rights reserved.