Sphere
Log in Join
☆ 16

Clipboard API throws NotAllowedError on our staging site

navigator.clipboard.writeText works on localhost and in prod (https), but on staging, which is http://staging.internal:8080, it throws: NotAllowedError: Write permission denied. I know clipboard is a secure-context API and http is the problem. I cannot easily get a cert for staging. Is there a fallback that is not document.execCommand('copy')? We tried that and it is flaky in Safari. Would rather not add a second code path if I can avoid it.

⚑ Report

4 replies

☆ 0
Y yaml ·2026-10-18#1

Secure context is the whole ballgame here. localhost is treated as secure by special case, your internal hostname is not. execCommand is the classic fallback but yeah it is deprecated and flaky. The clean fix is to put staging behind https, even a self-signed cert with an exception, or use a real hostname with a cert.

⚑ Report
☆ 0
I inode ·2026-10-24#2

Self-signed cert it is then. I added a hosts entry and a local CA cert so the browser stops complaining. Copy works. Slightly annoying to set up but better than maintaining two code paths. We learned this the hard way when a demo copy button silently failed on the projector.

⚑ Report
☆ 0
T tf ·2026-10-29#3

Just a note: even with https, writeText needs a user gesture in most browsers. If you are calling it from a setTimeout after a click, the gesture may be lost. Call it synchronously in the click handler if you can.

⚑ Report
☆ 0
L loki ·2026-11-06#4

We actually solved this by pointing staging at a real subdomain with a cert from the same wildcard we use in prod. Costs nothing if you already have the wildcard, and it means staging behaves exactly like prod for everything, not just clipboard.

⚑ Report
Log in to reply.