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

[Feat]: SSR support #202

Open
1 task done
bartlomiejzuber opened this issue Jun 15, 2021 · 13 comments · May be fixed by #1095
Open
1 task done

[Feat]: SSR support #202

bartlomiejzuber opened this issue Jun 15, 2021 · 13 comments · May be fixed by #1095
Assignees
Labels
core This is related to the core package feature request New feature or request react Issue is related to React solid Issue is related to SolidJS svelte Issue is related to Svelte upcoming A feature or bug fix is on its way for this issue vue Issue is related to Vue

Comments

@bartlomiejzuber
Copy link

bartlomiejzuber commented Jun 15, 2021

Note

Edit: This feature is under development:

Edited by @davidjerleke 2024-12-26.


Bug is related to

  • The Embla Carousel core (all versions)

Embla Carousel version

  • v4.5.3

Describe the bug

  • When carousel is being rendered via SSR there's noticeable flickering effect on first slide on each page refresh (regardless of cache).

CodeSandbox

Steps to reproduce

  1. Go to https://mfs1q.sse.codesandbox.io/
  2. Refresh the page and look at carousel.

Expected behavior

  • It shouldn't flicker when SSR is used.

Additional context

flicker

@bartlomiejzuber bartlomiejzuber added the bug Something isn't working label Jun 15, 2021
@davidjerleke
Copy link
Owner

davidjerleke commented Jun 15, 2021

Hi @bartlomiejzuber,

Thank you for your bug report. It might come as a surprise but this is expected. At the time of writing, Embla Carousel is a client side script and won't do anything on the server.

This is what's happening:

  • You serve your HTML and CSS from the server.
  • The client script mounts and Embla picks up the container and slide dimensions (slide sizes, spacing between slides etc).
  • Embla aligns the slides and positions slides needed to create the loop effect.
  • Hence, you'll see the flicker when this happens.

One way to solve this would be to hide the carousel with opacity: 0; until Embla has initialized. Alternatively, you can add opacity: 0; to all the slides if you still want to display the carousel arrows and dots until Embla has initialized. There are probably other ways to do it but I think you get the idea.

SSR support might be added in the future.

Best,
David

@davidjerleke davidjerleke added not a bug This issue is not a bug and removed bug Something isn't working labels Jun 15, 2021
@manojVivek
Copy link

manojVivek commented Sep 7, 2021

@davidjerleke Do you think the approach suggested in this issue, helps fix this problem? FormidableLabs/nuka-carousel#786 (comment) ?

@davidjerleke
Copy link
Owner

davidjerleke commented Sep 7, 2021

Hi @manojVivek,

Thanks for sharing that.

Currently, the HTML gets server side rendered and you don't loose any important SEO. I think the flicker thing is solvable with a similar approach as you suggest, but if this is going to be implemented we need to ask ourselves at what cost? How much will it affect bundle size and what would be a good solution for this?

@davidjerleke davidjerleke changed the title First slide flickers within carousel with a loop (SSR support) First slide flickers within carousel with a loop Oct 18, 2023
@davidjerleke davidjerleke added feature request New feature or request not planned Won't be investigated unless it gets lots of traction and removed not a bug This issue is not a bug labels Oct 18, 2023
@mariaaraujo
Copy link

mariaaraujo commented Dec 6, 2023

@davidjerleke Thanks for mentioning this issue! But could you help me with something? How could I control if the embla has initialized or not?

@mariaaraujo
Copy link

mariaaraujo commented Dec 7, 2023

@davidjerleke Thanks! I fixed my problem with the solution bellow and while the embla isn't rendered I show a Skeleton

const [isEmblaRendered, setIsEmblaRendered] = useState(false);

useEffect(() => {
    setIsEmblaRendered(true)
}, [])

@MartinCura
Copy link

MartinCura commented Dec 5, 2024

Hi all, in case it's of any help to anyone, here's my approach to a related problem (SSR).

I have a page with a big carousel (product images) of which the currently selected is stored in a query param of the URL. To achieve a server-rendered HTML that is already pointing to the correct image i added a translation to the <div> container of the items and which is the child of the element that holds the ref used by Embla.

Reduced example (from my components based on shadcn's):

  <div ref={carouselRef} className="overflow-hidden">
    <div
      ref={ref}
      className="flex"
      style={{ transform: `translateX(-${initialIdx * 100}%)` }}
    />
  </div>

This translation is overridden by Embla with a transform3d (here if i'm not mistaken) as soon as it loads/hydrates client-side (and becomes interactive), but there's no flicker because it matches up. If you have any margin between the items in the carousel, you may have to adjust for that.

Hope it helps anyone!

@davidjerleke davidjerleke changed the title (SSR support) First slide flickers within carousel with a loop [Feat]: SSR support Dec 5, 2024
@davidjerleke davidjerleke removed the not planned Won't be investigated unless it gets lots of traction label Dec 5, 2024
@davidjerleke
Copy link
Owner

This feature has been added to the roadmap:

@davidjerleke davidjerleke reopened this Dec 24, 2024
@davidjerleke davidjerleke added the upcoming A feature or bug fix is on its way for this issue label Dec 24, 2024
@davidjerleke davidjerleke self-assigned this Dec 24, 2024
davidjerleke added a commit that referenced this issue Dec 25, 2024
@davidjerleke davidjerleke linked a pull request Dec 25, 2024 that will close this issue
@davidjerleke davidjerleke added react Issue is related to React core This is related to the core package svelte Issue is related to Svelte solid Issue is related to SolidJS labels Dec 25, 2024
@davidjerleke davidjerleke added the vue Issue is related to Vue label Dec 25, 2024
@davidjerleke davidjerleke linked a pull request Dec 25, 2024 that will close this issue
@sarussss
Copy link
Contributor

Hi @davidjerleke.
I only work on the client side is there any way to remove SSR feature never used to reduce file size?

@davidjerleke
Copy link
Owner

davidjerleke commented Dec 28, 2024

Hi @sarussss,

Thank you for your question. The SSR feature will be released with v9 and it will be an opt-in feature. By default, it won’t be active. So if that’s your concern, you will be able to use Embla just as you do today without any problems.

But the bundle size will grow a little bit because SSR is the kind of feature that has to be built into the core. However, the bundle size won’t grow significantly. Maybe I will consider lifting out an existing feature and make it a plugin so the bundle size won’t grow but I haven’t decided that yet.

Did that answer your question?

@sarussss
Copy link
Contributor

Hi @davidjerleke
Thank you for your answer. I think it would be more reasonable to deploy it as a plugin, only when someone needs it will they use it.

@davidjerleke
Copy link
Owner

davidjerleke commented Dec 29, 2024

Hey @sarussss,

Sorry if I wasn’t clear 🙂. The SSR feature needs to be integrated into the core because it impacts several parts of the core code, so it can't be a plugin.

What I meant was that I might consider removing a different feature and turning that into a plugin instead, in order to reduce the core library's bundle size.

@sarussss
Copy link
Contributor

Hi @davidjerleke,
Don't get me wrong, I understand there will be features that need to be removed to convert SSR into a plugin, if you find those features are not too important then I think converting to a plugin is best.

@davidjerleke
Copy link
Owner

@sarussss no, SSR can’t be a plugin. It has to be added to the core library, meaning the embla-carousel package because of technical reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core This is related to the core package feature request New feature or request react Issue is related to React solid Issue is related to SolidJS svelte Issue is related to Svelte upcoming A feature or bug fix is on its way for this issue vue Issue is related to Vue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants