omridevk / ng-keyboard-shortcuts

Dead Simple Keyboard Shortcuts Management for Angular

Home Page:https://ng-keyboard-shortcuts.vercel.app/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with navigator and Angular Universal

lkurylo opened this issue · comments

I'm trying to set up an Angular Universal and I have a problem with call to navigator in keys.ts:

node .\dist\server.js
D:\repo\Budget\SRC\Trunk\Budget.Api\UI\dist\server.js:300301
var isMac = (navigator.userAgent.indexOf('Mac OS') !== -1);

ReferenceError: navigator is not defined

I tried using domino and MockBrowser as a workaround but with no result.

const MockBrowser = require('mock-browser').mocks.MockBrowser;
    const mock = new MockBrowser();


    global['navigator'] = mock.getNavigator();
commented

cool use case, however I have never tested this library with Angular Universal, I assume it might have some compatibility issue since I do use some browser API and not Angular adapters to achieve it.
I think supporting it might not be straight forward as it is.
Especially since my limited knowledge of Angular Universal.
I will try to look into it regardless.
Thanks a lot for reporting the issue.
I assume I need to test the environment and if we are running in universal, need to create a mock navigator object, or not run the user agents checks at all.

commented

@lkurylo
Please provide a minimum example in which I can reproduce so I can check various solutions.
Thanks a lot.
will re-open if still relevant.

Hi @omridevk

I faced the same issue described here.
I created a super simple repository for you to test the problem: https://github.com/miguelchico/basic-angular-cli-universal

Just install dependencies and then:

  • Execute ng serve for a non-ssr execution (all works fine)
  • Execute npm run dev:ssr. It will try to start a development server to serve the app (it is ok if you use the dev server, the small issue is the same)

This is the first output error.
image

I modified the line with issues in node_modules to skip the error and see if there were something else and I found another one:

image

You can find a bit more information about SSR in the official guide.

I think you might try to use a similar approach than here to solve both issues.

Please let me know if you need anything else to check it.

commented

@miguelchico
Thank you so much for providing this information
I will look into it ASAP and hopefully can provide a solution that will work for both platforms.
Thanks again.

@omridevk
I just created a quick fix for this. Would you mind to check it?
Thanks.

commented

will clone your repo and test it out. thanks a lot for creating it.

commented

thanks to @miguelchico
will be fixed in next release.

commented

closing.

commented

@miguelchico
I have modified some of your code, I hope it is okay :)
Can you please let me know if the issue persist in the latest version (I have added to fix to v7,v8 and v9) to make sure it works with all the Angular LTS versions.

@omridevk
Tested with version 10.1.12 and all good from my side.
Thank you!