Repository navigation
perf: use Clipboard API - #1416
Conversation
sserrata
left a comment
There was a problem hiding this comment.
Thanks for the contribution and the e18e initiative effort!
However, the Clipboard API (navigator.clipboard) is only available in secure contexts (HTTPS or localhost). In HTTP environments (e.g., local network development), navigator.clipboard is undefined and calling .writeText() on it will throw a TypeError.
Since this is a Docusaurus plugin, we should follow the pattern that upstream Docusaurus uses in @docusaurus/theme-classic. Their CopyButton handles this with a guard and lazy-loaded fallback:
async function copyToClipboard(text: string) {
// The clipboard API is only defined in secure contexts (HTTPS / localhost).
// See https://developer.mozilla.org/en-US/docs/Web/API/Clipboard
if (navigator.clipboard) {
return navigator.clipboard.writeText(text);
}
// Fall back to copy-text-to-clipboard for non-secure contexts (e.g. HTTP
// on a local network). The fallback is lazily loaded to avoid bundle
// overhead for the common HTTPS case.
const {default: copy} = await import('copy-text-to-clipboard');
return copy(text);
}This keeps copy-text-to-clipboard as a dependency but lazy-imports it only when navigator.clipboard is unavailable — keeping the bundle lean for the common HTTPS case while still working over plain HTTP.
Could you update the PR to follow this pattern?
|
Done! I think it'd be better to just use the Clipboard API but I understand the rationale here, it's a bit better at least 👍 |
sserrata
left a comment
There was a problem hiding this comment.
LGTM! The updated implementation correctly follows the upstream Docusaurus pattern with the navigator.clipboard guard and lazy-loaded fallback. Thanks for the quick update!
Description
Removes a dependency in favor of the widely supported Clipboard API. This PR was made as part of the e18e initiative.
Motivation and Context
Cleans up the ecosystem (see e18e site for more details)
How Has This Been Tested?
N/A
Screenshots (if appropriate)
Types of changes
Checklist