Cloudflare Workers are the future! 🏗
Tags: javascript, webdev, serverless, showdev
Published: 2022-05-09 16:10:37

Ahoy! Just returned from a deep dive into exploring [Cloudflare Workers](https://workers.cloudflare.com/) and it was a great experience! I've been itching to give them another go as I've been following [Deno](https://deno.com/) and others from the sidelines for some time now.

At a high level, Cloudflare Workers enable traditionally complex, backend-intensive logic to be located closer to the frontend. This is _not_ just another "do it all in JS" framework though. Rather, it's a paradigm shift that challenges how full-stack applications are built and deployed.

## A simple blog [example](https://blog.bryce.io/) 📝

I love dev.to - the CMS/IDE and community are all wonderful. But as I write more content I also want a way to display and share it via my own website. This ended up being a perfect application for this framework: taking on a complex set of features with a low-code solution.

Here's what I managed to get my little project to do (easily!) using Cloudflare Workers:

 - Fetch up-to-date data from dev.to's [API](https://developers.forem.com/api)
 - Create simple article/detail routing layer
 - Render components using JSX on server
 - Deploy automatically to custom domain (using GitHub actions)

Here it is in action: [blog.bryce.io](https://blog.bryce.io/).

And here's the repo if you want to poke around the code:

{% embed https://github.com/brycedorn/blog no-readme %}

## A thriving ecosystem for worker tooling 🪚

I used [hono](https://github.com/honojs/hono) (a wrapper around [miniflare](https://miniflare.dev/)) to handle some of the boilerplate around request and routing logic. It's also refreshingly fast! 🔥

The project also fully supports TypeScript. [Nano JSX](https://nanojsx.io/) is an ultra-fast JSX library that works great with SSR (and written in TS). And [wrangler](https://github.com/cloudflare/wrangler) makes deployment dead-simple, and integrates with the [wrangler-action](https://github.com/cloudflare/wrangler-action) to automatically deploy upon any changes.

## Why this is cool 🪄

As Cloudflare Workers are an on-demand serverless resource, they only run when a request is made. ~~And since they run at the edge, this mean caching is baked-in!~~* At scale, this means lots of resources and compute time saved without any additional effort on the developer.

No servers = local development (and deploys) can happen without any spin-up time. My last deploy via GitHub Actions took [less than 30 seconds](https://github.com/brycedorn/blog/runs/6353417768?check_suite_focus=true) (and the `build` step took 0s 🦄). Hot reloads during development actually feel instantaneous too.

Next I want to experiment with more client interactivity but I had a wonderful time building this small static/SSR project! 😇

---

Anyways, I hope this doesn't go against dev.to's API guidelines (let me know otherwise). And here are a bunch of [worker examples](https://developers.cloudflare.com/workers/examples/) to explore if this piqued your interest. I'm looking forward to using them more in the future!

_*Edit 6/3/22: turns out this note about caching was not true. I wrote about manually adding edge caching [here](https://dev.to/bryce/using-workers-kv-to-build-an-edge-cached-blog-23fo)._