From 40ea071c846b3ab1232391bab15d31f508913bf4 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 9 Mar 2026 18:54:48 +0000 Subject: [PATCH] Fix copy button on Safari --- src/components/PageHeading.tsx | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/components/PageHeading.tsx b/src/components/PageHeading.tsx index f488f4a9..ba4b413a 100644 --- a/src/components/PageHeading.tsx +++ b/src/components/PageHeading.tsx @@ -41,13 +41,20 @@ function CopyAsMarkdownButton() { return () => clearTimeout(timer); }, [copied]); - async function handleCopy() { + async function fetchPageBlob() { const cleanPath = asPath.split(/[?#]/)[0]; + const res = await fetch(cleanPath + '.md'); + if (!res.ok) throw new Error('Failed to fetch'); + const text = await res.text(); + return new Blob([text], {type: 'text/plain'}); + } + + async function handleCopy() { try { - const res = await fetch(cleanPath + '.md'); - if (!res.ok) return; - const text = await res.text(); - await navigator.clipboard.writeText(text); + await navigator.clipboard.write([ + // Don't wait for the blob, or Safari will refuse clipboard access + new ClipboardItem({'text/plain': fetchPageBlob()}), + ]); setCopied(true); } catch { // Silently fail