Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 37 ยท Installability and Distribution

Advanced PWA Capabilities

Make a PWA feel native: Web Share, file and protocol handlers, File System Access, Screen Wake Lock, window controls overlay, Background Fetch and safe feature detection.

Installable, offline and push-enabled already covers most of what makes an app feel real. The platform also has a growing set of APIs that close the remaining gap to native apps: sharing content, opening files, taking over links, keeping the screen on, drawing into the title bar. Most are Chromium-first, some are available only to installed apps, and several are missing in Safari or Firefox. This is why the golden rule of this lesson is progressive enhancement: detect the feature, use it if present, and always keep a working fallback.

The manifest-based features (shortcuts, share target and related members) are covered in shortcuts, share target and more manifest features. Here we focus on the JavaScript APIs and the remaining manifest entries that pair with them.

Rule one: detect, never assume

JavaScript
// read-only snippets use real browser APIs
const can = {
  share: "share" in navigator,
  fileSystem: "showOpenFilePicker" in window,
  wakeLock: "wakeLock" in navigator,
  badge: "setAppBadge" in navigator,
  contacts: "contacts" in navigator,
  launchQueue: "launchQueue" in window,
  backgroundFetch: "BackgroundFetchManager" in self,
};

Wrap each feature in a small function that either does the native thing or the portable thing, so the rest of your code never needs to ask.

JS Share with a clipboard fallback

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The real navigator.share() must be called from a user gesture and requires HTTPS. navigator.canShare({ files }) tells you whether file sharing is possible.

Handling files

Installed apps can register as handlers for file types, so a double-click on a .notes file opens your app. The manifest declares it:

JSON
{
  "file_handlers": [
    {
      "action": "/open",
      "accept": { "application/json": [".notes"] }
    }
  ]
}

The page then receives the files through the launch queue:

JavaScript
if ("launchQueue" in window) {
  launchQueue.setConsumer(async (launchParams) => {
    for (const handle of launchParams.files) {
      const file = await handle.getFile();
      openDocument(await file.text(), handle);   // keep the handle to save back
    }
  });
}

Together with the File System Access API (showOpenFilePicker, showSaveFilePicker, handle.createWritable()), this lets a web editor open, edit and save a real file on disk. Browsers that lack it should fall back to <input type="file"> for opening and a download link for saving.

  • protocol_handlers in the manifest let your app handle custom schemes such as web+notes: links.
  • scope_extensions and the experimental link-capturing features let your installed app open URLs from its own site, so tapping a link to your domain opens the app rather than a browser tab.
  • launch_handler with "client_mode": "focus-existing" reuses an open window rather than starting a second copy, and delivers the URL through the launch queue.

Keep the screen on

For recipes, presentations or navigation, the Screen Wake Lock API prevents dimming. The lock is released automatically when the tab is hidden, so request it again when the page becomes visible:

JavaScript
let lock = null;
async function keepAwake() {
  if (!("wakeLock" in navigator)) return false;
  lock = await navigator.wakeLock.request("screen");
  lock.addEventListener("release", () => console.log("wake lock released"));
  return true;
}
document.addEventListener("visibilitychange", () => {
  if (document.visibilityState === "visible" && lock?.released) keepAwake();
});
JS A resilient wake-lock manager

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A desktop-like title bar

Installed desktop PWAs can opt in to the Window Controls Overlay, which extends your content into the title bar area:

JSON
{ "display_override": ["window-controls-overlay", "standalone"] }
CSS
.titlebar {
  position: fixed;
  top: env(titlebar-area-y, 0);
  left: env(titlebar-area-x, 0);
  width: env(titlebar-area-width, 100%);
  height: env(titlebar-area-height, 40px);
  app-region: drag;               /* lets users drag the window by this bar */
}
.titlebar button { app-region: no-drag; }

The env() variables describe the space left free by the system buttons. The fallback values keep the layout valid when the feature is off.

Large downloads that survive

Background Fetch (Chromium) lets the service worker download big files such as podcasts or maps with a browser-managed progress notification, even if the user closes the tab:

JavaScript
const registration = await navigator.serviceWorker.ready;
await registration.backgroundFetch.fetch("episode-12", ["/media/ep12.mp3"], {
  title: "Episode 12", icons: [{ src: "/icons/icon-192.png", sizes: "192x192" }],
  downloadTotal: 48_000_000,
});

Other useful APIs

API Purpose Notes
Contact Picker Choose contacts without a permission-heavy address book Android Chrome
Web Share Target Receive shared data in your app manifest share_target
Badging Number on the app icon see notification design
Web Bluetooth, WebUSB, Web Serial Talk to hardware Chromium desktop and Android, needs HTTPS
Idle Detection, EyeDropper, WebOTP Specialty features Chromium

Before adopting an API, check Baseline status and "Can I use" data and decide what the user without it will see. A feature used for polish is fine to lose; a feature your core flow depends on needs a full fallback.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the safest pattern for using a new API?

    Feature detection lets each browser use what it has, and the fallback keeps the feature working elsewhere.
  2. 2. How does an installed PWA receive a file the user opened with it?

    File handlers deliver file handles via launchQueue.setConsumer.
  3. 3. Why re-request a Screen Wake Lock on visibilitychange?

    The sentinel is released when the tab is hidden, so request a new one when it becomes visible again.
  4. 4. What does display_override with window-controls-overlay allow?

    The overlay mode lets the app draw its own title bar next to the window buttons.

Summary

  • Most advanced APIs are Chromium-first: detect, enhance, and always provide a fallback.
  • Web Share, file handlers with the launch queue, and the File System Access API make installed apps useful for real documents.
  • Wake locks need re-acquiring after the page was hidden; the window controls overlay needs display_override and env() variables.
  • Use Background Fetch for large downloads and the Badging API for passive counts.

Next: Project: An Offline Notes App