Skip to content

Commit d7c4d2c

Browse files
authored
[Docs Site] Use ClipboardItem for async fetch in CopyPageButton (#22475)
* [Docs Site] Use ClipboardItem for async fetch in CopyPageButton * return blob rather than string
1 parent 3d1d65c commit d7c4d2c

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/components/CopyPageButton.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ 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+
.then((t) => new Blob([t], { type: "text/plain" }))
57+
.catch((e) => {
58+
throw new Error(`Received ${e.message} for ${markdownUrl}`);
59+
}),
60+
});
5861

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

0 commit comments

Comments
 (0)