Add graceful fallback for browsers without Web Share API support

Created on 26 October 2025, 9 days ago

Problem/Motivation

The module currently relies exclusively on the Web Share API to trigger native sharing dialogs. However, this API is not supported in all browsers - especially on desktop (e.g., Firefox, Safari on macOS). As a result, the share button silently fails in unsupported environments, leading to poor user experience.

Proposed resolution

Implement a graceful fallback mechanism for browsers that do not support navigator.share. Suggested behavior if navigator.share is unavailable:
- Use navigator.clipboard.writeText() to copy the current page URL.
- Show a confirmation message (e.g., alert() or modal): “Link copied to clipboard.”
- If clipboard API is also unavailable, fall back to prompt() with the URL pre-filled.
This logic can be added to the module’s JavaScript file inside a Drupal.behaviors block using once() to avoid duplicate bindings.

if (navigator.share) {
  navigator.share(shareData).catch(console.error);
} else if (navigator.clipboard) {
  navigator.clipboard.writeText(shareData.url).then(() => {
    alert('Link copied to clipboard!');
  }).catch(() => {
    prompt('Copy this link:', shareData.url);
  });
} else {
  prompt('Copy this link:', shareData.url);
}

Benefits

- Improves UX for users on unsupported browsers.
- Makes the module more robust and production-ready.
- Aligns with progressive enhancement principles.

Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.71.5 2024