Preview Server
Introduction
After generating your site with bun ssg, the preview server serves the static output from dist/ so you can verify everything looks correct before deploying. It handles directory-style URLs (/about/ → about/index.html), detects language subdirectories, and serves assets with proper MIME types.
bun preview
This serves ./dist on the port from PORT in .env (the example .env ships PORT=3000); pass --port to override it.
CLI Options
bun scripts/preview.ts [--port 3000] [--dist ./dist]
| Flag | Alias | Default / Source | Description |
|---|---|---|---|
--port <n> | -p | PORT (env) | Port the server listens on |
--dist <dir> | --dir | ./dist | Output directory to serve |
--help | -h | - | Print usage and exit |
How It Works
The preview server (scripts/preview.ts) is a minimal Bun HTTP server that:
- Serves
dist/index.htmlat the root (/) - the default language homepage - Resolves directory paths -
/about/looks fordist/about/index.html - Falls back to
.htmlextensions -/en/abouttriesdist/en/about.html - Serves any other file directly - CSS, JS, images, fonts with correct MIME types
- Returns 404 for anything that doesn't exist
Language Detection
At startup, the preview server scans the dist/ directory for subdirectories whose names match a configured language code in config/supported_languages.ts (e.g., en/, sl/, de/). Matching against the configured list - rather than any two-character directory - means content directories that happen to be two characters long (such as js/) are never mistaken for a language. It lists the detected languages in the startup log:
🖥️ Preview server ready at http://localhost:3000/
📂 Serving: ./dist
🌐 Languages: sl (default), en
The server doesn't apply language-specific logic - it simply serves files from the path the browser requests. If you navigate to /en/about/, it serves dist/en/about/index.html directly.
Production vs Development
The preview server is meant for verifying the built output - it's not a development server. The key differences:
Dev Server (bun dev) | Preview Server (bun preview) | |
|---|---|---|
| Renders templates | ✅ Live, on-demand | ❌ Static files only |
| Live reload | ✅ WebSocket | ❌ |
| File watching | ✅ Auto-refresh | ❌ |
| Requires SSG | ❌ | ✅ Must run bun ssg first |
| Serves from | src/public/ + static/ | dist/ |
| Language routing | ✅ Full resolution | ✅ File-based |
MIME Types
The preview server maps file extensions to MIME types for correct Content-Type headers:
| Extension | Content-Type |
|---|---|
.html | text/html; charset=utf-8 |
.css | text/css; charset=utf-8 |
.js | application/javascript; charset=utf-8 |
.json | application/json; charset=utf-8 |
.svg | image/svg+xml |
.png | image/png |
.jpg, .jpeg | image/jpeg |
.gif | image/gif |
.webp | image/webp |
.ico | image/x-icon |
.woff2, .woff, .ttf | font/* |
.txt | text/plain; charset=utf-8 |
.xml | application/xml; charset=utf-8 |
| (other) | application/octet-stream |