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

Support server side rendering #32

Closed
piersolenski opened this issue Nov 28, 2018 · 7 comments
Closed

Support server side rendering #32

piersolenski opened this issue Nov 28, 2018 · 7 comments

Comments

@piersolenski
Copy link

Getting a requestAnimationFrame is not defined error in non-browser environments, such as when Server Side Rendered or during a Static Site build.

@piersolenski
Copy link
Author

I imagine window and document might need to be checked for as well...

@stratboy
Copy link

as far as I know, requestAnimationFrame is not available in node.

@electerious
Copy link
Owner

electerious commented Nov 29, 2018

Is there a way to only run basicScroll on the client? I guess this would be better than adding window, document and requestAnimationFrame checks as basicScroll is made for the client. But I get your point. Just thinking about what would be best.

@bdrtsky
Copy link

bdrtsky commented Nov 29, 2018

@superfunkminister you can do this check by yourself. In Nuxt we have process.browser for that.

But it's worth to mention that even with this check I had issues with basicScroll with SSR, which I did not had with other animation libs. If interested I can reproduce this.

@electerious
Copy link
Owner

It's because basicScroll starts as soon as you include it: https://github.com/electerious/basicScroll/blob/master/src/scripts/main.js#L452

When we implement a window and document check, basicScroll should at least output a warning so it's not just doing nothing silently.

@piersolenski
Copy link
Author

Yep, my current way around the problem is to use this kind of pattern in React:

componentDidMount() {
  let basicScroll;
  if (typeof window !== 'undefined') {
    basicScroll = require('basicscroll');
    // Do stuff with basicScroll
  }
}

Obviously this is a very React specific example, and there's a bunch of other ways around it depending on your stack... But the easiest fix to stop this kind of issue reoccurring, and as a developer constantly having to implement a workaround, is to make the library itself isomorphic. For example, https://github.com/akiran/react-slick (also made for the client) handles the problem in the following way https://github.com/akiran/react-slick/blob/48c93ed36cfb58991e6bf4d34a1a588094281d12/src/utils/innerSliderUtils.js#L821

@electerious electerious changed the title requestAnimationFrame is not defined Support server side rendering Dec 20, 2018
@electerious
Copy link
Owner

electerious commented Dec 20, 2018

v3.0.1 includes a window check and should work fine when executed in a non-browser environment: https://github.com/electerious/basicScroll/blob/master/CHANGELOG.md#301---2018-12-20

Please let me know when it isn't work as expected.

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

4 participants