← home

← About this site

How the code fits together

Note: This page was drafted by Claude.

The files I edit are not the files a visitor downloads. I edit source files, Astro builds them into plain web files, and Cloudflare serves those. Browsers only understand three languages: HTML for content, CSS for looks and JavaScript for behavior. Everything else in the project exists to produce those three.

The build runs on my computer when I preview the site, and on Cloudflare after every push to GitHub. The dashed lines are files that are passed along without being changed.

Are there any JavaScript files?

None in the source code, but the finished site has one. What the source does have is TypeScript (.ts) files: the Taipan game in src/lib/taipan/, the helper that lists blog posts in src/lib/posts.ts, and the blog settings in src/content.config.ts.

TypeScript is JavaScript with type labels added, such as "this is a date" or "this is text". The labels let mistakes get caught before anything goes live. Browsers cannot run TypeScript, so the build removes the labels and outputs plain JavaScript.

The one .js file in the finished site is the Taipan game. It is the only page that needs code running in the visitor's browser. Every other page arrives as finished HTML. Two tiny scripts for the light and dark theme button are also written directly into the shared layout.

astro.config.mjs is JavaScript too (.mjs means a modern JavaScript module), but it holds settings for the build and is not part of the site.

What a .astro file is

A .astro file is a template: HTML with a small code section at the top, fenced off by --- lines. This is a shortened version of the Projects page:

---
// Code: runs during the build only
import Base from '../layouts/Base.astro';
---

<!-- HTML: becomes the page -->
<Base title="Projects">
  <h1>Projects</h1>
  ...
</Base>

<!-- CSS for this page only -->
<style> .soon { ... } </style>

Three ideas cover almost everything:

  1. The code section runs once, during the build. Visitors never see or run it. Its job is to prepare data, such as finding the newest blog posts, and put it into the HTML.
  2. Pages are built from components. One .astro file can use another like a custom tag. <Base> is the shared layout with the header, theme button and footer, and every page wraps itself in it, so a change to the layout appears everywhere. The three-model comparison block on the video experiment pages is a smaller reusable component.
  3. Where a file sits becomes its URL. Anything in src/pages/ is a page.
FileURL
src/pages/index.astro/
src/pages/projects.astro/projects/
src/pages/gaming/taipan.astro/gaming/taipan/
src/pages/blog/[slug].astro/blog/<each post>/

The square brackets in [slug] mean "one page per item". Blog posts are plain Markdown files in src/content/blog/. src/content.config.ts says what every post must have (a title, a date and a description), and the [slug] page turns each post into its own page named after the file. Files whose names start with an underscore are skipped as pages and only pulled into other pages.

The other folders and files

What Wrangler does

Wrangler is Cloudflare's command-line tool, and wrangler.jsonc is its settings file. The settings that matter are a few lines:

// The Cloudflare Worker that hosts the site
"name": "danielwakabayashi-com",
"assets": {
  // Serve the files Astro built
  "directory": "./dist",
  // Unknown URLs get the 404 page
  "not_found_handling": "404-page"
}

When I push to GitHub, Cloudflare's build service pulls the code, installs the tools, runs the Astro build and uses Wrangler with these settings to upload dist/. After that, a visitor's request is answered with the matching pre-built file from a Cloudflare server near them.

So Wrangler mostly works behind the scenes and I never run it myself. I would run it by hand to deploy without pushing, to preview the site exactly as Cloudflare serves it, or later to add server features such as a database.

One change, start to finish

  1. Edit. I change src/pages/projects.astro and preview it with astro dev, which rebuilds pages as I save.
  2. Push. I commit the change and push it to GitHub.
  3. Build. Cloudflare sees the push and runs astro build, which writes dist/projects/index.html: plain HTML with the shared header and footer already filled in.
  4. Serve. Wrangler's settings tell Cloudflare to serve dist/, and a minute later the new page is live at /projects/.