|
| 1 | +import { NextResponse } from 'next/server' |
| 2 | +import type { NextRequest } from 'next/server' |
| 3 | +import docsConfig from '../docs.config.js' |
| 4 | +const { updatedRedirectsData } = require('./data/redirects.ts'); |
| 5 | +function createRedirectUrl(request: NextRequest, destination: string, path: string): string { |
| 6 | + // Handle absolute URLs |
| 7 | + if (destination.startsWith('http')) { |
| 8 | + // Handle dynamic slug replacements |
| 9 | + if (destination.includes(':slug')) { |
| 10 | + const slugMatch = path.match(/[^/]+$/) |
| 11 | + const slug = slugMatch ? slugMatch[0] : '' |
| 12 | + destination = destination.replace(':slug*', slug) |
| 13 | + } |
| 14 | + // Handle version replacements |
| 15 | + if (destination.includes(':version')) { |
| 16 | + const versionMatch = path.match(/\d+\.\d+/) |
| 17 | + const version = versionMatch ? versionMatch[0] : '' |
| 18 | + destination = destination.replace(':version', version) |
| 19 | + } |
| 20 | + return destination |
| 21 | + } |
| 22 | + // Handle relative paths |
| 23 | + const basePath = '/docs' |
| 24 | + return destination.startsWith('/') ? |
| 25 | + `${request.nextUrl.origin}${basePath}${destination}` : |
| 26 | + `${request.nextUrl.origin}${basePath}/${destination}` |
| 27 | +} |
| 28 | + |
| 29 | +export function middleware(request: NextRequest) { |
| 30 | + const path = request.nextUrl.pathname |
| 31 | + const pathWithoutBase = path.replace('/docs', '') |
| 32 | + // Handle base redirects from redirects.ts |
| 33 | + const redirect = updatedRedirectsData.find((r: any) => r.source === pathWithoutBase) |
| 34 | + if (redirect) { |
| 35 | + return NextResponse.redirect(createRedirectUrl(request, redirect.destination, path)) |
| 36 | + } |
| 37 | + // Handle version without slug |
| 38 | + const versionOnlyMatch = pathWithoutBase.match(/^\/v\/(\d+\.\d+)$/) |
| 39 | + if (versionOnlyMatch) { |
| 40 | + return NextResponse.redirect(`https://${versionOnlyMatch[1]}.sourcegraph.com/`) |
| 41 | + } |
| 42 | + // Handle version-specific redirects |
| 43 | + if (pathWithoutBase.startsWith(`/v/${docsConfig.DOCS_LATEST_VERSION}/`)) { |
| 44 | + return NextResponse.redirect(createRedirectUrl( |
| 45 | + request, |
| 46 | + `https://sourcegraph.com/docs/:slug*`, |
| 47 | + pathWithoutBase |
| 48 | + )) |
| 49 | + } |
| 50 | + if (pathWithoutBase.startsWith(`/@${docsConfig.DOCS_LATEST_VERSION}/`)) { |
| 51 | + return NextResponse.redirect(createRedirectUrl( |
| 52 | + request, |
| 53 | + `https://sourcegraph.com/docs/:slug*`, |
| 54 | + pathWithoutBase |
| 55 | + )) |
| 56 | + } |
| 57 | + const versionMatch = pathWithoutBase.match(/^\/v\/(\d+\.\d+)\/(.*)/) |
| 58 | + if (versionMatch) { |
| 59 | + return NextResponse.redirect(createRedirectUrl( |
| 60 | + request, |
| 61 | + 'https://:version.sourcegraph.com/:slug*', |
| 62 | + pathWithoutBase |
| 63 | + )) |
| 64 | + } |
| 65 | + const atVersionMatch = pathWithoutBase.match(/^\/@(\d+\.\d+)\/(.*)/) |
| 66 | + if (atVersionMatch) { |
| 67 | + return NextResponse.redirect(createRedirectUrl( |
| 68 | + request, |
| 69 | + 'https://:version.sourcegraph.com/:slug*', |
| 70 | + pathWithoutBase |
| 71 | + )) |
| 72 | + } |
| 73 | + if (pathWithoutBase === '/changelog.rss') |
| 74 | + return NextResponse.redirect(createRedirectUrl(request, '/technical-changelog.rss', path)) |
| 75 | + return NextResponse.next() |
| 76 | +} |
| 77 | +export const config = { |
| 78 | + matcher: [ |
| 79 | + '/((?!api|_next/static|_next/image|assets|favicon.ico|sw.js).*)', |
| 80 | + ], |
| 81 | +} |
0 commit comments