Skip to content

Commit 8ce121f

Browse files
committed
[Docs Site] Use ClipboardItem for async fetch in CopyPageButton
1 parent 3d1d65c commit 8ce121f

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/components/CopyPageButton.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ export default function CopyPageButton() {
5050
const handleCopyMarkdown = async () => {
5151
const markdownUrl = new URL("index.md", window.location.href).toString();
5252
try {
53-
const response = await fetch(markdownUrl);
54-
55-
if (!response.ok) {
56-
throw new Error(`Received ${response.status} on ${response.url}`);
57-
}
53+
const clipboardItem = new ClipboardItem({
54+
["text/plain"]: fetch(markdownUrl)
55+
.then((r) => r.text())
56+
.catch((e) => {
57+
throw new Error(`Received ${e.message} for ${markdownUrl}`);
58+
}),
59+
});
5860

59-
const markdown = await response.text();
60-
await navigator.clipboard.writeText(markdown);
61+
await navigator.clipboard.write([clipboardItem]);
6162
track("clicked copy page button", {
6263
value: "copy markdown",
6364
});

0 commit comments

Comments
 (0)