npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

duplicate-tab-detector

v1.0.3

Published

React hook that detects duplicated browser tabs and resets per-tab session ids.

Downloads

7

Readme

duplicate-tab-detector

React hook that spots duplicated browser tabs, clears the duplicated tab's sessionStorage, and assigns a fresh session id so each tab stays isolated.

Install

npm install duplicate-tab-detector

Usage

Pick the style that fits your app:

  • Side-effect only (no lint warnings): starts detection as soon as the module loads.
    import "duplicate-tab-detector";
  • Hook with UI: use the hook to render state or add your own handler.
    import { useDuplicateTabSession } from "duplicate-tab-detector";

Full example with a simple UI:

import { useDuplicateTabSession } from "duplicate-tab-detector";

export function App() {
  const { sessionId, instanceId, duplicateDetected, resetSession } =
    useDuplicateTabSession({
      onDuplicate: ({ previousSessionId, newSessionId }) => {
        console.log("Duplicate detected", { previousSessionId, newSessionId });
      },
    });

  return (
    <div>
      <p>Session id: {sessionId ?? "loading..."}</p>
      <p>Instance id: {instanceId ?? "loading..."}</p>
      <p>
        Status:{" "}
        {duplicateDetected
          ? "Duplicate tab detected; sessionStorage was cleared for this tab."
          : "No duplicate detected for this tab."}
      </p>
      <button onClick={resetSession}>Reset session id manually</button>
    </div>
  );
}

If you prefer to start detection manually (for example, outside of React) you can call startDuplicateTabSession() from the package entry point.

Behavior at a glance (for developers)

  • The first import starts listening for duplicate tabs and broadcasts via localStorage.
  • A duplicated tab clears its own sessionStorage, writes a fresh session id, and sets duplicateDetected to true.
  • Storage failures (private mode, disabled storage) are tolerated by falling back to in-memory ids.
  • SSR is safe: when window is missing, the hook returns null state until the browser hydrates.

Options

  • sessionStorageKey (default tabSessionId): key used to store the per-tab session id.
  • requestKey (default tab-session-request): localStorage key used to broadcast duplicate-detection requests.
  • responseKey (default tab-session-response): localStorage key used to answer duplicate-detection requests.
  • onDuplicate: callback invoked after a duplicate is detected and the session id is reset.

What it does

  • Creates a per-tab session id (kept across reloads via sessionStorage).
  • When a tab is duplicated, the new tab broadcasts via localStorage and receives a response from the original tab.
  • Upon detecting that it is a duplicate, the duplicated tab clears its own sessionStorage, generates a new session id, and updates state.
  • Works even when storage is unavailable (privacy modes) by falling back to in-memory ids.

Notes

  • The hook only runs in the browser; server-side renders simply see the initial null values until hydration.
  • The package ships both ESM and CJS builds; TypeScript typings are included.