我已经为我的项目实现了SSR并将其部署到了heroku。现在,当我在heroku logs --tail
中启动应用程序时,我会运行很多东西并且会显着降低性能。大多数事情都是错误的:
错误ReferenceError:未定义文档
即使我使用这样的结构:
if (isPlatformBrowser(this.platformId)) {
const overflow = this.isMenuOpened ? 'hidden' : 'auto;';
this.renderer.setStyle(this.document.body, 'overflow', overflow);
}
每次重新加载页面时,这个东西都会重新开始运行。
在我的package.json
文件中,我运行以下命令:
"scripts": {
"ng": "ng",
"start": "node dist/server.js",
"build": "npm run build:server:prod && npm run build:browser:prod",
"build:server:prod": "ng run oilEnergy:server && webpack --config webpack.server.config.js --progress --colors",
"build:browser:prod": "ng build --prod"
}
因此,当部署Heroku时,它会构建服务器和浏览器文件夹并编译server.js文件,然后该文件负责运行我的应用程序。
一段时间后,我的应用程序崩溃,heroku显示Application error
而不是我的应用程序。
我不知道,为什么会这样。也许以前有人遇到过这个问题?
尝试与document
instead this.document
以下代码对我有用
import { PLATFORM_ID, Inject } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
constructor(
@Inject(PLATFORM_ID) private platformId: Object) {
if (isPlatformBrowser(this.platformId)) {
this.bodyTag = document.getElementsByTagName('body')[0];
this.htmlTag = document.getElementsByTagName('html')[0];
}
}