perf(landing): share one SSE socket for online counters, respect reduced motion
Header and hero/stats counters each opened their own EventSource to the online-count stream; a shared subscriber now opens a single socket and multicasts to every mounted counter. The entrance count-up animation skips its requestAnimationFrame loop when the user prefers reduced motion.
This commit is contained in:
1 parent
4b5dda467c
commit
a6e808a099
4 files changed
+95
-34
No files matched your search
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { motion, useInView } from "motion/react";
|
||||
import { motion, useInView, useReducedMotion } from "motion/react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
export function AnimatedCounter({
|
||||
@@ -12,6 +12,7 @@ export function AnimatedCounter({
|
||||
}) {
|
||||
const ref = useRef<HTMLSpanElement>(null);
|
||||
const isInView = useInView(ref, { once: true, margin: "-50px" });
|
||||
const prefersReducedMotion = useReducedMotion();
|
||||
const started = useRef(false);
|
||||
const [display, setDisplay] = useState(0);
|
||||
|
||||
@@ -24,6 +25,11 @@ export function AnimatedCounter({
|
||||
}
|
||||
started.current = true;
|
||||
|
||||
if (prefersReducedMotion) {
|
||||
setDisplay(value);
|
||||
return;
|
||||
}
|
||||
|
||||
const duration = 1200;
|
||||
const start = performance.now();
|
||||
let raf = 0;
|
||||
@@ -41,7 +47,7 @@ export function AnimatedCounter({
|
||||
|
||||
raf = requestAnimationFrame(tick);
|
||||
return () => cancelAnimationFrame(raf);
|
||||
}, [isInView, value]);
|
||||
}, [isInView, value, prefersReducedMotion]);
|
||||
|
||||
return (
|
||||
<motion.span
|
||||
|
||||
Reference in new issue
Block a user