daytona/apps/docs/src/middleware.ts
Juraj Štefanić ff9a69b831
feat(docs): redirect sitemap.xml to sitemap-index.xml (#4289)
Signed-off-by: Juraj Štefanić <jurajstefanic@outlook.com>
2026-03-31 16:51:08 +02:00

27 lines
766 B
TypeScript

import { defineMiddleware } from 'astro:middleware'
import { redirects } from './utils/redirects'
export const onRequest = defineMiddleware(({ request, redirect }, next) => {
const url = new URL(request.url)
const path = url.pathname.replace(/\/$/, '')
if (path === '/docs/sitemap.xml') {
return next(new Request(new URL('/docs/sitemap-index.xml', url), request))
}
// Match /docs/old-slug or /docs/{locale}/old-slug
const match = path.match(/^\/docs(?:\/([a-z]{2}))?\/(.+)$/)
if (match) {
const locale = match[1]
const slug = match[2]
const newSlug = redirects[slug]
if (newSlug) {
const target = locale ? `/docs/${locale}/${newSlug}` : `/docs/${newSlug}`
return redirect(target, 301)
}
}
return next()
})