AddSearch / js-client-library

JavaScript client library for AddSearch REST API
https://www.addsearch.com/
MIT License
3 stars 1 forks source link

Not working in SSR enviroments #52

Open oskari opened 1 year ago

oskari commented 1 year ago

In cookie.js#L10 a document -object is accessed but this might not be available in while in SSR, should probably be a check for if the document -object exists.

Facing this issue in Gatsby but I presume platforms like nextjs suffer the same?

haoAddsearch commented 1 year ago

@oskari null check was added in the latest release https://github.com/AddSearch/js-client-library/releases/tag/v0.8.6 Could you check again please?

oskari commented 1 year ago

I'm sorry, my initial description was written a bit too hastily. Null check isn't sufficient as the whole document object is not available.

ERROR  UNKNOWN

There was an error compiling the html.js component for the development server.
See our docs page on debugging HTML builds for help https://gatsby.dev/debug-html ReferenceError: document is not defined

  10 |
  11 | const getCookie = function(cookieName) {
> 12 |   if (!document) {
     | ^
  13 |     return;
  14 |   }
  15 |   let name = cookieName + "=";

  WebpackError: ReferenceError: document is not defined

  - cookie.js:12
    [www.mehilainen.fi]/[addsearch-js-client]/src/cookie.js:12:1

  - index.js:20
    [www.mehilainen.fi]/[addsearch-js-client]/src/index.js:20:1

I'd guess a better way around it would be typeof check, as seems to be the case in multiple js cookie libraries:

if (typeof document === 'undefined') {
    return
  }

https://github.com/js-cookie/js-cookie/blob/a427812597527fd82f9747fe075c2f65b93c3112/src/api.mjs#L6

haoAddsearch commented 1 year ago

Thank you for the PR @oskari I published a new fixed version 0.8.7