aspnetcore-angular-universal icon indicating copy to clipboard operation
aspnetcore-angular-universal copied to clipboard

window is not defind

Open fadelguy opened this issue 6 years ago • 18 comments

Hi,

I'm using last template version. node: 8.9.4

I have add npm package "ng-lottie" for animation. And after that i see in html "window is not defind"

I know that ng-lottie not supported angualr ssr but i have to use it for animations.

I have tried to use domino in boot.server.ts but it dosen't work i still see the error message.

Can someone please help me?

fadelguy avatar Mar 27 '18 10:03 fadelguy

Where do you load ng-lottie? Try adding it to app.module.browser.ts

ruant avatar Mar 28 '18 10:03 ruant

I have loaded ng-lottie in app.module shared module for server and browser if i only loaded it to the browser module i see an error on the html that lottie tag not know.

fadelguy avatar Mar 28 '18 11:03 fadelguy

First: window, document, navigator, and other browser types - do not exist on the server - so using them, or any library that uses them (jQuery for example) will not work. as stated in the readme. So only load the ng-lottie in the app.module.browser.ts.

The tag not know, maybe you haven't imported/declared it correctly?

ruant avatar Mar 28 '18 11:03 ruant

I know that limitations. i have in my AppComponent selector <Spinner>. the SpinnerComponent used ng2-lottie for create the anumation for loader in our app.

If i dont add the ng-lottie on the server module i get an error on html that it dont know the

fadelguy avatar Mar 28 '18 12:03 fadelguy

Then I'm out of ideas. Looks like the component it self isn't suitable for use in a serverside rendered project...

ruant avatar Mar 28 '18 13:03 ruant

Can you show some of the code you used to try to populate globals['window'] with domino? @fadelguy

MarkPieszak avatar Mar 28 '18 20:03 MarkPieszak

I have tried this code:

const domino = require('domino'); const fs = require('fs'); const path = require('path'); const template = fs.readFileSync(path.join(__dirname, '.', 'dist', 'index.html')).toString(); const win = domino.createWindow(template); global['window'] = win; global['document'] = win.document;

But it doesn't work

fadelguy avatar Mar 28 '18 20:03 fadelguy

We don't have an index.html since we're using dotnet core, the code you posted would work for a regular Node/Universal setup. So just just create your own blank html string real quick, and add your app-root (or whatever you may have renamed it to) in there.

Something like this, should do the trick (if I'm not mistaken).

const template = `
  <html><head></head>
  <body>
  <app-root></app-root>
  </body>
  </html>
`;

MarkPieszak avatar Mar 28 '18 23:03 MarkPieszak

I tried your template but still now working. I see the error message in html "window is not defined"

I put domino at the top of boot.server.ts I do something wrong?

fadelguy avatar Mar 29 '18 05:03 fadelguy

Is it possible to load ng-lottie only inside the component in condition I'm in browser without loaded it on the module?

fadelguy avatar Mar 29 '18 08:03 fadelguy

that is what the module is for... there are 3 different module files. 1 for server specifics, 1 for client specifics, and 1 for shared things. If you place it only in the client one, that should work, but since we don't have any info on how your project is setup, we can't do much more than we already have.

Adondriel avatar Apr 11 '18 20:04 Adondriel

I am using Asp .net core 2.1 with angular 6.1.2 server side rendering. But my issue is that When ever i add Routing to my angular app it gives me error "window not defined" while rendering the page on server side. I have tried adding an empty component with routing and gives me the same error. while using the app without routing works perfectly with SSR. I am not able to figure out why this is happening? Error stack is :

Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: 'window is not defined ReferenceError: window is not defined at factory (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\common\bundles\common.umd.js:5610:169) at _callFactory (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:8457:24) at _createProviderInstance (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:8415:30) at resolveNgModuleDep (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:8390:21) at _callFactory (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:8463:71) at _createProviderInstance (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:8415:30) at resolveNgModuleDep (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:8375:25) at NgModuleRef_.get (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:9083:20) at RouterInitializer.bootstrapListener (C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\router\bundles\router.umd.js:5354:48) at C:\Users\asd\Desktop\YJewellery\src\Presentation\Nop.Web\ClientApp\node_modules\@angular\core\bundles\core.umd.js:4677:60'

rankitbishnoi avatar Aug 20 '18 08:08 rankitbishnoi

Please help with domino and aspnetcore

newprogrammer1991 avatar Feb 11 '19 20:02 newprogrammer1991

@fadelguy try to use dynamic component loader + importing the lib only in browser module, here is the link https://angular.io/guide/dynamic-component-loader

michael-vasyliv avatar Feb 15 '19 08:02 michael-vasyliv

thanks but what about domino?

newprogrammer1991 avatar Feb 15 '19 14:02 newprogrammer1991

const renderPromise = AppServerModuleNgFactory ? /* AoT / renderModuleFactory(AppServerModuleNgFactory, options) : / dev */ renderModule(AppServerModule, options); return renderPromise.then((response: any) => { console.log(response); const win = domino.createWindow(response.html); response.globals.window = win; return ({ html: response.html, globals: response.globals }); I tried this but does'nt work. Please help

newprogrammer1991 avatar Feb 16 '19 16:02 newprogrammer1991

any resolution for the issue related to including domino in asp.net core project ?

ex2-sdhawan avatar Mar 04 '19 10:03 ex2-sdhawan

We don't have an index.html since we're using dotnet core, the code you posted would work for a regular Node/Universal setup. So just just create your own blank html string real quick, and add your app-root (or whatever you may have renamed it to) in there.

Something like this, should do the trick (if I'm not mistaken).

const template = `
  <html><head></head>
  <body>
  <app-root></app-root>
  </body>
  </html>
`;

Please help

newprogrammer1991 avatar Apr 11 '19 21:04 newprogrammer1991