-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserve-docs.ts
More file actions
67 lines (58 loc) · 1.79 KB
/
serve-docs.ts
File metadata and controls
67 lines (58 loc) · 1.79 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// serve.ts
import { readdirSync } from 'fs';
import { extname, join } from 'path';
const project = 'design-system';
const buildDir = `./dist/${project}/browser`;
const port = 3000;
console.log(`Serving static files from ${buildDir} on port ${port}`);
// A simple function to get the correct MIME type for a file
function getMimeType(filePath: string): string {
const extension = extname(filePath).toLowerCase();
const mimeTypes: { [key: string]: string } = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
};
return mimeTypes[extension] || 'application/octet-stream';
}
Bun.serve({
port,
async fetch(req) {
const url = new URL(req.url);
let filePath = join(buildDir, url.pathname);
// If the path is a directory, serve the index.html file
try {
const stats = await Bun.file(filePath).exists();
if (stats && readdirSync(filePath)) {
filePath = join(filePath, 'index.html');
}
} catch {
// It's not a directory, continue
}
const file = Bun.file(filePath);
const fileExists = await file.exists();
if (fileExists) {
return new Response(file, {
headers: { 'Content-Type': getMimeType(filePath) },
});
}
// Handle client-side routing (SPA fallback)
const indexFile = Bun.file(join(buildDir, 'index.html'));
if (await indexFile.exists()) {
return new Response(indexFile, {
headers: { 'Content-Type': 'text/html' },
});
}
return new Response('Not Found', { status: 404 });
},
error() {
return new Response('Not Found', { status: 404 });
},
});