Awesome
Demos for Cloudflare Workers
Assorted demos for doing things with Cloudflare Workers.
Examples are:
- Render a complete webpage at the edge
- A/B testing
- Check if the user is a bot or crawler and set that value as a header
- Check if the user is on a mobile device and set that value as a header
- Check the user's country and set that value as a header
What are edge functions/workers?
Workers let you do edge logic which means:
- You can do things with your code before even hitting any other servers
- It's very fast since the compute happens at one of Cloudflare's almost 200 data centers (probably very close to you!)
- Frankly you can do lots of things with it since it's just plain JavaScript!
- But my favorite: It gives you some of those "server"ful headers and things back to any of your serverless implementations
On Cloudflare, these serverless edge functions are also extremely fast (according to them, up to 50x faster). Having worked quite a bit with serverless across the big three providers, I can attest that Cloudflare Workers are the fastest you will get if you need serverless compute.
Use cases
I can imagine a LOT of use cases, but for my part I'm seeing these in particular:
- As a complete, serverless backend for low-compute purposes (push something to a database, add a user, send email...)
- As an extremely lightweight webserver
- As a way to proxy other, "real" webservers and giving them edge-side headers (like country, device and checking if they are a bot) before actually rendering them — still testing this out
Limitations
There's a few limitations as well:
- The free plan supports up to 10ms of compute; paid plan does 50ms; you don't have a lot of time to operate with
- If you use Serverless Framework, that plugin may still be wonky for the
.dev
sites (the ones you get out of the box)
How to use the code
You will have to have a Cloudflare account (free) and you can get the regular Workers plan for free as well. You receive a free {DOMAIN}.workers.dev
domain where the functions will be hosted.
You could use Serverless Framework to deploy code, but the absolutely easiest way is just to log in to Cloudflare and use the panels. Note that Safari is not fully supported, but Chrome works just fine, if you intend to use the inline coding and preview functionality.
For actually getting started, it's all really easy, and covered well in the docs.