@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.
…
-
idleReady -
requestingStarting… -
heldScreen awake -
lostPaused — tab hidden -
deniedBlocked — here's the fix -
unsupportedTap to use the fallback -
fallbackAwake via video fallback
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 release | maintained; semver | 16 Dec 2020 |
| Native Wake Lock | yes, first | yes (when present) |
| Honest state | 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 |