liveBook · Manning

Feature detection

As explained in Vanilla Web

Feature detection is a technique for determining at runtime whether a browser or environment supports a particular capability.

A web app may run in different browser engines, versions, and embedded web views. Feature detection checks for a capability at runtime instead of assuming it exists because of the browser’s name. It lets the app keep a usable core and add capabilities where supported. section 2.1.1 section 2.2

The basic pattern is a JavaScript if statement that tests whether an API is present before using it. Build essential behavior on stable APIs; treat less widely available or experimental APIs as enhancements with fallbacks. Compatibility data from MDN, CanIUse, and Baseline helps with planning, while the runtime check handles the environment actually running the app. section 2.2.1

if ('permissions' in navigator) {
   navigator.permissions.query({name: "geolocation"})
        .then(console.log);
}

Check for the Permissions API on navigator before querying geolocation permission. section 2.2.1

Keep the check tied to a usable path:

  • For optional UI APIs, offer a fallback so the core experience remains usable. section 3.8.2
  • For file pickers, check for the picker on window and fall back to a regular file input where appropriate. Availability does not remove the secure-context and user-gesture requirements. section 12.4.7
  • If a small library depends on a newer API, make it support feature detection rather than assuming every browser has that API. section 14.1.1
if ("showSaveFilePicker" in window) {
  const handle = await window.showSaveFilePicker({
    suggestedName: "draft.txt",
    types: [
      {
        description: "Text files",
        accept: { "text/plain": [".txt"] },
      },
    ],
  });

  const writable = await handle.createWritable();
  await writable.write("Vanilla Web");
  await writable.close();
}

A save workflow checks for showSaveFilePicker before opening and writing a user-selected file; the picker still needs a secure context and a user gesture. section 12.4.7

How Vanilla Web covers it

How deep it goes
Demonstrates property-presence checks for the Permissions API and file pickers, including picker constraints, without exploring browser internals.
Examples
Uses a guarded geolocation permission query and guarded file-opening and file-saving workflows.
What it assumes
Introduces availability checking with a simple JavaScript if, without requiring a library or framework.
What you can do after
Apply it via native JavaScript property checks to use supported APIs conditionally and provide a fallback.
How it connects
Links runtime checks to progressive enhancement, graceful fallbacks, API permissions, and Baseline compatibility data.

Also explained in

Compare how 3 books explain feature detection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.