✍️ Blog

Personal site and portfolio. Astro 7 on Cloudflare Workers (SSR), React 19 islands, Directus, Tailwind CSS 4, Base UI, safe-mdx, Video.js, Mux, and Lanyard.
🚀 Install
git clone https://github.com/zpuckeridge/blog
cd blog
cp .env.example .env.local
bun install
bun run dev
Development opens at https://blog.localhost via Portless. For Astro’s default port, use PORTLESS=0 bun run dev.
Local preview after a production build:
bun run preview
Environment variables
Set these in .env.local (and matching Worker secrets in production):
| Variable |
Purpose |
| DIRECTUS_URL |
Directus instance URL (required for content) |
| DIRECTUS_TOKEN |
Read-only static token (required; public collection read is disabled) |
| PUBLIC_SITE_URL |
Canonical URL (e.g. https://zacchary.me) for RSS, sitemap, and metadata |
| LOOPS_API_KEY |
Newsletter signups via Loops |
| GITHUB_TOKEN |
GitHub contributions graph |
| VIDEO_PASSWORD |
Password gate for /videos and /video/* |
| LOCATION_WEBHOOK_SECRET |
Authenticates iOS Shortcuts location webhooks (/api/location) |
| RHYTHMS_WEBHOOK_SECRET |
Bearer token for POST /api/rhythms (optional; falls back to location) |
| APPLE_MUSIC_TEAM_ID |
Apple Music API team id for developer token signing |
| APPLE_MUSIC_KEY_ID |
Apple Music API key id for developer token signing |
| APPLE_MUSIC_PRIVATE_KEY |
Apple Music API private key (.p8 PEM contents, including headers) |
| APPLE_MUSIC_USER_TOKEN |
Optional dev-only Music User Token (production stores token in KV as apple-music:user-token) |
| PLAYLIST_SYNC_ADMIN_SECRET |
Bearer token for /api/playlist-sync admin routes |
| SPOTIFY_CLIENT_ID |
Spotify application client id for playlist sync |
| SPOTIFY_CLIENT_SECRET |
Spotify application client secret for playlist sync |
| GOOGLE_CLIENT_ID |
Google OAuth client id for YouTube Music playlist sync |
| GOOGLE_CLIENT_SECRET |
Google OAuth client secret for YouTube Music playlist sync |
For astro dev / wrangler dev, put VIDEO_PASSWORD and LOCATION_WEBHOOK_SECRET in .dev.vars as well (gitignored).
🚩 Features
Content
- Directus CMS — Posts, notes, videos, projects, books, movies, credits, uses
- Reading log — Articles I’ve read, kept in
src/lib/read-articles.ts and shown on Activity with books, films, and credits
- Timeline — Posts and short notes in one list; filter notes, search titles, filter by tag
- Tag pages —
/timeline/tag/[tag]
- MDX (safe-mdx) — Side notes, inline definitions, GFM footnotes, Tweet embeds, YouTube facade
- Related posts — Links at the bottom of each article
- Newsletter — Loops integration on posts
- Videos — Video.js v10 player for Mux video, with optional site-wide password protection
- Images — Directus assets via
/img/* with Cloudflare Images resizing
- Image zoom — Click-to-zoom on content images (navigation)
Presence and listening
- Location status — iOS Shortcuts webhook + KV geofence; shown on the homepage
- Discord status — Lanyard (REST + WebSocket on the client)
- Apple Music — Now playing comes from Discord activity via Lanyard. Play history and the homepage recent list come from the half-hourly Apple Music poll, using the Music User Token in KV (
apple-music:user-token). Stored plays keep a source field (lanyard for older rows, apple for the poll). Apple Music user tokens expire after roughly six months; restore the removed /music/connect flow from git history, re-authorise once, then remove it again—or set APPLE_MUSIC_USER_TOKEN locally and write KV via Wrangler.
- Music history —
/music shows monthly stats and up to 1,000 plays for the selected month; the full log and monthly aggregates (listening:stats in KV) keep every play. Genres come from Apple’s API on ingest. Backfill: POST /api/listening/backfill with batched offset= (Bearer RHYTHMS_WEBHOOK_SECRET); use ?rebuild=stats to rebuild stats only.
- Playlist sync — One-way mirror of the Apple Music library to private Spotify and YouTube Music playlists. Apple Music is the source of truth. The default mode lists every library playlist. A cron runs every six hours (
0 */6 * * *) and syncs a small batch each time. Manual run and status live at /api/playlist-sync and require Authorization: Bearer PLAYLIST_SYNC_ADMIN_SECRET. Refresh tokens are stored in LOCATION_KV and are never written to the repo. Full workflow: docs/playlist-sync.md.
- Status history heatmaps — Full work, home, and Discord history in KV;
/activity charts the last 30 Brisbane days
Activity
- Live activity feed — Anonymous visitor stream (paths, titles, coarse client label, city-level location); 24-hour retention in a Durable Object (
ActivityFeedStore)
- Listening in the feed — Site listens (track clicks) appear as
listened events alongside page views
Article UX
- Table of contents — Per-article navigation
- Copy link — Share article URLs
- Time to read — Estimated reading time
Site
- RSS —
/rss.xml
- Sitemap & robots — Generated routes
- llms.txt —
/llms.txt for crawlers and LLMs
- Carbon rating — Website Carbon badge (colophon)
- Theme toggle — Light/dark on the homepage;
m opens the nav menu (desktop menu / mobile drawer)
- GitHub contributions — Homepage graph
- Security headers — CSP and related headers on HTML responses
Status history heatmaps
A Cloudflare cron runs every minute. On a five-minute sample bucket it records location and Discord presence into LOCATION_KV under status:history:v1. Graphs live on /activity (not the homepage). A second cron runs every 30 minutes and records listening history from the Apple Music API when a Music User Token is stored. Lanyard is only the live now-playing presence.
- Discord online counts
online, idle, and dnd; offline is not counted.
- Work / home use the current geofence category when location is fresh (not stale).
transit and away count toward observed location time but not work or home totals.
- Day boundaries use
Australia/Brisbane.
- Retention is 365 days. Gaps between samples are capped at 15 minutes so outages do not inflate history.
- No backfill — history starts when this feature is deployed.
A third cron runs every six hours and mirrors Apple Music library playlists. See docs/playlist-sync.md.
Playlist sync
Apple Music stays the source of truth. Spotify is matched by ISRC, then by title, artist, and duration. YouTube Music has no ISRC lookup, so it matches on title, artist, and duration through the official YouTube Data API v3. Matches are cached in LOCATION_KV. Tracks that cannot be matched are listed on the status route and left out of the copy. A source playlist that fails to match every track does not wipe the target. Mirrors are private and are never deleted.
The steps below are the short version. Setup, the batching cursor, quota, and troubleshooting are in docs/playlist-sync.md.
Secrets
Set these with wrangler secret put (and in .dev.vars or .env.local for local dev). Do not commit the values.
wrangler secret put PLAYLIST_SYNC_ADMIN_SECRET
wrangler secret put SPOTIFY_CLIENT_ID
wrangler secret put SPOTIFY_CLIENT_SECRET
wrangler secret put GOOGLE_CLIENT_ID
wrangler secret put GOOGLE_CLIENT_SECRET
Apple Music signing secrets (APPLE_MUSIC_TEAM_ID, APPLE_MUSIC_KEY_ID, APPLE_MUSIC_PRIVATE_KEY) are reused. Library playlists also need the Music User Token already stored at apple-music:user-token.
Redirect URIs
Register these exactly. Authorise against https://zacchary.me. Preview deployments use a different KV namespace, so a code granted there is not visible to production.
| Service |
Redirect URI |
| Spotify |
https://zacchary.me/api/playlist-sync/spotify/callback |
| Google (YouTube Data API) |
https://zacchary.me/api/playlist-sync/youtube/callback |
Spotify scopes: playlist-read-private, playlist-modify-private, playlist-modify-public.
Google scope: https://www.googleapis.com/auth/youtube. Enable YouTube Data API v3, create a Web application client, and set the consent screen to External and Testing with the account as a test user. Internal fails with org_internal for Gmail accounts. Details are in docs/playlist-sync.md.
Connect once
curl -s -X POST \
-H "Authorization: Bearer $PLAYLIST_SYNC_ADMIN_SECRET" \
https://zacchary.me/api/playlist-sync/spotify/start
curl -s -X POST \
-H "Authorization: Bearer $PLAYLIST_SYNC_ADMIN_SECRET" \
https://zacchary.me/api/playlist-sync/youtube/start
Open the returned url, approve access, and stop when the page says the service is connected.
Config
playlistSyncMode in src/lib/playlist-sync/config.ts defaults to all-library, which mirrors every library playlist. Set it to configured to mirror only playlistSyncSources. excludeApplePlaylistIds skips Apple ids during discovery. A hand-listed source still wins.
export const playlistSyncSources = [
{
id: "late-night",
name: "Late night",
applePlaylistId: "p.yourLibraryPlaylistId",
},
];
applePlaylistId is a library id (p.…) or a catalogue id (pl.…). Leave the Spotify and YouTube ids unset to create private playlists on the first sync that has a matched track. Created ids are stored in KV. Set spotifyPlaylistId or youtubePlaylistId to reuse an existing playlist instead.
Run and inspect
curl -s -X POST \
-H "Authorization: Bearer $PLAYLIST_SYNC_ADMIN_SECRET" \
https://zacchary.me/api/playlist-sync/run
curl -s \
-H "Authorization: Bearer $PLAYLIST_SYNC_ADMIN_SECRET" \
https://zacchary.me/api/playlist-sync/status
A run with no body answers 202 and { "queued": true, "nextScheduledRun": "<ISO-8601>" }. The next cron (0 */6 * * *) syncs the next batch. The request does not sync on its own. Read queued, cursor, and lastResult from status. To queue one playlist without moving the cursor, post { "sourceId": "late-night" }. That answer is 202 as well.
YouTube quota
The default YouTube Data API project allows 100 search.list calls per day (a separate bucket; 1 unit each) and 10,000 units per day for other methods. videos.list, used once per search to read duration, costs 1 unit. Adding, removing, or reordering a playlist item costs 50 units. This sync keeps a local budget of 80 searches and 8,000 write units per Pacific day so one run cannot exhaust the project. Cached tracks do not spend a search. A large first mirror therefore takes more than one day, and YouTube order is applied only while write units remain (partial: true on the status report). Membership is preferred over a perfect reorder when the budget runs out.
Tracks with no catalogue ISRC, karaoke or tribute uploads, and titles that differ by more than ten seconds of duration are left unmatched.
📁 Structure
| Route |
Description |
/ |
Home — projects, recent articles, GitHub graph, location, Discord, now playing |
/timeline |
All posts and notes (search and tag filters) |
/timeline/[slug] |
Post (MDX) |
/timeline/tag/[tag] |
Posts for one tag |
/activity |
Status heatmaps, reading and credits, live visitor activity |
/music |
Apple Music listen history |
/videos, /video/[slug] |
Video index and player (password gate) |
/projects |
Project list |
/about, /about/articles, /about/books, /about/movies, /about/credits |
About, reading log, and lists |
/uses |
Tools and gear (Directus) |
/cv |
Résumé |
/colophon |
Stack, type, credits, and legal imprint |
/rss.xml, /sitemap.xml, /llms.txt |
Feeds and discovery |
/blog redirects to /timeline. /imprint redirects to /colophon#imprint.
The activity feed stores a sanitised path, title, approximate city-level location, timestamp, coarse browser/OS label, and optional listening metadata. Rows are keyed by a hashed cookie id plus User-Agent. It does not store raw IPs, full user agents, referrers, or query strings.
Deploy (Cloudflare Workers)
bun run deploy
predeploy runs Ultracite, React Doctor, TypeScript, and knip. The build writes to dist/; Wrangler serves assets and runs src/worker.ts (scheduled polls, Durable Objects, KV).
Configure KV (LOCATION_KV), Durable Objects (ActivityFeedStore), Images, cron triggers, and secrets in the Cloudflare dashboard or with wrangler secret put.
评论