Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working in SSR enviroments #52

Open
oskari opened this issue Aug 24, 2023 · 3 comments
Open

Not working in SSR enviroments #52

oskari opened this issue Aug 24, 2023 · 3 comments

Comments

@oskari
Copy link

oskari commented Aug 24, 2023

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
Copy link
Collaborator

@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
Copy link
Author

oskari commented Aug 25, 2023

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants