react-slick icon indicating copy to clipboard operation
react-slick copied to clipboard

Does this work with server side rendering? no 'window' or 'document' dependencies?

Open YarivGilad opened this issue 10 years ago • 30 comments

I've been trying out too many carousel react components that assume they are running on the client and have 'window' and 'document' dependencies all over the place. This breaks the code trying to integrate it on an isomorphic app...

YarivGilad avatar Apr 02 '16 15:04 YarivGilad

Nope, it doesn't, also depends on mounted DOM for measurement.

jonathanglasmeyer avatar Apr 03 '16 16:04 jonathanglasmeyer

@Ajar-Ajar did you ever find a solution for isomorphic rendering?

MitchWeiss avatar Jun 07 '16 02:06 MitchWeiss

Isomorphic / universal React web-apps are becoming the standard To avoid a ReferenceError: window is not defined I'd suggest to wrap all dependencies to window with

var canUseDOM = !!(
  typeof window !== 'undefined' &&
  window.document &&
  window.document.createElement
)
if(canUseDOM ){
     // use window dependency...
}else{
  //have an alternative that wouldn't break functionality...
}

YarivGilad avatar Jun 07 '16 10:06 YarivGilad

This should be fixed in [email protected]

akiran avatar Sep 01 '16 17:09 akiran

I'm having the same problem with a universal setup. The carousel mostly works when I use via stylesheet tags, but on first load (server-side render) there are visual bugs with the slider that disappear by clicking through links (client-side render).

When I try with the npm package instead of stylesheet tags it doesn't work at all.

Ganasist avatar Sep 12 '16 06:09 Ganasist

Ran into this issue as well. Current solution is as @Ganasist mentioned, if working with a universal setup, it will only work properly if you include both the stylesheet and the npm package.

ypyang237 avatar Nov 10 '16 00:11 ypyang237

@ypyang237 I'm having this issue as well. When you say "include both the stylesheet and the npm package", do you mean you are including the CDN for slick-carousel, while using npm for react-slick? Or are you importing the slick-carousel stylesheets to a separate stylesheet? None of the css is being applied on my isomorphic app.

Thanks in advance!

alexanthony813-zz avatar Jan 19 '17 22:01 alexanthony813-zz

@alexanthony813
Yes, you understood correctly. I included the two CDN stylesheets for slick-carousel and had react-slick installed with npm as well to get it working. Hope this helps you!

ypyang237 avatar Jan 19 '17 23:01 ypyang237

I have react-slick working with server-side rendering, however I get the following error when using certain responsive settings. I believe it has to do with features only available in my "responsive" breakpoint (i.e. dots):

Settings:

const settings = {
  dots: false,
  draggable: false,
  fade: true,
  infinite: true,
  swipe: false,
  responsive: [
    {
      breakpoint: 767,
      settings: {
        dots: true,
        draggable: true,
        fade: false,
        swipe: true,
      },
    },
  ],
};

Error:

Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:

Not sure if anything can be done about this, since the server-side rendered component doesn't know the browser width and the dots will always render client-side, but maybe there's a way to make this error disappear?

coreyleelarson avatar Feb 27 '17 20:02 coreyleelarson

I'm having the same issue that @coreyleelarson. It even shows the difference between server and client rendering: (client) "><div class="slick-arrow slick-prev" (server) "><div class="slick-list" data-reactid="

paulinhapenedo avatar Apr 06 '17 03:04 paulinhapenedo

This enquire.js require breaks again server rendering: https://github.com/akiran/react-slick/commit/b798b1fbd0aa50c73b3c0a2b506b7a27a3f40f31#diff-e8eaa42a1b8ab4981d9272be425c03dfR8

node_modules/enquire.js/src/MediaQueryDispatch.js:14
    if(!window.matchMedia) {
        ^

ReferenceError: window is not defined
    at new MediaQueryDispatch (node_modules/enquire.js/src/MediaQueryDispatch.js:14:9)
    at Object.<anonymous> (node_modules/enquire.js/src/index.js:2:18)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (node_modules/react-slick/lib/slider.js:25:16)

paradoxxxzero avatar Apr 19 '17 15:04 paradoxxxzero

getting the same error as @paradoxxxzero

idangozlan avatar Apr 19 '17 18:04 idangozlan

having same error as @paradoxxxzero too hard downgrading react-slick to 0.14.6 solved this issue. I hate to downgrade like this but hopefully it gets resolved soon.

jdmswong avatar Apr 19 '17 18:04 jdmswong

Same error as @paradoxxxzero within a universal app has popped up in the last 48 hours using v0.14.5.

rtmalone avatar Apr 20 '17 14:04 rtmalone

@jdmswong @rtmalone Can you try 0.14.11

akiran avatar Apr 20 '17 15:04 akiran

0.14.11 seems to fix the server rendering :+1:

paradoxxxzero avatar Apr 24 '17 09:04 paradoxxxzero

@paradoxxxzero @akiran indeed. upgrading to 0.14.11 solved the issue. thanks, all.

rtmalone avatar Apr 24 '17 13:04 rtmalone

I am using the latest build 0.15.0 but still while rendering on server side getting visual issues.Included the stylesheets too as mentioned in above comments but still not able to work out.Anyone has any working examples?

priteshpoddar avatar Jan 18 '18 05:01 priteshpoddar

All the issues that are related to server-side rendering, are supposed to be handled in the third release (maybe the third week) from now.

laveesingh avatar Feb 01 '18 07:02 laveesingh

@laveesingh Wanted to check in on this. I have been trying to find a way around this (can't just only render it Client side as we need the HTML for SEO), and am wondering if I should just wait for the official solution or keep plugging away.

mradambeck avatar Feb 21 '18 22:02 mradambeck