Skip to content

Browser Lifecycle

Typed browser session lifecycle — visibility, focus, connectivity, idle detection, cross-tab sync, and plugins in one headless API.

Install

bash
npm install @jayoncode/browser-lifecycle
bash
pnpm add @jayoncode/browser-lifecycle
bash
yarn add @jayoncode/browser-lifecycle

Example: pause work when the tab is hidden

ts
import { createBrowserLifecycle } from "@jayoncode/browser-lifecycle";

const lifecycle = createBrowserLifecycle({ autoStart: true });

lifecycle.on("page:hidden", () => {
  pauseMedia();
  flushTelemetry();
});

lifecycle.on("page:visible", () => {
  resumeMedia();
});

// Read consolidated state at any time
const { visibility, focus, connectivity } = lifecycle.getSnapshot();

// Teardown on route unmount or app shutdown
await lifecycle.dispose();

One instance per tab replaces scattered document / window listeners with typed events and a readonly snapshot.

Verify event ordering →

Problem → approach

Typical painBrowser Lifecycle
document, window, and navigator listeners scattered across featuresOne createBrowserLifecycle() session with typed on() handlers
Tab visibility, focus, and connectivity each wired differentlyModules normalize signals; getSnapshot() exposes consolidated state
SSR crashes or silent no-ops when APIs are missingCapability detection and SSR-safe defaults before modules attach

Overview

createBrowserLifecycle() orchestrates browser signals through a module pipeline. Consumers subscribe to events or poll getSnapshot(); modules (visibility, focus, connectivity, idle, cross-tab) compose behind a single session boundary.

ConcernAPI surface
Lifecyclestart(), dispose(), phase events
Signalson(event, handler), onEvent(handler)
StategetSnapshot() — readonly session state
ExtensionPlugin hooks, module configuration
DiagnosticsgetRuntimeDiagnostics() for development

Designed for SSR-safe capability detection and framework-agnostic integration (React, Vue, vanilla, etc.).

Documentation path

Foundation

#GuideTopicsPlayground
1TutorialInstall, session, subscribe, disposeDashboard
2Core conceptsSession, snapshot, events, modulesState

Core modules

#GuideTopicsPlayground
3VisibilityPage Visibility APIVisibility
4EventsSubscription modelEvents
5Session corePhases, startup orderLifecycle

Configuration and extension

#GuideTopicsPlayground
6Core infrastructureConfig, capabilities, SSRConfiguration
7Usage guideProduction patternsDeveloper tools
8PluginsModule registrationPlugins

Package fit

RequirementModule / event
Pause background work on tab hidepage:hidden / page:visible
React to window focuswindow:focus / window:blur
Offline-aware UIconnectivity:*
Idle timeout / autosave triggersIdle module
Cross-tab coordinationCross-tab sync
SSR / capability guardsCore infrastructure

Reference

ResourceLink
API (TypeDoc)/packages/browser-lifecycle/api/
Framework examples/packages/browser-lifecycle/examples/
Best practices/packages/browser-lifecycle/best-practices/
Patterns/packages/browser-lifecycle/patterns/
FAQ/packages/browser-lifecycle/faq/
Playground guide/packages/browser-lifecycle/playground/playground

Version

Current npm version: v0.2.0 (@jayoncode/browser-lifecycle). See Migration and Changelog for release history.

JOC is a JayOnCode monorepo of @jayoncode/* packages. Docs sync from source via TypeDoc and sync scripts.