front-end-monorepo: Project Stats: Project Stats numbers (Classifications Today / Classifications Total) don't show on initial Next.js page fetch

Inconsistent UI Bug?

Package: app-project and/or lib-react-components Confirmed as of commit: Also see: #5974 (previous fix for AnimatedNumbers), #5971 (similar issue) Possibly related Talk issue: https://www.zooniverse.org/talk/17/3402644 ?

In certain cases when the Next.JS server first fetches a page, it’s possible for the Project Stats (“Classifications Today” and “Classifications Total” count on the Classify page) to not display accurate numbers, despite fetching the correct data.

Replicating this issue is a faff, but can be done on localhost:

  • Tested using macOS + Chrome 126 non-Incognito, targeting app-project, with a logged-in session saved. (i.e. when I open the target URL, I’m already logged in with an account that has already made 5+ classifications today.)
  • Start a FRESH instance of the local dev server (yarn dev)
  • Open a new window in Chrome.
  • Open target URL: https://local.zooniverse.org:3000/projects/fulsdavid/the-daily-minor-planet/classify/workflow/22354?env=production (Daily Minor Planets)
    • A hydration warning should appear.
    • Scroll down to the Project Stats; it should display inconsistent numbers (see screenshot below)
  • Refresh the page. Project stats should be normal.

Screenshot: Project Stats show 0 Classifications Today and 0 Classifications Total, but the bar chart indicates otherwise.

image

Extended Testing

Asides from the issue seemingly stemming from the "initial Next.JS page fetch", additional testing shows that the Project Stats otherwise works fine.
  • Tested with macOS + Chrome 126 on https://www.zooniverse.org/projects/fulsdavid/the-daily-minor-planet/classify/workflow/22354 and local (app-project, master)
  • I’m able to load the FEM Classify page (while already logged in) and see the initial Classifications Today and Total counts. While logged in, I can make classifications, and both the Classifications Today and Total counts update as expected. (About 5 classifications per test)
  • I can log out, and the counts will reset to 0
  • I can login to the same account, or to a different account, and the Classifications Today and Total counts update as expected.

Dev Notes

Thoughts 1: I don’t have enough expertise on the Next.JS infrastructure to understand why/how the fetch process would be a problem. Is it a hydration thing?

Even if it is, I can’t tell if it’s anything user-facing (i.e. if it affects the non-dev servers), and how I’d confirm/check that.

Thoughts 2: could it be possible that in certain scenarios, the animation of the <AnimatedNumbers> used in the project stats is interrupted, causing the numbers to “freeze” at 0 (for small counts) or appear to “lag” (for larger counts)?

I’m starting to think if it’s worth removing AnimatedNumbers altogether, if it’s a potential cause of problems.

Thoughts 3: this may issue or may not be related to the Talk post that initiated this investigation, since that volunteer said that despite refreshing their page, the lag still persisted.

But since then it has started to fall behind the ‘classifications today’ counter, until it was lagging behind by 55 classifications. I tried refreshing the page, clearing my cache, waiting for an hour and refreshing again, none of which made any difference

But then again:

Logging out of my account and back in again did seem to do something, though - the total classifications counter is now off by just two classifications. Better, but still not quite what I wanted.

Status

Unknown priority. Impact level unclear, but some volunteers are understandably upset when this occurs. Replicability/consistency of the issue is dubious since it relies on some Next.JS infrastructure shenanigans that I can only consistently replicate on my localhost.

UPDATE (1 Aug): now set to low priority.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22 (22 by maintainers)

Most upvoted comments

Here’s an explanation of the bug.

  1. When the AnimatedNumber component mounts, value is 0 and animated is false. useEffect runs and creates an intersection observer which will animate the displayed number from 0 to 0.
  2. value changes to your actual count and a new animateValue function is created, which will animate the span to that value, but useEffect doesn’t run, so the intersection observer doesn’t update to use the new animateValue. It has captured the value of value when it was created, which is 0.
  3. When you scroll the number into view, it runs an outdated animateValue from the first render, animates the displayed number to 0, then sets animated to true.

So the component will always show 0, if it loads offscreen and value loads asynchronously. There’s no story or test for the case where value is set asynchronously but the code is written to display the value of value that’s captured when the component mounts.

For a more technical explanation, I recommend MDN’s explanation of lexical scoping in closures. Each time the useEffect hook runs, it’s capturing animateValue and lessAnimation from the current render. They, in turn, are capturing the current values of value and duration. TBH, the easiest way to avoid bugs like this is to avoid nested closures. Also, set up ESLint to warn when it detects nested closures, and look out for nested closures as a code smell in PR reviews.