Skip to content

perf: use Clipboard API - #1416

Merged
sserrata merged 3 commits into
PaloAltoNetworks:mainfrom
florian-lefebvre:perf/clipboard-api
Apr 14, 2026
Merged

sserrata merged 3 commits into
PaloAltoNetworks:mainfrom
florian-lefebvre:perf/clipboard-api

Conversation

@florian-lefebvre

Copy link
Copy Markdown
Contributor

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

  • Improvement/optimization

Checklist

  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes if appropriate.
  • All new and existing tests passed.

@sserrata sserrata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@florian-lefebvre

Copy link
Copy Markdown
Contributor Author

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 sserrata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! The updated implementation correctly follows the upstream Docusaurus pattern with the navigator.clipboard guard and lazy-loaded fallback. Thanks for the quick update!

@sserrata
sserrata merged commit 42e1bf2 into PaloAltoNetworks:main Apr 14, 2026
2 of 3 checks passed
@sserrata sserrata mentioned this pull request Apr 14, 2026
4 tasks

This branch had an error being deployed

1 failed deployment
default — 4a0e8c92 Deployed Apr 14, 2026 by florian-lefebvre via Analyze Unsafe (javascript) #2067
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants