I am trying to implement Server side rendering in my Angular project using angular universal, for that I referred to the documentation but am quite not able to proceed further as on executing npm run dev:ssr
I am getting bundles generated but process fails complete execution due to an error which is
D:\CMS_clone1\CMS\CMSBIZ_admin_interface\dist\angular11-sample-app\server\main.js:100191
window.dataLayer = window.dataLayer || {};
^
ReferenceError: window is not defined
at Module.9vUh (D:\CMS_clone1\CMS\CMSBIZ_admin_interface\dist\angular11-sample-app\server\main.js:100191:1)
at __webpack_require__ (D:\CMS_clone1\CMS\CMSBIZ_admin_interface\dist\angular11-sample-app\server\main.js:26:30)
at Module.vY5A (D:\CMS_clone1\CMS\CMSBIZ_admin_interface\dist\angular11-sample-app\server\main.js:382815:78)
at __webpack_require__ (D:\CMS_clone1\CMS\CMSBIZ_admin_interface\dist\angular11-sample-app\server\main.js:26:30)
at Module.ZAI4 (D:\CMS_clone1\CMS\CMSBIZ_admin_interface\dist\angular11-sample-app\server\main.js:236347:78)
at __webpack_require__ (D:\CMS_clone1\CMS\CMSBIZ_admin_interface\dist\angular11-sample-app\server\main.js:26:30)
at Module.24aS (D:\CMS_clone1\CMS\CMSBIZ_admin_interface\dist\angular11-sample-app\server\main.js:51247:69)
at __webpack_require__ (D:\CMS_clone1\CMS\CMSBIZ_admin_interface\dist\angular11-sample-app\server\main.js:26:30)
at Module.K011 (D:\CMS_clone1\CMS\CMSBIZ_admin_interface\dist\angular11-sample-app\server\main.js:174790:80)
at __webpack_require__ (D:\CMS_clone1\CMS\CMSBIZ_admin_interface\dist\angular11-sample-app\server\main.js:26:30)
A server error has occurred.
node exited with 1 code.
connect ECONNREFUSED 127.0.0.1:59092
I am stuck at this for a couple of days now, I tried a few suggestions related to domino, mockBrowser but neither seemed to help, If anyone can possibly help me out to resolve this issue of mine it would be a lot helpful. Thanking in advance.
I had the same issue a few weeks back. SSR doesn't have any idea as to what a window or document is, so I added the following into server.ts: