Skip to content

Commit 7c0762b

Browse files
committed
docs: redirect /docs/* to root routes
1 parent 98e3a66 commit 7c0762b

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

.vitepress/config.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,35 @@ export default defineConfig({
1212
{
1313
name: 'aastar-docs-rewrite-docs-prefix',
1414
configureServer(server) {
15-
server.middlewares.use((req, _res, next) => {
16-
const url = req.url
15+
server.middlewares.use((req: any, res: any, next: any) => {
16+
const url: string | undefined = req.url
1717
if (!url) return next()
1818

1919
const [path, query] = url.split('?')
20-
if (path === '/docs' || path.startsWith('/docs/')) {
21-
const rewrittenPath = path === '/docs' ? '/' : path.slice('/docs'.length) || '/'
22-
req.url = query ? `${rewrittenPath}?${query}` : rewrittenPath
20+
if (path === '/docs' || path === '/docs/' || path.startsWith('/docs/')) {
21+
const redirectedPath = path === '/docs' || path === '/docs/' ? '/' : path.slice('/docs'.length) || '/'
22+
const location = query ? `${redirectedPath}?${query}` : redirectedPath
23+
res.statusCode = 302
24+
res.setHeader('Location', location)
25+
res.end()
26+
return
2327
}
2428
next()
2529
})
2630
},
2731
configurePreviewServer(server) {
28-
server.middlewares.use((req, _res, next) => {
29-
const url = req.url
32+
server.middlewares.use((req: any, res: any, next: any) => {
33+
const url: string | undefined = req.url
3034
if (!url) return next()
3135

3236
const [path, query] = url.split('?')
33-
if (path === '/docs' || path.startsWith('/docs/')) {
34-
const rewrittenPath = path === '/docs' ? '/' : path.slice('/docs'.length) || '/'
35-
req.url = query ? `${rewrittenPath}?${query}` : rewrittenPath
37+
if (path === '/docs' || path === '/docs/' || path.startsWith('/docs/')) {
38+
const redirectedPath = path === '/docs' || path === '/docs/' ? '/' : path.slice('/docs'.length) || '/'
39+
const location = query ? `${redirectedPath}?${query}` : redirectedPath
40+
res.statusCode = 302
41+
res.setHeader('Location', location)
42+
res.end()
43+
return
3644
}
3745
next()
3846
})

0 commit comments

Comments
 (0)