Ko-fi's embeddable button relies on external JavaScript and triggers a warning in Google's PageSpeed Insights. Here's an alternative version that looks identical but fixes that issue.
Donation platform Ko-fi offers a button you can embed anywhere on your site to encourage your users to 'buy you a coffee'. I use it here on Technically Product - you can see it in the sidebar, under the picture of me. It's the one with the jiggly coffee cup!
Getting the button is simple:
However... if you use Ko-fi's embed code and then run your site through Google's PageSpeed Insights tool, it will flag something up in its DIAGNOSTICS section:
"Avoid document.write(). For users on slow connections, external scripts dynamically injected via document.write() can delay page load by tens of seconds."
The embed code points to an external JavaScript file hosted on the Ko-fi site, and it's this script that uses the document.write() method (to insert the button html I assume). As the Chrome Developers site says in its Best Practice session:
"Using document.write() can delay the display of page content by tens of seconds and is particularly problematic for users on slow connections. Chrome therefore blocks the execution of document.write() in many cases, meaning you can't rely on it."
What if you want to use the Ko-Fi button but also want to avoid document_write()? Well, Ko-Fi does offer some static images you can use instead, like this one:
You can generate these by going to Buttons & Widgets > Ko-fi Button as before, then clicking on Image rather than Button. But they have some big limitations:
So rather than going down the static image route, here's a snippet of html (with inline CSS) you can use to add the button to your site without relying on JavaScript. It's very much a non-official approach (i.e. I've just made it up) but it is based on Ko-fi's original code. If you want to copy the snippet, you'll find a 'copy' button in the options at the top:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | <span> <style> img.kofiimg { display: initial !important; vertical-align: middle; height: 13px !important; width: 20px !important; padding-top: 0 !important; padding-bottom: 0 !important; border: none; margin-top: 0; margin-right: 5px !important; margin-left: 0 !important; margin-bottom: 3px !important; content: url('https://storage.ko-fi.com/cdn/cup-border.png') } .kofiimg:after { vertical-align: middle; height: 25px; padding-top: 0; padding-bottom: 0; border: none; margin-top: 0; margin-right: 6px; margin-left: 0; margin-bottom: 4px !important; content: url('https://storage.ko-fi.com/cdn/whitelogo.svg') } .btn-container { display: inline-block !important; white-space: nowrap; min-width: 160px } span.kofitext { color: #fff !important; letter-spacing: -0.15px !important; text-wrap: none; vertical-align: middle; line-height: 33px !important; padding: 0; text-align: center; text-decoration: none !important; text-shadow: 0 1px 1px rgba(34, 34, 34, 0.05); } .kofitext a { color: #fff !important; text-decoration: none:important; } .kofitext a:hover { color: #fff !important; text-decoration: none } a.kofi-button { box-shadow: 1px 1px 0px rgba(0, 0, 0, 0.2); line-height: 36px !important; min-width: 150px; display: inline-block !important; background-color: #29abe0; padding: 2px 12px !important; text-align: center !important; border-radius: 7px; color: #fff; cursor: pointer; overflow-wrap: break-word; vertical-align: middle; border: 0 none #fff !important; font-family: 'Quicksand', Helvetica, Century Gothic, sans-serif !important; text-decoration: none; text-shadow: none; font-weight: 700 !important; font-size: 14px !important } a.kofi-button:visited { color: #fff !important; text-decoration: none !important } a.kofi-button:hover { opacity: .85; color: #f5f5f5 !important; text-decoration: none !important } a.kofi-button:active { color: #f5f5f5 !important; text-decoration: none !important } .kofitext img.kofiimg { height: 15px !important; width: 22px !important; display: initial; animation: kofi-wiggle 3s infinite; } @keyframes kofi-wiggle { 0% { transform: rotate(0) scale(1) } 60% { transform: rotate(0) scale(1) } 75% { transform: rotate(0) scale(1.12) } 80% { transform: rotate(0) scale(1.1) } 84% { transform: rotate(-10deg) scale(1.1) } 88% { transform: rotate(10deg) scale(1.1) } 92% { transform: rotate(-10deg) scale(1.1) } 96% { transform: rotate(10deg) scale(1.1) } 100% { transform: rotate(0) scale(1) } } </style> <link href="https://fonts.googleapis.com/css?family=Quicksand:400,700" rel="stylesheet" type="text/css"> <div class="btn-container"> <a href="https://ko-fi.com/U7U8BRF6G" title="Support me on ko-fi.com" class="kofi-button" style="background-color: #29abe0; cursor: pointer;" target="_blank"> <span class="kofitext"> <img src="https://storage.ko-fi.com/cdn/cup-border.png" alt="Ko-fi donations" class="kofiimg">Support Me on Ko-fi</span> </div></a></span> |
You'll need to change a few things in the code to customise the button for your site. They're all in the bottom chunk (after the closing </style> tag):
Note that the code snippet still references images hosted externally on the Ko-fi site, as well as Google Fonts. But crucially, no JavaScript and no document.write()!
Once you've made the change to your site, run it through PageSpeed Insights again and you'll see that it no longer flags a problem with document.write(). You may also see a small increase in your performance score.