My learning diary

Cloudflare Workers

It’s time to put my short custom domain to good use. Cloudflare Workers is a quick ’n’ dirty way for me to do URL forwarding. With URL forwarding, I will no longer need to set up DNS records or page rules. The former requires domain verification. Plus, social media websites do not support custom domains (why will they). The latter costs money (the first 3 page rules are free).

My worker’s name was "vanity" for obvious reasons and it sits at* and*. Without further ado, here’s my worker:

const defaultPath = 'blog';
const regex = /^(?:https:\/\/)(?:www\.)?(?:jiay\.ee\/)([A-Za-z0-9]+[A-Za-z0-9-_]*[A-Za-z0-9]+)$/i;
const links = {
  [defaultPath]: '',
  fb: '',
  gh: '',

addEventListener('fetch', event => {

async function handleRequest(request) {
  const exec = await regex.exec(request.url);
  const path = await exec ? exec[1].toLowerCase() : defaultPath;
  return Response.redirect(links[path] || links[defaultPath], 302);

So goes to my Steam profile. Give it a try. Last but not least, if you find your worker still stuck at an old version, clear your browser cache.

Relevant posts