Skip to content

Commit 578a3ae

Browse files
Added redirects middleware (#880)
<!-- Explain the changes introduced in your PR --> ## Pull Request approval You will need to get your PR approved by at least one member of the Sourcegraph team. For reviews of docs formatting, styles, and component usage, please tag the docs team via the #docs Slack channel. --------- Co-authored-by: Maedah Batool <[email protected]>
1 parent 4076bea commit 578a3ae

File tree

5 files changed

+133
-31
lines changed

5 files changed

+133
-31
lines changed
File renamed without changes.

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/data/navigation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const navigation: NavigationItem[] = [
5555
subsections: [
5656
{ title: "Chat", href: "/cody/capabilities/chat", },
5757
{ title: "Autocomplete", href: "/cody/capabilities/autocomplete", },
58-
{ title: "Prompts", href: "/cody/capabilities/commands", },
58+
{ title: "Prompts", href: "/cody/capabilities/prompts", },
5959
{ title: "OpenCtx", href: "/cody/capabilities/openctx", },
6060
{ title: "Debug Code", href: "/cody/capabilities/debug-code", },
6161
{ title: "Context Filters", href: "/cody/capabilities/ignore-context", },

src/data/redirects.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6717,6 +6717,20 @@ const redirectsData = [
67176717
destination: "/code-search/code-navigation/inference_configuration",
67186718
permanent: true
67196719
},
6720+
// Model Config docs
6721+
{
6722+
source: "/cody/clients/model-configuration",
6723+
destination: "/cody/enterprise/model-configuration",
6724+
permanent: true
6725+
},
6726+
6727+
//Commands redirects
6728+
{
6729+
source: "/cody/capabilities/commands",
6730+
destination: "/cody/capabilities/prompts",
6731+
permanent: true
6732+
},
6733+
67206734

67216735
];
67226736

src/middleware.ts

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

0 commit comments

Comments
 (0)