-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview.ts
More file actions
43 lines (36 loc) · 1.13 KB
/
preview.ts
File metadata and controls
43 lines (36 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env bun
/**
* Serves the static `dist/` build locally for testing before deploying
* to Netlify / Vercel / S3. Plain file server, no processing.
*
* Usage: bun run preview
*/
const port = Number(process.argv[2]) || 3001
const distDir = 'dist'
Bun.serve({
port,
async fetch(req) {
const url = new URL(req.url)
let pathname = url.pathname
// Map directory roots and extensionless routes to .html files
// (Netlify's Pretty URLs mode does the same: /about → /about.html)
if (pathname === '/' || pathname === '') {
pathname = '/index.html'
}
else if (!pathname.includes('.')) {
pathname = pathname.replace(/\/$/, '') + '.html'
}
const filePath = `${distDir}${pathname}`
const file = Bun.file(filePath)
if (await file.exists()) {
return new Response(file)
}
// Fallback to 404.html
const notFound = Bun.file(`${distDir}/404.html`)
if (await notFound.exists()) {
return new Response(notFound, { status: 404 })
}
return new Response('Not Found', { status: 404 })
},
})
console.log(`[preview] serving ./${distDir} at http://localhost:${port}`)