Blog / How the internet works /
Cloudflare Workers explained simply
Most people who use Cloudflare know it as the thing that sits in front of a website and makes it faster and safer. Fewer know that it also lets you run your own small programs on its network. Those programs are called Workers, and once you see what they’re for, a lot of fiddly website problems get simpler.
The plain-words definition
A Cloudflare Worker is a small piece of JavaScript that runs on Cloudflare’s servers, close to your visitors, every time a request comes in. It can look at the request, change it, answer it directly or pass it on to your real website, and then adjust the reply on the way back.
Imagine a doorman at a building. Normally he just points everyone to the right floor. With a Worker, you give the doorman a set of instructions: “If someone asks for the old address, send them to the new one. If they come from this country, show them this page. If they don’t have a pass, turn them away.” All before anyone troubles the people working upstairs.
Where it runs, and why that matters
Cloudflare has data centres in a great many cities around the world. A Worker is deployed to all of them. When a visitor makes a request, the code runs in a location near that visitor, not in a single server in one country. This is what people mean by “the edge”: the edge of the network, close to the user.
Two benefits follow: lower delay, for the reasons I described in why ping matters for games and calls, and no server for you to look after. You don’t set up, patch or restart anything. You upload the code and it runs. That’s the “serverless” idea. There are still servers, of course. You just aren’t the one managing them.
What Workers are used for
Here are realistic examples for people who run sites:
- Redirects at scale. Thousands of old addresses mapped to new ones, handled without loading WordPress each time.
- Changing headers. Adding security headers or cache rules to responses, without touching the origin server.
- A/B tests and personalisation. Show different versions of a page depending on a cookie, country or device.
- Simple APIs. A small endpoint that returns data or talks to another service, without a full back end.
- Protecting things. Check a token or secret before a request reaches your site, or block specific patterns.
- Handling forms. Accept a form submission on a static site and forward it to an email or database service.
- Proxying and rewriting. Serve a blog from a subfolder of the main domain, even if it’s hosted elsewhere.
- Light image or content changes on the fly.
Several of these are what you’d otherwise need a plugin, a server or a paid service for. They pair well with the sort of site I described in static hosting: when a site needs no server, where the odd bit of server-side logic is the only missing piece.
What a Worker looks like
Here’s a tiny example that redirects one address to another:
export default {
async fetch(request) {
const url = new URL(request.url);
if (url.pathname === "/old-page") {
return Response.redirect("https://example.com/new-page", 301);
}
return fetch(request);
},
};
It checks the requested path. If it matches, it sends a permanent redirect. Otherwise it passes the request on to the normal website. That’s all there is to the shape: a function receives a request and returns a response.
You can write and test Workers in Cloudflare’s dashboard editor, or use their command-line tool for bigger projects.
What they are not
- Not a full server. Workers have limits on how long they run and how much memory they use, and they don’t behave like a traditional machine you can log into. They suit short, quick jobs.
- Not a replacement for your host. They sit in front of, or alongside, your site.
- Not magic for slow sites. If the real problem is a heavy theme or database, a Worker won’t fix it.
- Not risk-free. A bug in a Worker can break every request to your site, since it sits in the path. Test before you deploy to production, and keep a way to turn it off.
Cost
Cloudflare has offered a free tier for Workers with daily limits, and paid plans for more use. I won’t quote the numbers, because plans change. Check their pricing page for the current allowances before you build anything that depends on them.
When you do not need one
- You have one or two redirects: your CMS or a redirect plugin handles it.
- You’re not comfortable reading simple JavaScript, and there’s an existing plugin or Cloudflare rule that does the same job. Cloudflare’s rules features cover many common tasks, such as redirects and header changes, without code. I touched on these in Cloudflare settings that help speed and security.
- The problem is inside the application, not in the traffic in front of it.
A good rule: try the built-in rules first. Reach for a Worker when you need logic that rules can’t express.
Why a web admin should know about it
Even if you never write one, knowing Workers exist changes how you solve problems. “We can’t change the server” or “the platform doesn’t allow that” becomes “we can change it before it reaches the server”. For client sites on locked-down platforms, that’s a useful escape hatch.
It also explains some mysteries. If you find odd behaviour on a site using Cloudflare, such as redirects that aren’t in the CMS or headers you didn’t set, a Worker or a rule might be the cause. Check the dashboard.
Key takeaways
- A Worker is a small JavaScript program that runs on Cloudflare’s network near your visitors.
- It can redirect, rewrite, protect, personalise and answer requests without a server of your own.
- It suits quick, small jobs and pairs well with static sites.
- Try Cloudflare’s built-in rules first; use Workers for custom logic.
- Test carefully, because a Worker sits in the path of every request.
Related reading
Is there anything on your site you’ve put up with because “the platform can’t do that”? A Worker might.