Skip to content

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

@io-ing

Description

@io-ing

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

Bug Report

Docusaurus version: 3.9.2
Node version: v20.19.5
Browser: chrome 145.0.7632.116

Problem

After upgrading from 3.8.x to 3.9.x, the code block copy button throws an error when the site is accessed via a non-HTTPS local network address (e.g.
http://192.168.x.x:3000).

TypeError: Cannot read properties of undefined (reading 'writeText')
at CopyButton/index.js

Root Cause

PR #11422 replaced copy-text-to-clipboard with the native navigator.clipboard.writeText() API. However, navigator.clipboard is undefined in non-HTTPS contexts
(except localhost). The old library had a <textarea> fallback that handled this case.

Reproducible demo

No response

Steps to reproduce

  1. Run npm run start -- --host 0.0.0.0
  2. Access the site from another device on the same LAN via http://192.168.x.x:3000
  3. Click the copy button on any code block

Expected behavior

Code is copied to clipboard successfully.

Actual behavior

The copy button throws an error and nothing is copied:

TypeError: Cannot read properties of undefined (reading 'writeText')
at CopyButton/index.js

Your environment

  • Public source code: N/A
  • Public site URL: N/A
  • Docusaurus version used: 3.9.2
  • Environment name and version: Node.js v20.19.5, Chrome 145.0.7632.116
  • Operating system and version: Ubuntu 20.04 LTS

Self-service

  • I'd be willing to fix this bug myself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn error in the Docusaurus core causing instability or issues with its execution

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions