Skip to content

Commit 59debcb

Browse files
committed
Added redirects middleware
1 parent d496197 commit 59debcb

File tree

2 files changed

+111
-30
lines changed

2 files changed

+111
-30
lines changed

next.config.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,36 @@ const nextConfig = {
1414
// https://vercel.com/docs/projects/environment-variables/system-environment-variables
1515
// basePath: process.env.VERCEL_ENV === 'production' ? '/docs' : '',
1616
basePath: '/docs',
17-
async redirects() {
18-
return [
19-
...updatedRedirectsData,
20-
{
21-
source: `/v/${config.DOCS_LATEST_VERSION}/:slug*`,
22-
destination: `https://sourcegraph.com/docs/:slug*`,
23-
permanent: false
24-
},
25-
{
26-
source: `/@${config.DOCS_LATEST_VERSION}/:slug*`,
27-
destination: `https://sourcegraph.com/docs/:slug*`,
28-
permanent: false
29-
},
30-
{
31-
source: '/v/:version(\\d+\\.\\d+)/:slug*',
32-
destination: 'https://:version.sourcegraph.com/:slug*',
33-
permanent: true
34-
},
35-
{
36-
source: '/@:version(\\d+\\.\\d+)/:slug*',
37-
destination: 'https://:version.sourcegraph.com/:slug*',
38-
permanent: true
39-
},
40-
{
41-
source: '/changelog.rss',
42-
destination: '/technical-changelog.rss',
43-
permanent: true
44-
}
45-
];
46-
}
17+
// async redirects() {
18+
// return [
19+
// ...updatedRedirectsData,
20+
// {
21+
// source: `/v/${config.DOCS_LATEST_VERSION}/:slug*`,
22+
// destination: `https://sourcegraph.com/docs/:slug*`,
23+
// permanent: false
24+
// },
25+
// {
26+
// source: `/@${config.DOCS_LATEST_VERSION}/:slug*`,
27+
// destination: `https://sourcegraph.com/docs/:slug*`,
28+
// permanent: false
29+
// },
30+
// {
31+
// source: '/v/:version(\\d+\\.\\d+)/:slug*',
32+
// destination: 'https://:version.sourcegraph.com/:slug*',
33+
// permanent: true
34+
// },
35+
// {
36+
// source: '/@:version(\\d+\\.\\d+)/:slug*',
37+
// destination: 'https://:version.sourcegraph.com/:slug*',
38+
// permanent: true
39+
// },
40+
// {
41+
// source: '/changelog.rss',
42+
// destination: '/technical-changelog.rss',
43+
// permanent: true
44+
// }
45+
// ];
46+
// }
4747
};
4848

4949
module.exports = async () => {

src/middleware.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)