Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

angular5 - Server Side Rendering Angular 4/5 Issue

I am working on angular 5. I am done with my app and created a build and deployed on the server and it works fine.
But I have issue with Search Engine Optimization that pages are not indexed in google. I used Server Side Rendering and have the below error:

F:universal-demo-v5>node dist/server.js F:universal-demo-v5distserver.js:111436 })(window, document, 'Hammer');    ^ ReferenceError: window is not defined
    at Object.defineProperty.value (F:universal-demo-v5distserver.js:111436:4)
    at __webpack_require__ (F:universal-demo-v5distserver.js:20:30)
    at Object.hammerjs (F:universal-demo-v5distserver.js:139493:18)
    at __webpack_require__ (F:universal-demo-v5distserver.js:128177:30)
    at Object.../../../../ngx-carousel/src/ngx-carousel/ngx-carousel.component.js (F:universal-demo-v5distserver.js:138668:67)
    at __webpack_require__ (F:universal-demo-v5distserver.js:128177:30)
    at Object.../../../../ngx-carousel/src/ngx-carousel/ngx-carousel.component.ngfactory.js (F:universal-demo-v5distserver.js:139254:10)
    at __webpack_require__ (F:universal-demo-v5distserver.js:128177:30)
    at Object.../../../../../src/app/components/home/home.component.ngfactory.js (F:universal-demo-v5distserver.js:129216:11)
    at __webpack_require__ (F:universal-demo-v5distserver.js:128177:30)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is very late but maybe it will help you or someone in the future. This happens when using Angular Universal, its because the server side rendering doesnt have a 'window' to use, only the browser does.

So when this code is executed the server fails to find the non-existent window. You can fix this by either removing hammerjs (it isnt essential, just adds quality of life to material 2 and similar UI)

Or you can try this fix here: https://github.com/angular/universal/issues/830

Which involves defining the window in your server.ts code so when it runs it works correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...