— Cloudflare, Netlify, Cloudflare Workers, Firebase Functions — 4 min read
Cloudflare provides a generous free tier for many of their useful products. I list them below (and will keep them updated as I use more). Some products do have some downsides during development, even when working at small-scale. I'm happy to say I overcame them, and want you to help you do the same.
⚠️ Warning ⚠️: This page also includes random technical details to help you solve issues. ⚠️
.uk or .co.uk domain), instead of GoDaddy. Great domain prices, and it just works.Switching from Netlify to Cloudflare Pages brings 2 main benefits:
GoogleBot or not.Cloudflare workers might seem like the normal serverless functions we've heard about, like Vercel, Google/ Firebase Cloud Functions, AWS Lambda and Azure Functions are selling.
However, Cloudflare Workers' runtime is more restrictive. This might mean Cloudflare Workers are much cheaper and faster to run at scale, but I won't dig into performance today.
They are Javascript only, to run other languages you need to use WebAssembly. Other platforms allow you to run pretty much run anything: Google offers Cloud Run to run serverless containers, and Lambda allows custom runtimes to allow you to run anything you want.
Cloudflare workers runs as a Service Worker (in a V8 isolate), not as a NodeJS application or a browser application. Some libraries don't support this very well, and so this limits the use of Cloudflare Workers. For example, XMLHttpRequest doesn't work. This is different to other serverless products, which run in NodeJS. The reason why Cloudflare uses Service Workers and not NodeJS is to run in V8 isolates for "scale, security and speed". See Kenton Varda's talk for more.
Service Workers are specialised typed of Web Workers, specifically designed to allow intercepting network requests to act as a cache/ proxy. This allows offline use. Web Workers only have a smaller set of APIs, which won't allow you to intercept network requests, and is more designed to run Javascript off the main thread.
Whats the difference between a Service Worker and a Web Worker?
Web Worker: General work done off the main thread.
Service Worker: A special purpose runtime which is a proxy between the browser and the server. Service Workers have extra APIs for intercepting network requests. Because Cloudflare Workers intercept network requests that are incoming to a domain, they kind of fit the service worker model.
Webpack uses the same target (webworker) for both of them
From client/ browser apps, you pass messages to it using window.postMessage()
Billing experience: Cloudflare Functions do not bill when the CPU is not running: i.e. It is free when you're making a network request in the cloudflare worker and waiting for the promise to return. This is different to Google Cloud/ Firebase pricing, which will definitely be more expensive:
Compute time is measured from the time your function receives a request to the time it completes, either through you signaling completion, or through a timeout, other failure or any other termination.
The debugging experience is relatively poor: you cannot set breakpoints, run it locally, and the error messages are not that helpful. I recommend cfworker to improve the normal Cloudflare debugging experience though. My example repo which uses Ably in a Cloudflare Worker, including helpful ways to debug (e.g. cfworker) exists here.
wrangler dev might not give you any errors. You should first try to publish the worker using wrangler publish, fix the errors and confirm it works. Then wrangler dev will work.api.club2d.orth.uk doesn't get a valid SSL cert. Thanks The Freelancer ;) on Discord again. So I solved this by using the default worker domain, i.e. worker_name.your_worker_subdomain.workers.dev, which doesn't have this problem.wrangler.toml or the cloudflare website will be accessible as global variables, not through process.env.VAR_NAME, but just VAR_NAME. This is confusing, because if you're using typescript, you need to ignore the warning like so:1// @ts-ignore2console.warn(`api key is ${ABLY_PRIVATE_API_KEY}`);AAAA record to 100::, and that worked great. The Workers dashboard does not notify you of any of this, but the documentation does.Join the cloudflare discord to get for support from really helpful Cloudflare people. The community is very helpful, and they've saved me a lot of time in debugging my Cloudflare Workers. However, with other platforms, I hardly needed to debug anything.
Which Cloudflare products are you using or considering using? What other options are you looking at?