vydimitrov/use-count-up

render `end` value in HTML during SSR

Closed this issue · 7 comments

Problem:

in SSR generated HTML use-count-up renders 0, instead of end value, which has adverse effects for SEO.

Example

<span><CountUp isCounting end={500} /></span> will result in <span>0</span> in HTML.

Expected:

<span><CountUp isCounting end={500} /></span> should result in <span>500</span> in HTML.

And then on hydration start animating from 0.

Using next.js@12. Likely the same behavior with previous versions as well.

Hey @ksaitor, I will investigate it and come back to you. Can you meanwhile test it with version 2.x.x? I just want to know if it is a bug that was introduced in version 3.

just tested on use-count-up@2 — same problem.

I see now, this was actually never implemented in the component. As far as I know, the only way to know that the component is rendered on the server is to check if window is present. So one way to make it work for you is this start={typeof window === 'undefined' ? end : 0}.

Is this going to work you?

Thank you. Yeah, that works - start={typeof window === 'undefined' ? end : 0}.

Would be nice if this was part of the package, though. Perhaps as an optional flag like ssr. I'd set it to true by default, but you know your user base better than i do :)

Imho, SEO is being hugely overlooked by react community, and it'd be great if more libraries took steps to be aware of it, hence strengthening the tech and community overall.

I was wondering since the markup is generated on the server, do you get to see the end value for a brief moment before the animations kicks in?

I added your idea to the Feature request discussions here. Lets see how it goes and I may considering adding the feature.

@vydimitrov it does show for a split second, but it doesn't bother me personally… imho, it's even better that when it shows 0 for a split second. 👍

Currently using start={typeof window === 'undefined' ? end : 0} and use-count-up on these pages:

2021-11-03 15-00-21 2021-11-03 15_01_27

image

Great, it looks really good :) Thanks for sharing your work!

I am going to close this issue now and consider the feature I proposed.