Skip to content

Keep on this device

A toggle that stores a page and its files in this browser so it opens with no connection.

<span id="keep-live" data-cap="offline-keep" data-worker="./offline-sw.js" data-scope="./" data-cache="cap-specimen-kept-v1"></span>

Installation

// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/offline-keep.css";
import { enhance } from "capsomer/behavior/offline-keep";

enhance();

Usage

<span data-cap="offline-keep" data-worker="/sw.js" data-scope="/"></span>
<!-- script fills it: -->
<span data-cap="offline-keep" data-worker="/sw.js" data-scope="/" data-cap-ready="">
  <button type="button" class="cap-btn cap-keep" aria-pressed="false" title="Keep a copy that opens with no connection">Keep on this device</button>
  <span class="cap-keep-status" role="status"></span>
</span>

Examples

Kept, with its status

<span data-cap="offline-keep" data-cap-ready="">
  <button type="button" class="cap-btn cap-offline-keep-button" aria-pressed="true" title="Press to remove the copy kept on this device">Keep on this device</button>
  <span class="cap-offline-keep-status" role="status">Kept on this device: it opens with no connection.</span>
</span>

API reference

Functions

FunctionSignatureDoes
keepSupported(): boolean
workerUrl(o: Pick<KeepOptions, "worker" | "cache">): stringThe worker's address with the store's name, unless it is the default.
filesOfPage(page: string): string[]Every same-origin file this page uses: its stylesheets, scripts and module chunks, images, and the fonts and scripts the page loaded after. Each is stored as fetched, so the kept page draws as it does online.
isKept(o: KeepOptions): Promise<boolean>Looked up, never opened: opening a store creates it, and a reader who only views the page stores nothing.
keep(o: KeepOptions): Promise<void>
remove(o: KeepOptions): Promise<void>
enhance(root?: ParentNode): () => voidFills every empty [data-cap="offline-keep"] with the toggle and its status line.

Parts and data attributes

ClassIs
.cap-offline-keep-buttonthe root
.cap-offline-keep-statusits own class
AttributeValues
data-capoffline-keep
StateRead from
:emptythe platform
[aria-pressed="true"]an attribute

Accessibility

KeyDoes
TabMoves to the button
Enter, SpaceKeeps the page, or removes the kept copy
  • A toggle button (aria-pressed) whose words change with its state; its title says what a press will do.
  • The result is a status line, read when it changes.