Playing with Cloudflare Workers
I have a small Chrome extension I created for myself, that shows the daily Garfield comic strip every time I open a new tab. I’ll tell you another time how that works, but today…
I noticed that the strips are not loading. Turned out the page where I was loading them from was not updated for the past few days, so I had to search for another source. I found one really fast, but there was a problem.
My original source used a very convenient URL for the images:
Unfortunately the new source was a little less convenient:
Yeah… hash. But! The pages containing these URLs were date based, so now I had a new problem, how to turn a date, into a URL that is available on a page that is identifiable by that date? I need a… Transformation!
How do I a do that? Set up a server somewhere… bah no, too old, too complicated. Then set up a virtual machine… no, IaaS is still too complicated. Ok, then, just deploy my code with dependencies to some environment… no, no, no, PaaS is still too complicated. I just want my few lines of code somewhere, to do my stuff, like… FaaS!!!
That’s it, Function as a Service! I recently noticed (was bombarded like constantly in newsletters with) Cloudflare Workers. The free tier provides up to 30 workers and 100,000 requests per day on a domain ending with workers.dev. Ok, let’s take this one for a ride.
After signing up, I had to choose a worker subdomain, something.workers.dev. Think this through, you won’t be able to change this later. After that my first worker was created with some random-name-12345, and I landed in the editor.
It took me no more than an hour to figure out what I want and write my 8 lines of code, that transforms the date parameter in the query string into the URL, load the page, regex match the image URL and make a redirection. So now if I call:
it is immediately redirected to that hashhashhash URL what I mentioned in the beginning.
Just what I wanted.