navigator.share With canShare Guard
Feature-detect navigator.share, validate payloads with navigator.canShare before calling it, and await the promise the share sheet resolves.
Code
// navigator.canShare answers whether the payload shape is shareable on this
// device before navigator.share is called with it.
async function shareReport(): Promise<void> {
const payload = {
title: 'Weekly deploy report',
text: '42 deploys, 3 rollbacks',
url: 'https://devexamples.com/report',
};
if (!navigator.share) {
console.log('Web Share unsupported here');
return;
}
if (!navigator.canShare?.(payload)) {
console.log('payload not shareable on this device');
return;
}
await navigator.share(payload); // resolves when the share sheet closes
console.log('report shared');
}Under the hood
Everything runs through the single shareReport.
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.
Points of care
- Runs on what the toolchain already provides — nothing beyond it.
- The snippet leans on
navigator— browser-only surfaces a bare Node process cannot provide.
Using it
Its main signature reads async function shareReport()
Output lines are prefixed (Web Share unsupported here, payload not shareable on this device, report shared), so each step’s effect is easy to spot.
Key statements
await navigator.share(payload); // resolves when the share sheet closes
console.log('report shared');
if (!navigator.canShare?.(payload)) {
console.log('payload not shareable on this device');
return;
}
Interactive Tools for This Topic
DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.
FAQ
- Does this require external packages?
- No. It relies only on built-in language and platform APIs, so you can paste it into a project without installing anything. The file defines `shareReport` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.