角度通用中的服务器端渲染未显示fav图标

时间:2018-09-18 14:19:43

标签: angular angular-universal ssr

我有一个角度应用程序,我想使其成为通用的角度应用程序。

使我的应用程序对SEO更友好的原因。

遵循https://angular.io/guide/universal中描述的步骤,我什至在https://github.com/mappedinn/how-to-create-angular-univeral中重现了示例。

这几乎是成功的。唯一的问题是通过以下命令运行应用程序时,浏览器未在选项卡中显示favicon.ico

npm run build:ssr
npm run serve:ssr

请参阅下面的图片FAVICON.ICO NOT DISPLAYED

但是,如果通过命令ng serve运行应用程序,则该图标将可用。

请参考以下图片: FAVICON.ICO DISPLAYED

那么,您能帮忙解决这个问题吗?


更新1

由于文件夹src/assets/不存在。它已创建,现在包括文件favicon.ico

构建SSR是正确的(命令npm run build:ssr)。

为SSR提供服务会产生错误(命令npm run serve:ssr

> angular-io-example@1.0.0 serve:ssr /home/amine/docker-projects/how-convert-to-universal
> node dist/server

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::4000
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at Server.setupListenHandle [as _listen2] (net.js:1367:14)
    at listenInCluster (net.js:1408:12)
    at Server.listen (net.js:1492:7)
    at Function.listen (/home/amine/docker-projects/how-convert-to-universal/dist/server.js:44577:24)
    at Object.<anonymous> (/home/amine/docker-projects/how-convert-to-universal/dist/server.js:116:5)
    at __webpack_require__ (/home/amine/docker-projects/how-convert-to-universal/dist/server.js:20:30)
    at Object.defineProperty.value (/home/amine/docker-projects/how-convert-to-universal/dist/server.js:69:18)
    at Object.<anonymous> (/home/amine/docker-projects/how-convert-to-universal/dist/server.js:72:10)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! angular-io-example@1.0.0 serve:ssr: `node dist/server`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the angular-io-example@1.0.0 serve:ssr script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/amine/.npm/_logs/2018-09-18T16_21_55_407Z-debug.log

更新2

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/local/bin/npm', 'run', 'serve:ssr' ]
2 info using npm@6.4.1
3 info using node@v8.10.0
4 verbose run-script [ 'preserve:ssr', 'serve:ssr', 'postserve:ssr' ]
5 info lifecycle angular-io-example@1.0.0~preserve:ssr: angular-io-example@1.0.0
6 info lifecycle angular-io-example@1.0.0~serve:ssr: angular-io-example@1.0.0
7 verbose lifecycle angular-io-example@1.0.0~serve:ssr: unsafe-perm in lifecycle true
8 verbose lifecycle angular-io-example@1.0.0~serve:ssr: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/amine/docker-projects/how-convert-to-universal/node_modules/.bin:/usr/local/apache/spark-2.3.1-bin-hadoop2.7:/home/amine/anaconda3/bin:/home/amine/google-cloud-sdk/bin:/usr/local/apache/spark-2.3.1-bin-hadoop2.7:/home/amine/anaconda3/bin:/home/amine/google-cloud-sdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/apache/hadoop-2.9.0/bin:/usr/local/apache/hadoop-2.9.0/sbin:/usr/local/apache/hadoop-2.9.0/bin:/usr/local/apache/hadoop-2.9.0/sbin
9 verbose lifecycle angular-io-example@1.0.0~serve:ssr: CWD: /home/amine/docker-projects/how-convert-to-universal
10 silly lifecycle angular-io-example@1.0.0~serve:ssr: Args: [ '-c', 'node dist/server' ]
11 silly lifecycle angular-io-example@1.0.0~serve:ssr: Returned: code: 1  signal: null
12 info lifecycle angular-io-example@1.0.0~serve:ssr: Failed to exec serve:ssr script
13 verbose stack Error: angular-io-example@1.0.0 serve:ssr: `node dist/server`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:301:16)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at EventEmitter.emit (events.js:214:7)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at ChildProcess.emit (events.js:214:7)
13 verbose stack     at maybeClose (internal/child_process.js:925:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid angular-io-example@1.0.0
15 verbose cwd /home/amine/docker-projects/how-convert-to-universal
16 verbose Linux 4.15.0-34-generic
17 verbose argv "/usr/bin/node" "/usr/local/bin/npm" "run" "serve:ssr"
18 verbose node v8.10.0
19 verbose npm  v6.4.1
20 error code ELIFECYCLE
21 error errno 1
22 error angular-io-example@1.0.0 serve:ssr: `node dist/server`
22 error Exit status 1
23 error Failed at the angular-io-example@1.0.0 serve:ssr script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

2 个答案:

答案 0 :(得分:0)

确保在您的angular.json文件中将其包含在资产部分下

"assets": [
    // add any images paths here, or add to your assets folder and put folder path here
]

答案 1 :(得分:0)

我为此https://medium.com/@maciejtreder/angular-server-side-rendering-with-ng-toolkit-universal-c08479ca688

找到了不错的文章

简单的ng add @ng-toolkit/universal会将应用程序转换为通用程序。

额外:ng add @ng-toolkit/serverless用于服务器端呈现