Skip to content

fix: add clipboard fallback for non-secure contexts (HTTP)#189

Open
ptlzc wants to merge 1 commit into
benjitaylor:mainfrom
ptlzc:fix/clipboard-fallback-non-secure-context
Open

fix: add clipboard fallback for non-secure contexts (HTTP)#189
ptlzc wants to merge 1 commit into
benjitaylor:mainfrom
ptlzc:fix/clipboard-fallback-non-secure-context

Conversation

@ptlzc

@ptlzc ptlzc commented Jun 23, 2026

Copy link
Copy Markdown

Problem

The Clipboard API (navigator.clipboard.writeText) is only available in secure contexts (HTTPS or localhost). When agentation runs over plain HTTP — e.g. a Vite dev server inside a Docker container accessed by IP — navigator.clipboard is undefined and the copy button silently fails. The UI shows the "copied" checkmark, but nothing actually reaches the clipboard.

The existing catch block swallows the error without any fallback:

if (copyToClipboard) {
  try {
    await navigator.clipboard.writeText(output);
  } catch {
    // Clipboard may fail (permissions, not HTTPS, etc.) - continue anyway
  }
}

Fix

Add a copyTextToClipboard utility that:

  1. Tries navigator.clipboard.writeText() first (preferred, secure contexts)
  2. Falls back to a temporary <textarea> + document.execCommand('copy') when the Clipboard API is unavailable

This ensures copy works in all contexts — HTTPS, localhost, and plain HTTP.

Testing

  • All 83 existing tests pass
  • Build succeeds (tsup CJS + ESM + types)
  • No new dependencies

The Clipboard API (navigator.clipboard.writeText) is only available in
secure contexts (HTTPS or localhost). When agentation is used over HTTP
(e.g. via Docker dev containers accessed by IP), navigator.clipboard is
undefined and copy silently fails — the UI shows 'copied' but nothing
reaches the clipboard.

Add a copyTextToClipboard utility that falls back to the legacy
document.execCommand('copy') approach with a temporary textarea when
the Clipboard API is unavailable, ensuring copy works in all contexts.
@vercel

vercel Bot commented Jun 23, 2026

Copy link
Copy Markdown

@ptlzc is attempting to deploy a commit to the Benji Taylor's Projects Team on Vercel.

A member of the Team first needs to authorize it.

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.

1 participant