Behind the scenes
How I built this site
This portfolio is a small serverless web app on Cloudflare. I wrote it in plain HTML, CSS and JavaScript with no framework, so I can explain every part of it. It runs on Cloudflare's free plan, at my own domain, ajayhearn.uk, which is registered through Cloudflare too.
What happens when someone visits
- The browser asks Cloudflare for the page. Cloudflare Pages sends the HTML, CSS and JavaScript from its global network, so it loads quickly wherever the visitor is.
- The page's JavaScript then asks for
/api/content. A Pages Function (a small piece of server code running on Cloudflare) reads my content from Workers KV, Cloudflare's key-value database, and returns it as JSON. - The JavaScript builds each section from that content. All text is inserted as plain text, never as HTML, so nothing saved in the admin panel can inject code into the page.
- Images are served from
/api/images/…. Each image has a random ID that never changes, so browsers can cache them for a year.
How I edit the content
- The admin panel at
/adminis a form for every section. It means I can update the site from any device without changing code. - Login: my password is stored as an encrypted Cloudflare secret, not in the code. When I log in, the server gives my browser a cookie signed with HMAC-SHA256. It lasts 8 hours, and the browser keeps it out of reach of JavaScript (
HttpOnly,Secure,SameSite=Strict). - Saving: the server rebuilds my content from scratch using only the fields it knows about. It also checks lengths and links (only
https://), then stores the result in KV. Changes usually reach every Cloudflare location within a minute. - Link preview: apps like LinkedIn and Discord read a page's preview tags without running any JavaScript. So a Pages Function middleware fills in the preview title, description and image from KV before each page is sent. I can change the preview from the admin panel without touching the code.
- Images: the admin panel shrinks very large photos in the browser before uploading. The server checks each file's first bytes to confirm it really is a JPG, PNG, WebP or GIF, with a 5 MB limit. When I save, images I've removed are deleted from storage.
How the contact form works
The form sends the message to a Pages Function. The function checks it and posts it to a private channel in my Discord server using a Discord webhook. The webhook address is kept as an encrypted secret, so visitors never see it. Before anything is stored or sent, Cloudflare Turnstile checks that the visitor is a person, and the function confirms that result with Cloudflare. This means requests sent straight to the API without passing the check are refused. A hidden "honeypot" box also catches simple bots, and each connection can send 5 messages an hour. Messages can't ping anyone in the server.
Security
- Every admin request is checked by middleware before it runs. Without a valid login cookie, the server refuses it.
- Logging in needs three things: a Cloudflare Turnstile human check, my password, and (once switched on) a 6-digit code from an authenticator app on my phone. Each code only works once.
- After 10 wrong attempts, logins from that connection are blocked for 15 minutes, and every successful login sends an alert to my Discord, so I'd know if anyone else got in.
- The admin page sends strict security headers: it can't be shown inside another website, and the browser only runs code from my site and Cloudflare.
- Passwords and cookie signatures are compared in a way that doesn't leak timing information.
- The admin and API only accept JSON or image uploads, which stops other websites from submitting forms on my behalf.
- Everything is served over HTTPS, and no secrets are stored in the code.
Accessibility
- The site scores 100 in Google Lighthouse's accessibility check, in light and dark mode, on desktop and mobile.
- The accessibility menu changes text size and turns on high contrast, an easy-read font (Atkinson Hyperlegible), wider spacing and underlined links. It can also stop animations, and visitors who have turned animations off on their device get that automatically.
- It works with a keyboard and screen readers. There's a skip link and visible focus outlines, and every image and link has a name screen readers can read out.
How I deploy changes
- I test on my Mac with
npm run dev, which runs the same Cloudflare runtime locally. - I put changes live with
npm run deploy, which uses Wrangler, Cloudflare's command-line tool, to upload the site. - The code and its full history are kept in a private GitHub repository. Content I change in the admin panel is stored in KV, separate from the code.