davidjerleke / embla-carousel

A lightweight carousel library with fluid motion and great swipe precision.
https://www.embla-carousel.com
MIT License
5.38k stars 165 forks source link

Init event not fired in SSR (React / NextJS) #689

Closed StevenVerbiest closed 5 months ago

StevenVerbiest commented 5 months ago

Bug is related to

Embla Carousel version

Describe the bug

The .on("init") event is not invoked when used in a useEffect in a SSR component.

CodeSandbox

https://stackblitz.com/edit/stackblitz-starters-syez5k?file=app%2FEmblaComponent.tsx https://stackblitzstarterssyez5k-c0ir--3000--f7aa08df.local-credentialless.webcontainer.io/

const [emblaRef, emblaApi] = useEmblaCarousel();

useEffect(() => {
  if (!emblaApi) return;

  emblaApi.on('init', () => {
    console.log('init');
  });
}, [emblaApi]);

Steps to reproduce

  1. Open the Stackblitz webcontainer
  2. Open the console
  3. There is no 'init' log

The log is shown after a hot reload. If you see the init log, do a hard refresh.

Expected behavior

The init event should always fire.

davidjerleke commented 5 months ago

@StevenVerbiest I can't access the link you shared. Would you mind double checking it?

Best, David

StevenVerbiest commented 5 months ago

@davidjerleke I can't get the direct link to work for some reason, but do you have access to the source files? If so, you could open the project in a new tab (available on the top right of the page) and connect the project then (if needed).

davidjerleke commented 5 months ago

@StevenVerbiest thanks for pointing me to the source files, that worked. I believe you should read this to better understand why it's happening:

Best, David

StevenVerbiest commented 5 months ago

Thanks for the info. I'm also having some other issues related to SRR regarding to special layouts, but I won't go into detail since it's unrelated to this issue. I like Embla, but not supporting SSR is an issue for me, so I will sadly need to look for an alternative. Will definitely try Embla for my CSR projects and wil try to keep an eye for SRR support in the future. Cheers!

davidjerleke commented 5 months ago

@StevenVerbiest thanks. Although Embla doesn't run its script on the server, it still allows rendering the HTML and CSS on the server side.