I added a visitor counter to my website this morning using the Cloudflare KV store. Easy, fun, fast, and old-school cool 😎.
What’s cool about this is that my entire homepage is technically static. It’s all served from the edge using Cloudflare Pages. However, Pages lets me send fetch requests from the edge too during page load.
This isn’t an XHR request (the visitor doesn’t see the request in their browser), it’s sent directly from the server. That means I can authenticate my requests without having to be concerned about leaking api keys.
So when the page loads, I retrieve the current page view count from Cloudflare KV, and increment it using the Cloudflare API, then pass the result on to the frontend.