laravel/framework · warning · Error

Failed to copy text to clipboard

Error message

Failed to copy text to clipboard

What it means

Thrown by the exception renderer's copy-to-clipboard helper when the legacy fallback path (document.execCommand('copy')) fails. It only triggers when navigator.clipboard is unavailable (insecure context) AND execCommand returns false, meaning the browser refused the copy. This is a frontend convenience error in Laravel's Whoops-style exception page, not application code.

Source

Thrown at src/Illuminate/Foundation/resources/exceptions/renderer/scripts.js:36

});

window.copyToClipboard = async function (text) {
    if (navigator.clipboard) {
        await navigator.clipboard.writeText(text);
    } else {
        const textarea = document.createElement('textarea');
        textarea.value = text;
        textarea.style.position = 'fixed';
        textarea.style.opacity = '0';
        textarea.style.pointerEvents = 'none';
        document.body.appendChild(textarea);
        textarea.select();

        const result = document.execCommand('copy');
        document.body.removeChild(textarea);

        if (!result) {
            throw new Error('Failed to copy text to clipboard');
        }
    }
};

const highlighter = createHighlighterCoreSync({
    themes: [lightPlus, darkPlus],
    langs: [php, sql, json],
    engine: createJavaScriptRegexEngine(),
});

window.highlight = function (
    code,
    language,
    truncate = false,
    editor = false,
    startingLine = 1,
    highlightedLine = null
) {

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Serve the page over HTTPS (or via localhost) so navigator.clipboard is available and the fallback is never reached.
  2. If embedded in an iframe, add allow="clipboard-write" to the iframe element and ensure the parent grants permission.
  3. Ensure the document is focused (window.focus()) before invoking copyToClipboard.
  4. Wrap the call in try/catch and show a manual fallback (e.g. an already-selected textarea) when it throws.

Example fix

// before
window.copyToClipboard = async function (text) {
  if (navigator.clipboard) {
    await navigator.clipboard.writeText(text);
  } else {
    // execCommand fallback that may throw
  }
};

// after
try {
  await window.copyToClipboard(text);
} catch (e) {
  // reveal a textarea with the text pre-selected so the user can press Ctrl+C
  showManualCopyFallback(text);
}
Defensive patterns

Strategy: try-catch

Validate before calling

function canUseClipboardApi() {
  return typeof navigator !== 'undefined' && navigator.clipboard && window.isSecureContext;
}

Type guard

function isClipboardAvailable() {
  return typeof navigator !== 'undefined' && !!navigator.clipboard && window.isSecureContext;
}

Try / catch

try {
  await window.copyToClipboard(text);
} catch (e) {
  // graceful degradation: show a pre-selected textarea fallback
  showManualCopyFallback(text);
}

Prevention

When it happens

Trigger: Calling window.copyToClipboard(text) on a page served over plain HTTP (not localhost), inside a sandboxed iframe without allow-popups/clipboard-write, or when the document is not focused so execCommand('copy') returns false.

Common situations: Running the app on http:// (non-TLS) staging where the Async Clipboard API is gated behind secure contexts; iframe-embedded previews; automated test runners where the document lacks focus.


AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06). Data as JSON: /data/errors/6208705c99606be5.json. Report an issue: GitHub.