forked from reactjs/react.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path[...path].ts
More file actions
47 lines (37 loc) · 1.24 KB
/
[...path].ts
File metadata and controls
47 lines (37 loc) · 1.24 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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import type {NextApiRequest, NextApiResponse} from 'next';
import {readContentFile} from '../../../utils/docs';
const FOOTER = `
---
## Sitemap
[Overview of all docs pages](/llms.txt)
`;
export default function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'GET') {
res.setHeader('Allow', 'GET');
return res.status(405).send('Method not allowed');
}
const pathSegments = req.query.path;
if (!pathSegments) {
return res.status(404).send('Not found');
}
const filePath = Array.isArray(pathSegments)
? pathSegments.join('/')
: pathSegments;
// Block /index.md URLs - use /foo.md instead of /foo/index.md
if (filePath.endsWith('/index') || filePath === 'index') {
return res.status(404).send('Not found');
}
const content = readContentFile(filePath);
if (content === null) {
return res.status(404).send('Not found');
}
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.setHeader('Cache-Control', 'public, max-age=3600');
return res.status(200).send(content + FOOTER);
}