AwakeTab

@awaketab/wake: the honest wake lock library

@awaketab/wake keeps a web page's screen from dimming and tells you the truth about it: seven states, a reason and an advice code on every change, and held only while the browser really holds the lock. Zero dependencies, ≤ 3.4 KB gzipped with the fallback videos inlined, MIT. The maintained replacement for NoSleep.js.

npm install @awaketab/wake

Live state machine

Running awaketab-wake.iife.js v1.0.0 — the exact file on npm. …

  1. idle Ready
  2. requesting Starting…
  3. held Screen awake
  4. lost Paused — tab hidden
  5. denied Blocked — here's the fix
  6. unsupported Tap to use the fallback
  7. fallback Awake via video fallback

idle

Transition log

    Use it

    ESM (bundlers)

    import { createWakeLock } from '@awaketab/wake';
    
    const lock = createWakeLock();
    lock.on('change', ({ from, to, reason, advice }) => {
      pill.textContent = to;            // idle · requesting · held · lost · denied · unsupported · fallback
      hint.textContent = advice ?? '';  // e.g. 'battery_saver', 'iframe_no_allow'
    });
    start.addEventListener('click', () => lock.request()); // a gesture keeps the video fallback available
    stop.addEventListener('click', () => lock.release());

    CDN (no build step)

    <script src="https://cdn.jsdelivr.net/npm/@awaketab/wake@1/dist/awaketab-wake.iife.js"></script>
    <script>
      const lock = AwakeTabWake.createWakeLock();
      document.querySelector('#start').addEventListener('click', () => lock.request());
    </script>

    React, Preact and Vue

    import { useWakeLock } from '@awaketab/wake/react';
    
    export function KeepAwake() {
      const { state, request, release } = useWakeLock();
      return state === 'held' || state === 'fallback'
        ? <button onClick={release}>Screen awake — stop</button>
        : <button onClick={request}>Keep screen on</button>;
    }

    Compared with NoSleep.js

    Feature @awaketab/wake NoSleep.js 0.12.0
    Last releasemaintained; semver16 Dec 2020
    Native Wake Lockyes, firstyes (when present)
    Honest stateseven states + change events with reason and adviceisEnabled boolean
    Denial diagnosisclassifyDenial() advice codesnone
    Fallbackinlined 1-frame video, gesture-aware, pauses when hiddenlooping video, always on
    Size (gzip)≤ 3.4 KB, fallback included≈ 3.2 KB
    TypeScript · SSRnative · inert on the servercommunity typings · no
    AdaptersReact, Preact, Vue (< 400 B each)no
    A wake lock keeps the display on while the page is visible — nothing more. It does not keep a hidden tab awake, does not stop a laptop sleeping when the lid closes, and does not keep Teams or Slack "Available" (presence follows input, and this library never fakes input).