fix(activity-feed): avoid hydration mismatch from localStorage
Read tracking preference after mount instead of during initial render to prevent SSR/client divergence that can leave the feed stuck in its initial (small/transparent) styles after page reload. Co-authored-by: dennis <dennis@konkol.net>
This commit is contained in:
@@ -54,27 +54,31 @@ export default function ActivityFeed() {
|
|||||||
const [isExpanded, setIsExpanded] = useState(true);
|
const [isExpanded, setIsExpanded] = useState(true);
|
||||||
const [isMinimized, setIsMinimized] = useState(false);
|
const [isMinimized, setIsMinimized] = useState(false);
|
||||||
const [hasActivity, setHasActivity] = useState(false);
|
const [hasActivity, setHasActivity] = useState(false);
|
||||||
const [isTrackingEnabled, setIsTrackingEnabled] = useState(() => {
|
// NOTE: Don't read localStorage during initial render.
|
||||||
// Check localStorage for tracking preference
|
// Doing so can cause a hydration mismatch (SSR default vs client preference),
|
||||||
if (typeof window !== "undefined") {
|
// which can leave the feed stuck in its initial (small/transparent) motion styles.
|
||||||
try {
|
const [isTrackingEnabled, setIsTrackingEnabled] = useState(true);
|
||||||
const stored = localStorage.getItem("activityTrackingEnabled");
|
|
||||||
return stored !== "false"; // Default to true if not set
|
|
||||||
} catch (error) {
|
|
||||||
// localStorage might be disabled
|
|
||||||
if (process.env.NODE_ENV === 'development') {
|
|
||||||
console.warn('Failed to read tracking preference:', error);
|
|
||||||
}
|
|
||||||
return true; // Default to enabled
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
const [quote, setQuote] = useState<{
|
const [quote, setQuote] = useState<{
|
||||||
content: string;
|
content: string;
|
||||||
author: string;
|
author: string;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
|
|
||||||
|
// Sync tracking preference after mount (client-only)
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof window === "undefined") return;
|
||||||
|
try {
|
||||||
|
const stored = localStorage.getItem("activityTrackingEnabled");
|
||||||
|
const enabled = stored !== "false"; // Default to true if not set
|
||||||
|
setIsTrackingEnabled(enabled);
|
||||||
|
} catch (error) {
|
||||||
|
// localStorage might be disabled
|
||||||
|
if (process.env.NODE_ENV === "development") {
|
||||||
|
console.warn("Failed to read tracking preference:", error);
|
||||||
|
}
|
||||||
|
// Keep default (enabled)
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
// Fetch data every 30 seconds (optimized to match server cache)
|
// Fetch data every 30 seconds (optimized to match server cache)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Don't fetch if tracking is disabled or during SSR
|
// Don't fetch if tracking is disabled or during SSR
|
||||||
|
|||||||
Reference in New Issue
Block a user