-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbs-config.js
More file actions
48 lines (45 loc) · 1.5 KB
/
bs-config.js
File metadata and controls
48 lines (45 loc) · 1.5 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
module.exports = {
server: {
baseDir: "_site",
routes: {
"/404": "_site/404.html"
}
},
files: "_site",
port: 8080,
notify: false,
open: false,
// Custom middleware to handle 404 errors
middleware: [
{
route: "",
handle: function (req, res, next) {
// If the request is for a file that doesn't exist and it's not an asset
if (!req.url.includes('.') || req.url.endsWith('.html')) {
const fs = require('fs');
const path = require('path');
// Check if the requested file exists
const filePath = path.join('_site', req.url);
const indexPath = path.join('_site', req.url, 'index.html');
const htmlPath = req.url.endsWith('.html') ? filePath : filePath + '.html';
// If none of the possible paths exist, serve 404
if (!fs.existsSync(filePath) &&
!fs.existsSync(indexPath) &&
!fs.existsSync(htmlPath)) {
// Only serve 404 for HTML requests (not assets)
const accepts = req.headers.accept || '';
if (accepts.includes('text/html')) {
const notFoundPath = path.join('_site', '404.html');
if (fs.existsSync(notFoundPath)) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/html');
return res.end(fs.readFileSync(notFoundPath));
}
}
}
}
next();
}
}
]
};