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

Importing the library is causing a crash on SSR - non isomorphic #55

Closed
carlesnunez opened this issue Apr 22, 2020 · 14 comments
Closed

Comments

@carlesnunez
Copy link

The problem
As this library perform operations on import with CustomEvent api that only works on client importing this on files that are being used from server side is causing a crash.

@carlesnunez carlesnunez changed the title Importing the library is causing a crash on SSR Importing the library is causing a crash on SSR - non isomorphic Apr 22, 2020
@matepaiva
Copy link

Hello :) Are there any plans to fix this issue?

@jharrilim
Copy link
Collaborator

Yes, #61 should fix it (and fix other scenarios like incognito).

@matepaiva
Copy link

Thank you!

@jharrilim
Copy link
Collaborator

It turns out that #61 won't fix this. The main problem is that SSR tools like Gatsby currently don't support anything browser related on the server side. It is unreasonable to expect every browser module to polyfill their module for browser globals since browser code should be expected to run... in the browser.

I am currently working on a pre-rendering tool which solves this problem, because it is definitely possible to be done on the backend instead of trying to fix every browser module. You may see an example here:

https://github.com/jharrilim/rolledup/tree/master/examples/proj

The React code in that "proj" example emits the static HTML found in the "dist" folder.

@Hubro
Copy link

Hubro commented Sep 16, 2020

@jharrilim Can't you do a if (typeof window === "undefined") { ... } and make your hook a noop on the server side? Just return the default value and don't do anything more.

I'm a bit less sure about the CustomEvent, since you can't subclass a class that doesn't exist on the server side. Could you conditionally define the class?

This issue unfortunately makes this library impossible to use with Next.js, since it has SSR by default 😞

@carlesnunez
Copy link
Author

I remember that the problem came because the Custom Event was not being able to be extended conditionally so it was always failing because of the extend.

@Amantel
Copy link

Amantel commented Apr 5, 2021

Hey ho! Any luck with this?

@lorensr
Copy link

lorensr commented Apr 6, 2021

One possible solution here: #17 (comment)

@Amantel
Copy link

Amantel commented Apr 7, 2021

well... that is to dirty for me, TBH)

@yovanoc
Copy link

yovanoc commented May 2, 2021

You could use https://github.com/Comandeer/babel-plugin-banner to add typeof window !== "undefined" && in top of the file to build the local-storage lib

@jharrilim
Copy link
Collaborator

Hey ho! Any luck with this?

Sorry for the super late response; yes, I believe so. IIRC, The things that are needed to make this work are:

  1. CustomEvent needs to be conditionally defined (Will be done here Add CustomEvent polyfill #78)
  2. LocalStorage needs to be conditionally defined (Done here add storage class to handle gracefull degration #61)

Unfortunately with regards to the rolledup project, well... it was easy to prove that you could build a node environment pre-renderer that used browser globals, but creating an alternative to Gatsby takes a lot more work 😅

@emateu
Copy link
Contributor

emateu commented Sep 21, 2021

This might help: #80

@trevorblades
Copy link

Now that #80 is merged, when can we expect a new version to be published? @jharrilim

@iamsolankiamit
Copy link
Member

@trevorblades 2.4.4 version has the fix for SSR.

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

No branches or pull requests

10 participants