Skip to content

fix(theme): add clipboard fallback for non-HTTPS environments#11792

Closed
io-ing wants to merge 1 commit intofacebook:mainfrom
io-ing:fix/copy-button-non-https-fallback
Closed

fix(theme): add clipboard fallback for non-HTTPS environments#11792
io-ing wants to merge 1 commit intofacebook:mainfrom
io-ing:fix/copy-button-non-https-fallback

Conversation

@io-ing
Copy link

@io-ing io-ing commented Mar 10, 2026

navigator.clipboard is undefined in non-HTTPS contexts (e.g. accessing dev server via LAN IP like http://192.168.x.x:3000), which causes the copy button to throw an error since #11422 removed the copy-text-to-clipboard fallback.

This PR adds a textarea-based fallback when navigator.clipboard is unavailable.

Fixes 11791

@meta-cla
Copy link

meta-cla bot commented Mar 10, 2026

Hi @io-ing!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@netlify
Copy link

netlify bot commented Mar 10, 2026

[V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit d0b9f0f
🔍 Latest deploy log https://app.netlify.com/projects/docusaurus-2/deploys/69b108f0cc79ea0008e6c97e
😎 Deploy Preview https://deploy-preview-11792--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

I would prefer re-introducing copy-text-to-clipboard
because it handles quirks we don't want to deal with

We could eventually lazy load it with a dynamic import so that it doesn't need to load/init for users who are using https

return;
}

// Fallback for non-HTTPS environments where navigator.clipboard is unavailable
Copy link
Collaborator

Choose a reason for hiding this comment

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

If this doesn't work, I'd rather revert to the former lib we were using, that handles quirks too

@meta-cla
Copy link

meta-cla bot commented Mar 10, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla bot added the CLA Signed Signed Facebook CLA label Mar 10, 2026
@io-ing io-ing force-pushed the fix/copy-button-non-https-fallback branch 4 times, most recently from 0e17ed4 to c146016 Compare March 11, 2026 06:12
@io-ing io-ing force-pushed the fix/copy-button-non-https-fallback branch from c146016 to d0b9f0f Compare March 11, 2026 06:17
Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

almost looks good but I'd like a comment to explain why we need this and also to not swallow errors

unhandled errors must remain unhandled. If someone is using Sentry/posthog/whatever observability tool on their website, they should be able to capture/report the unhandled errors

}

try {
const copy = (await import('copy-text-to-clipboard')).default;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add a comment, maybe linking to this PR, explaining why we still need the lib? (now and in the future)

const copy = (await import('copy-text-to-clipboard')).default;
return copy(code);
} catch {
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

please do not swallow errors

try {
await navigator.clipboard.writeText(code);
return true;
} catch {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please do not swallow errors

@slorber
Copy link
Collaborator

slorber commented Mar 12, 2026

hey, I didn't notice but we got 2 PR for the same issue, and I already merged this other one: #11796

@slorber slorber closed this Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Signed Facebook CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CopyButton throws "Cannot read properties of undefined (reading 'writeText')" in non-HTTPS environments after 3.9.x

2 participants