Skip to content
Add to ChromeAdd to Edge Add to Home Screen
Open source · MIT · v1.0.0

@awaketab/wake: a wake lock that says when it works

@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 npm package coming soon

Live state machine

Running awaketab-wake.iife.js v1.0.0, the build that will be published to npm.

Ready state: idle

Uses this browser’s own Screen Wake Lock API. Switch tabs for real to see it pause. Simulate tab hidden and visible work in the second scenario. An injected lock whose “tab hidden” releases it, so lost and the retry are visible. An injected lock that rejects with NotAllowedError, as Firefox does at 5 % battery or less while not charging. Simulate tab hidden and visible work in the second scenario. No Wake Lock API, so Request starts the inlined video fallback from your click. Simulate tab hidden and visible work in the second scenario.

  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

release() returns to idle from any state. States you have visited stay lit.

Transition log

newest first · last 12

No transitions yet. Press Request to ask for a lock.

    Use it

    ESM, for bundlers. Every change event carries the state it left, the state it entered, a reason and an advice code.

    main.js
    import { createWakeLock } from '@awaketab/wake'; const lock = createWakeLock();lock.on('change', ({ from, to, reason, advice }) => {  pill.textContent = to;            // one of the seven states  hint.textContent = advice ?? '';  // e.g. 'iframe_no_allow', 'hidden_document'});start.addEventListener('click', () => lock.request()); // a tap allows the fallbackstop.addEventListener('click', () => lock.release());

    Compared with NoSleep.js

    Both use the native Wake Lock when it exists. The difference is what you are told when it does not work.

    Feature @awaketab/wake NoSleep.js 0.12.0
    Last release Maintained; semver 16 December 2020
    Native Wake Lock Yes, first Yes (when present)
    State reporting Seven states + change events with reason and advice isEnabled boolean
    Denial diagnosis classifyDenial() advice codes None
    Fallback Inlined 1-frame video, gesture-aware, pauses when hidden Looping video, always on
    Size (gzip) ≤ 3.4 KB, fallback included ≈ 3.2 KB
    TypeScript · SSR Native · inert on the server Community typings · no
    Adapters React, Preact, Vue (< 400 B each) No
    Last release
    wakeMaintained; semver
    NoSleep.js16 December 2020
    Native Wake Lock
    wakeYes, first
    NoSleep.jsYes (when present)
    State reporting
    wakeSeven states + change events with reason and advice
    NoSleep.jsisEnabled boolean
    Denial diagnosis
    wakeclassifyDenial() advice codes
    NoSleep.jsNone
    Fallback
    wakeInlined 1-frame video, gesture-aware, pauses when hidden
    NoSleep.jsLooping video, always on
    Size (gzip)
    wake≤ 3.4 KB, fallback included
    NoSleep.js≈ 3.2 KB
    TypeScript · SSR
    wakeNative · inert on the server
    NoSleep.jsCommunity typings · no
    Adapters
    wakeReact, Preact, Vue (< 400 B each)
    NoSleep.jsNo
    What a wake lock cannot do

    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).