-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserve.js
61 lines (55 loc) · 2 KB
/
serve.js
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
const http = require('http');
const path = require('path');
const serveHandler = require('serve-handler');
const port = 3000;
const baseUrl = '/docs/';
const buildDir = 'build';
const outDir = path.resolve('.', buildDir);
const servingUrl = `http://localhost:${port}`;
// Based on `docusaurus serve`
// https://github.com/facebook/docusaurus/blob/v3.1.1/packages/docusaurus/src/commands/serve.ts#L27
const server = http.createServer((req, res) => {
// Automatically redirect requests to /baseUrl/
if (!req.url?.startsWith(baseUrl)) {
res.writeHead(302, {
Location: baseUrl,
});
res.end();
return;
}
// Remove baseUrl before calling serveHandler, because /baseUrl/ should
// serve /build/index.html, not /build/baseUrl/index.html (does not exist)
req.url = req.url.replace(baseUrl, '/');
// HACK: serve-handler doesn't know about baseUrl, add it back when redirecting
// https://github.com/vercel/serve-handler/blob/6.1.5/src/index.js#L586-L588
// https://github.com/facebook/docusaurus/issues/7991
const originalWriteHead = res.writeHead;
res.writeHead = function (statusCode, statusMessage, headers) {
if (typeof statusMessage !== 'string') {
headers ??= statusMessage;
statusMessage = undefined;
}
if (statusCode >= 300 && statusCode < 400) {
const location = headers['Location'];
if (location.startsWith('/') && !location.startsWith(baseUrl)) {
headers['Location'] = location.replace('/', baseUrl);
}
}
return originalWriteHead.call(this, statusCode, statusMessage, headers);
};
return serveHandler(req, res, {
cleanUrls: [
// Clean URLs ending in /index.html or /index
'/**/index.html',
'/**/index',
// Don't clean URLs ending in /some-page.html, since that breaks our demos
'!/**/*.html',
],
public: outDir,
trailingSlash: true,
directoryListing: false,
});
});
const url = servingUrl + baseUrl;
console.log(`Serving path=${buildDir} directory at: url=${url}`);
server.listen(port);