- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试按照官方 Angular guide for angular universal 将服务器端渲染 (SSR) 添加到我的 Angular 应用程序中.我能够设置快速服务器并且我从服务器获得的响应包含我的应用程序,虽然看起来有点古怪,好像缺少一些 CSS 导致 FontAwesome图标被渲染太大等。
但是由于某种原因,客户端/浏览器永远不会接管(重新)渲染,并且网站与来自服务器的响应保持不变。它不是交互式的,完全是静态的。单击控件(链接除外)或交互元素时,没有任何 react 。
在这里您可以看到 的部分内容App Shell / HTML .
这里是 Network Traffic .
包.json :
{
"name": "my-app",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"dev:ssr": "ng run my-app:serve-ssr",
"serve:ssr": "node dist/my-app/server/main.js",
"build:ssr": "ng build --prod && ng run my-app:server:production",
"prerender": "ng run my-app:prerender"
},
"private": true,
"dependencies": {
"@agm/core": "^1.1.0",
"@angular/animations": "^10.0.14",
"@angular/common": "^10.0.14",
"@angular/compiler": "^10.0.14",
"@angular/core": "^10.0.14",
"@angular/elements": "^10.0.14",
"@angular/forms": "^10.0.14",
"@angular/localize": "~10.0.14",
"@angular/platform-browser": "^10.0.14",
"@angular/platform-browser-dynamic": "^10.0.14",
"@angular/platform-server": "^10.0.14",
"@angular/router": "^10.0.14",
"@angular/service-worker": "^10.0.14",
"@fortawesome/angular-fontawesome": "^0.7.0",
"@fortawesome/fontawesome-svg-core": "^1.2.30",
"@fortawesome/free-brands-svg-icons": "^5.14.0",
"@fortawesome/free-regular-svg-icons": "^5.14.0",
"@fortawesome/free-solid-svg-icons": "^5.14.0",
"@ng-bootstrap/ng-bootstrap": "^7.0.0",
"@ng-select/ng-select": "^4.0.4",
"@nguniversal/express-engine": "^10.0.2",
"@webcomponents/webcomponentsjs": "^2.2.10",
"bootstrap": "^4.3.1",
"bufferutil": "^4.0.1",
"core-js": "^2.5.4",
"document-register-element": "^1.13.2",
"domino": "^2.1.6",
"express": "^4.15.2",
"html-webpack-plugin": "^2.30.1",
"intersection-observer": "^0.7.0",
"jquery": "^3.5.1",
"ng-lazyload-image": "^5.1.2",
"ng5-slider": "^1.1.14",
"ngx-cookie-service": "^2.2.0",
"ngx-infinite-scroll": "^7.1.0",
"popper.js": "^1.14.7",
"rxjs": "^6.6.2",
"tslib": "^2.0.0",
"utf-8-validate": "^5.0.2",
"webpack": "^4.44.1",
"xmlhttprequest": "^1.8.0",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1000.8",
"@angular/cli": "^10.0.8",
"@angular/compiler-cli": "^10.0.14",
"@angular/language-service": "^10.0.14",
"@nguniversal/builders": "^10.0.2",
"@types/express": "^4.17.0",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.2",
"karma-jasmine": "~3.3.0",
"karma-jasmine-html-reporter": "^1.5.0",
"ngx-perfect-scrollbar": "^7.2.1",
"node-sass": "^4.14.1",
"protractor": "~7.0.0",
"ts-node": "~7.0.0",
"tslint": "~6.1.0",
"typescript": "^3.9.7"
}
}
Angular .json :
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"my-app": {
"i18n": {
"locales": {
"de-AT": {
"translation": "src/locale/messages.de-AT.xlf",
"baseHref": ""
}
}
},
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"watch": false,
"index": "src/index.html",
"outputPath": "dist/my-app/browser",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/apple-touch-icon.png",
"src/apple-touch-icon-precomposed.png",
"src/robots.txt",
"src/sitemap.xml",
"src/assets",
"src/manifest.json"
],
"styles": [
"src/styles.scss"
],
"scripts": [
{
"input": "node_modules/document-register-element/build/document-register-element.js"
}
]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"i18nMissingTranslation": "error",
"localize": [
"de-AT"
],
"optimization": true,
"outputHashing": "bundles",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "3mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "20kb"
}
],
"serviceWorker": true,
"ngswConfigPath": "src/ngsw-config.json"
},
"dev": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.dev.ts"
}
],
"aot": false,
"i18nMissingTranslation": "error",
"localize": [
"de-AT"
],
"optimization": false,
"outputHashing": "none",
"extractCss": true,
"namedChunks": true,
"extractLicenses": true,
"vendorChunk": false,
"budgets": [
{
"type": "initial",
"maximumWarning": "10mb",
"maximumError": "12mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "20kb"
}
],
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "my-app:build"
},
"configurations": {
"production": {
"browserTarget": "my-app:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "my-app:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.scss"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/robots.txt",
"src/sitemap.xml",
"src/assets",
"src/manifest.json"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json",
"src/tsconfig.server.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"server": {
"builder": "@angular-devkit/build-angular:server",
"options": {
"outputPath": "dist/my-app/server",
"main": "server.ts",
"tsConfig": "src/tsconfig.server.json"
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"sourceMap": false,
"optimization": true
}
}
},
"serve-ssr": {
"builder": "@nguniversal/builders:ssr-dev-server",
"options": {
"browserTarget": "my-app:build",
"serverTarget": "my-app:server"
},
"configurations": {
"production": {
"browserTarget": "my-app:build:production",
"serverTarget": "my-app:server:production"
}
}
},
"prerender": {
"builder": "@nguniversal/builders:prerender",
"options": {
"browserTarget": "my-app:build:production",
"serverTarget": "my-app:server:production",
"routes": [
"/"
]
},
"configurations": {
"production": {}
}
}
}
},
"my-app-e2e": {
"root": "e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "my-app:serve"
},
"configurations": {
"production": {
"devServerTarget": "my-app:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "my-app"
}
server.ts :
import 'zone.js/dist/zone-node';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';
const domino = require('domino');
const fs = require('fs');
const path = require('path');
const template = fs
.readFileSync(path.join('dist/my-app/browser/de-AT', 'index.html'))
.toString();
const window = domino.createWindow(template);
// Ignite UI browser objects abstractions
(global as any).window = window;
(global as any).document = window.document;
(global as any).Event = window.Event;
(global as any).KeyboardEvent = window.KeyboardEvent;
(global as any).MouseEvent = window.MouseEvent;
(global as any).FocusEvent = window.FocusEvent;
(global as any).PointerEvent = window.PointerEvent;
(global as any).HTMLElement = window.HTMLElement;
(global as any).HTMLElement.prototype.getBoundingClientRect = () => {
return {
left: '',
right: '',
top: '',
bottom: ''
};
};
(global as any).XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
// Other optional depending on your application configuration
(global as any).object = window.object;
(global as any).navigator = window.navigator;
(global as any).localStorage = window.localStorage;
(global as any).DOMTokenList = window.DOMTokenList;
Object.defineProperty(window.document.body.style, 'transform', {
value: () => {
return {
enumerable: true,
configurable: true,
};
},
});
(global as any).CSS = null;
(global as any).Prism = null;
import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';
// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
const server = express();
const distFolder = join(process.cwd(), 'dist/my-app/browser/de-AT');
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
server.engine('html', ngExpressEngine({
bootstrap: AppServerModule,
}));
server.set('view engine', 'html');
server.set('views', distFolder);
// Example Express Rest API endpoints
// server.get('/api/**', (req, res) => { });
// Serve static files from /browser
server.get('*.*', express.static(distFolder, {
maxAge: '1y'
}));
// All regular routes use the Universal engine
server.get('*', (req, res) => {
res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
});
return server;
}
function run(): void {
const port = process.env.PORT || 4000;
// Start up the Node server
const server = app();
server.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
run();
}
export * from './src/main.server';
tsconfig.base.json :
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2020",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"downlevelIteration": true,
"typeRoots": [
"node_modules/@types",
"typings.d.ts"
],
"lib": [
"es2018",
"dom"
]
}
}
tsconfig.server.json :
{
"extends": "./tsconfig.app.json",
"compilerOptions": {
"outDir": "../out-tsc/server",
"target": "es2016",
"types": [
"node"
]
},
"files": [
"main.server.ts",
"../server.ts"
],
"angularCompilerOptions": {
"entryModule": "./app/app.server.module#AppServerModule"
}
}
app.server.module.ts :
import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { UniversalInterceptor } from './universal-interceptor';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
@NgModule({
imports: [
AppModule,
ServerModule
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: UniversalInterceptor,
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppServerModule {}
我真的不知道我做错了什么。有任何想法吗?
最佳答案
经过长时间的调查,我找到了罪魁祸首。似乎在添加 Angular Universal 时,CLI 包装了 DOMContentLoaded
事件监听器围绕另一个 DOMContentLoaded
引导应用程序 中的事件监听器main.ts 文件。这就是它的样子:
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
document.addEventListener('DOMContentLoaded', () => { // Remove this
document.addEventListener('DOMContentLoaded', () => {
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
});
}); // Remove this
因此,删除第二个事件监听器使应用程序再次启动。
关于javascript - Angular Universal 不会接管客户端的渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63781342/
请帮助我了解如何在 Angular Universal 中使用服务器渲染。 我做了什么。我访问了 Angular Universal 官方网站。设置 Node.js。下载推荐项目 Angular 2
我正在研究使用 explicit universes 的可能性用于在 Coq 中构建固定的 Universe 层次结构。在构建它时使用常量 (2, 3, 4) 的尝试失败了:最后,所有组合仍然类型检查
我查看了 Universal Analytics,但没有找到问题的正确答案。 如何将这一行从旧的 Analytics 更新到新的 Universal Analytics? _gaq.push(["_s
我在 IIS 中部署 angular 通用应用程序时遇到问题。在 angular 通用中,创建了两个 dist 文件夹,一个是客户端的 dist,另一个是 dist-server,当我尝试托管时,我提
我使用@ng-toolkit/universal 作为我的服务器端渲染方法。一切正常,没有我的图像。我正在动态地获取它们。因此,当用户没有头像时,会显示一个占位符图像。因此我正在使用这个函数: ver
-编译应用程序后,我多次收到此错误 enter image description here 文本错误: 错误错误 at XMLHttpRequest.send (C:\Users\seva-
我正在Win10中使用VS2015开发通用应用程序。模拟器和android模拟器运行正常。 但是,当我启动Windows Phone Mobile模拟器时,该模拟器运行并显示“操作系统正在启动”,然后
我正在实现一个路由保护(CanActivate 接口(interface)),我需要在某些条件下重定向到未找到的页面。这可以通过以下语句来实现: if (isNode){ let res : Resp
我偶然发现了一个奇怪的情况,其中有 reflect.runtime.universe._进口原因reflect.runtime.universe.RuntimeClass推断它似乎在哪里Nothing
每当我在我的应用程序中加载图像时,我的 logcat 都会给我这条消息。 04-09 19:09:59.241: W/ImageLoader(276): Try to initialize Image
我正在尝试添加包但出现错误 meteor add universe:carousel => Errors while adding packages: 选择软件包版本时: error: Confli
所以我一直在尝试将我的应用程序转换为 angular universal,并且在大多数情况下都很好。但我之前读过一些“陷阱”:https://github.com/onespeed-articles/
我有一个指令,使文本输入到谷歌的地方自动完成输入,看起来像: import { Directive, AfterViewInit , Output, EventEmitter, NgZone, Ele
大约一年前,我们升级到了通用Analytics(分析),由于升级我们的站点搜索并没有在我们的Analytics(分析)帐户中显示关键字,甚至没有跟踪其使用次数。 作为营销人员,这是一个问题,因为我想知
我正准备使用 Angular Universal 为我的 Angular 7 应用程序设置 SSR。我遵循了官方文档( https://angular.io/guide/universal )。我到了
在应用程序关闭(暂停)后,我正在将一个不大于 10KB 的数据文件写入 RoamingFolder。该应用程序在开发桌面和 Surface 2 上运行,两者都登录到同一个 Microsoft 帐户。但
This question already has answers here: Page Navigation using MVVM in Store App (5个答案) 6年前关闭。 使用MVVM
我尝试安装iOS-Universal-Framework 。尝试运行instalation shell screept从这个存储库并始终获取消息: iOS Real Static Framework
这就是我目前初始化 chromedriver 的方式 System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe"); 但是
您好,我在尝试运行 perl 脚本时遇到以下错误: pc:~/Phd/lenovo/programs/vep/scripts/variant_effect_predictor$ perl varian
我是一名优秀的程序员,十分优秀!