nrkno / core-components

Accessible and lightweight Javascript components
https://static.nrk.no/core-components/latest/
MIT License
117 stars 10 forks source link

Core suggest throws error when rendering on server and window is already shimmed #660

Closed janerikbr closed 1 year ago

janerikbr commented 2 years ago

Description

On tv.nrk.no, we render most of our markup server side. When attempting to render an instance of core suggest on the server, we get the following error:

fatal error ReferenceError [Error]: navigator is not defined

This is due to the following lines of code:

var IS_BROWSER = typeof window !== 'undefined';
IS_BROWSER && /(android)/i.test(navigator.userAgent); // Bad, but needed

IS_BROWSER && /iPad|iPhone|iPod/.test(String(navigator.platform));
var IS_IE11 = IS_BROWSER && window.msCrypto; // msCrypto only exists in IE11

... which in turn is caused by the fact that we already have a global object named window on the server to get server side rendering of web components to work. This is the shim we use: https://github.com/popeindustries/lit/blob/main/packages/lit-html-server/src/dom-shim.js

If I remove the above-mentioned lines of code, the problem persists, all due to the fact that window is present.

I'm not entirely sure how to deal with this issue – and maybe it's even not a core-suggest thing, but I'd love some feedback and maybe we could solve this together. It would be great if we could use core-suggest on the new search page on tv.nrk.no, still utilising server side rendering. Perhaps the IS_BROWSER test could test for more than just the presence of window?

skjalgepalg commented 1 year ago

Closing this as fixed under #663