Skip to content

SSR Support – Server-Side Rendering Guide

Overview

All hooks in this library are SSR-safe out of the box. They work correctly with:

  • Next.js (App Router & Pages Router)
  • Remix
  • Gatsby
  • Astro (with React islands)
  • Any custom SSR setup

How it works

Every hook checks for the existence of browser APIs (document, window, navigator) before accessing them. On the server, hooks return sensible defaults:

HookServer default
useDocVisibletrue
useIdleVisibility{ visible: true, idle: false }
useAutoPauseVideoNo-op
useSmartPollingFires initial fetch, no interval
usePageFocusEffectNo-op
useNetworkAwarePollingisOnline: true, base interval
useInactivityTimeoutidle: false, isWarning: false, isTimedOut: false
useWakeLockisSupported: false, isActive: false
useBatteryAwarecharging: true, level: 1, isSupported: false

No "use client" needed (but compatible)

The hooks use useState and useEffect, which are client-side React primitives. In Next.js App Router, you’ll need to use them in Client Components:

"use client";
import { useDocVisible } from "react-visibility-hooks";
export function VisibilityIndicator() {
const isVisible = useDocVisible();
return <span>{isVisible ? "👁" : "💤"}</span>;
}

The library itself does not include a "use client" directive — this is intentional so it doesn’t force a client boundary higher than necessary.

Hydration safety

useDocVisible syncs the actual visibility state in useEffect after hydration, so even if the server-rendered value (true) differs from the client’s actual state, there’s no hydration mismatch.