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

window is not defined #287

Closed
nescha opened this issue Feb 24, 2020 · 21 comments · Fixed by #301
Closed

window is not defined #287

nescha opened this issue Feb 24, 2020 · 21 comments · Fixed by #301

Comments

@nescha
Copy link

nescha commented Feb 24, 2020

An error happens during build after recent update to @brainhubeu/react-carousel@1.12.6:
ReferenceError: window is not defined at Object.<anonymous> ([ROOT]/react/node_modules/ brainhubeu/react-carousel/lib/react-carousel.js:1:279

@piotr-s-brainhub
Copy link
Contributor

@nescha

do you use server-side rendering?

@piotr-s-brainhub
Copy link
Contributor

@slmr

do you use server-side rendering?

@Neddz
Copy link

Neddz commented Feb 25, 2020

I started having the same issue with this library. I use Gatsby and can confirm it no longer works with SSR.

@piotr-s-brainhub piotr-s-brainhub self-assigned this Feb 25, 2020
@Sorvereign
Copy link

Sorvereign commented Feb 26, 2020

Hi, I was having the same issue with NextJS, I tried to check the source code but I didn't find anything weird so I find a solution for this. What I did was dynamic importing with no SSR

So, instead of the traditional importing:
import Carousel from '@brainhubeu/react-carousel'

Do:

//This first
import dynamic from 'next/dynamic';

const Carousel = dynamic(
() => import ('@brainhubeu/react-carousel'),
{
ssr: false
}
)

This works for me, have a nice day.

@piotr-s-brainhub
Copy link
Contributor

@nescha @Neddz @Sorvereign

I have a workaround #292
I published it as @brainhubeu/react-carousel@1.12.9-alpha.
Could you check whether it works for you?
Do you think my workaround is better or using next?

@jannispaul
Copy link

I have the same issue (using Gatsby) and your workaround works for me. Thanks!

@nescha
Copy link
Author

nescha commented Feb 27, 2020

@nescha @Neddz @Sorvereign

I have a workaround #292
I published it as @brainhubeu/react-carousel@1.12.9-alpha.
Could you check whether it works for you?
Do you think my workaround is better or using next?

It seems it works for me.
Yes, I do use NextJS and SSR

@semy
Copy link

semy commented Feb 27, 2020

Hey @piotr-s-brainhub, for me, this workaround unfortunately does not work.

I use NextJS 9.2.3 with SSR.

@piotr-s-brainhub
Copy link
Contributor

@semy

But SSR with @brainhubeu/react-carousel and NextJS works for you?
With my workaround (@brainhubeu/react-carousel@1.12.9-alpha), what issues do you encounter?

@semy
Copy link

semy commented Feb 27, 2020

Okay, for some reason it works now. However, the component ignores the width of the outer element :-/ Is there any solution?

@piotr-s-brainhub
Copy link
Contributor

@semy

do you encounter the same problem without SSR or only with SSR?

@piotr-s-brainhub
Copy link
Contributor

@xvvvyz
Copy link

xvvvyz commented Mar 11, 2020

For those using Gatsby, here's a horrible & gross hack to work around this issue:

// gatsby-node.js

module.exports.onPreInit = () => {
  const carouselSrcFile =
    'node_modules/@brainhubeu/react-carousel/lib/react-carousel.js';

  if (!fs.existsSync(carouselSrcFile)) return;

  fs.writeFileSync(
    carouselSrcFile,
    fs
      .readFileSync(carouselSrcFile, 'utf8')
      .replace('(window,', `(typeof window==='undefined'?{}:window,`)
  );
};

@piotr-s-brainhub
Copy link
Contributor

@cadejscroggins

Does this hack really work?
Isn't it a problem that in the browser window isn't an empty object?

@xvvvyz
Copy link

xvvvyz commented Mar 11, 2020

@piotr-s-brainhub it works. When rendered in a browser, window is available and is passed to the IIFE as normal since it's not undefined.

@piotr-s-brainhub
Copy link
Contributor

@cadejscroggins

so does the @brainhubeu/react-carousel@1.12.9-alpha work for you as well?
if yes, we can consider, including this workaround in each version

@xvvvyz
Copy link

xvvvyz commented Mar 11, 2020

@piotr-s-brainhub it does. Including it in future releases would be very helpful.

@microbit-matt-hillsdon
Copy link
Contributor

See possible fix via webpack config to reinstate webpack 3 behaviour on #344.

@tramax
Copy link

tramax commented May 11, 2021

If you use Dots and have problem with undefined component using the { default: Carousel, Dots } import syntax mentioned in the readme, try importing like this:

import dynamic from 'next/dynamic';

const Carousel = dynamic(
  () => import('@brainhubeu/react-carousel'),
  { ssr: false },
);

const Dots = dynamic(
  () => import('@brainhubeu/react-carousel').then(module => module.Dots),
  { ssr: false },
);

@tysbrown
Copy link

tysbrown commented Jun 21, 2021

Was having this issue only when deploying to Netlify, everything ran fine on my local Gatsby dev server.

@piotr-s-brainhub Your patch worked great! Thanks for the fix.

EDIT: I spoke too soon, now for some reason the plugins aren't working? Specifically the slidesToShowPlugin and arrowsPlugin.

LATEST EDIT: Rather than using the older version/patch, Gatsby has a solution for these kinds of SSR errors using this package and using it to import the component (in this case, the carousel component):

const Carousel = loadable(() => import("./Carousel"));

This finally solved the issue for me!

@lorraineS
Copy link

Was having this issue only when deploying to Netlify, everything ran fine on my local Gatsby dev server.

@piotr-s-brainhub Your patch worked great! Thanks for the fix.

EDIT: I spoke too soon, now for some reason the plugins aren't working? Specifically the slidesToShowPlugin and arrowsPlugin.

LATEST EDIT: Rather than using the older version/patch, Gatsby has a solution for these kinds of SSR errors using this package and using it to import the component (in this case, the carousel component):

const Carousel = loadable(() => import("./Carousel"));

This finally solved the issue for me!

How did you make the plugins work (slidesToShowPlugin, autoplayPlugin, etc ...) ?

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

Successfully merging a pull request may close this issue.