Skip to content

fix: handle clipboard copy failures gracefully #35

Open
dsimansk wants to merge 1 commit into
functions-dev:masterfrom
dsimansk:pr/fix-catch
Open

fix: handle clipboard copy failures gracefully #35
dsimansk wants to merge 1 commit into
functions-dev:masterfrom
dsimansk:pr/fix-catch

Conversation

@dsimansk

Copy link
Copy Markdown
Contributor

Changes

  • Fix missing catch in clipboard navigation

Per issue description:

GitHub Pages clipboard fix - Add missing .catch() on navigator.clipboard.writeText promise chain to prevent unhandled rejection if clipboard permission is denied.

@matejvasek is it that simple? :)

@matejvasek matejvasek requested a review from twoGiants June 10, 2026 12:34
Comment thread pages/index.html Outdated

@twoGiants twoGiants left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep it up and see my comment below 😸 👍

Comment thread pages/index.html Outdated
@matejvasek

matejvasek commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Doesn't he error happen before the promises comes to existence? I think the catch should be one level up or we should simply check if navigator.clipboard is defined.

It crashes with access navigator.clipboard.writeText before any promise is formed because navigator.clipboard does not exist.

Regardless it's definitely important to have that catch block! One never know what throws.

@matejvasek

matejvasek commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator
diff --git a/pages/index.html b/pages/index.html
index 7023eff..78753e7 100644
--- a/pages/index.html
+++ b/pages/index.html
@@ -240,6 +240,11 @@
           if (strip) {
             text = text.split('\n').map(l => l.startsWith(strip) ? l.slice(strip.length) : l).join('\n');
           }
+	  if (typeof navigator.clipboard === 'undefined') {
+            btn.classList.add('copy-error');
+            setTimeout(() => { btn.classList.remove('copy-error'); }, 1500);
+	    return
+	  }
           navigator.clipboard.writeText(text).then(() => {
             btn.classList.add('copied');
             setTimeout(() => { btn.classList.remove('copied'); }, 1500);

@matejvasek

Copy link
Copy Markdown
Collaborator

Hmm it appears the above is issue only if testing site under plain HTTP. In real usage with HTTPS it probably won't happen.

@matejvasek matejvasek requested a review from twoGiants June 12, 2026 12:14
@twoGiants

Copy link
Copy Markdown
Collaborator

Can you add a screenshot? Then I don't need to manual test locally.

One nit.

Also squash commits and use claude for a nice commit message. We have a guide docs/references/commit-message-guide.md he can follow.

@dsimansk

Copy link
Copy Markdown
Contributor Author

My bad, I chatted with Matej about sceenshots and everything and forgot to get back here.

Screenshot 2026-06-12 at 14 04 07 Screenshot 2026-06-12 at 11 52 11

Comment thread pages/index.html Outdated
The copy buttons on the landing page silently failed when
the Clipboard API was unavailable (non-HTTPS) or when
writeText was rejected. Added a pre-check for
navigator.clipboard and a .catch() handler, both showing
an error state with an X icon and "Copy failed" label.

Co-Authored-By: Claude <noreply@anthropic.com>
@dsimansk dsimansk changed the title Fix missing catch in clipboard navigation fix: handle clipboard copy failures gracefully Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants