Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dev/check-links.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ function buildPathMap() {
};
}

// Source route -> destination of src/data/redirects.ts. The middleware uses the
// first rule whose source equals the requested path, so first entry wins here too.
// Source route -> destination of src/data/redirects.js. The first rule whose
// source equals the requested path wins here too.
function loadRedirects() {
const redirects = new Map();
const source = fs.readFileSync(path.join(ROOT_DIR, 'src/data/redirects.ts'), 'utf-8');
const source = fs.readFileSync(path.join(ROOT_DIR, 'src/data/redirects.js'), 'utf-8');
const ruleRegex = /source:\s*(['"])(.*?)\1,\s*destination:\s*(['"])(.*?)\3/gs;
for (const [, , from, , to] of source.matchAll(ruleRegex)) {
if (!redirects.has(from)) redirects.set(from, to);
Expand Down
30 changes: 29 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
const {withContentlayer} = require('next-contentlayer');
const {execSync} = require('child_process');
const {updatedRedirectsData} = require('./src/data/redirects.js');
/** @type {import('next').NextConfig} */

function createStaticRedirects() {
const seenSources = new Set();
const redirects = [];

for (const redirect of updatedRedirectsData) {
if (
redirect.source.includes('#') ||
redirect.source.includes('?') ||
seenSources.has(redirect.source)
) {
continue;
}

seenSources.add(redirect.source);
redirects.push({...redirect, permanent: false});
}

return redirects;
}

const staticRedirects = createStaticRedirects();
console.log(`Configured ${staticRedirects.length} static redirects`);

const nextConfig = {
reactStrictMode: true,
swcMinify: true,
Expand All @@ -15,7 +39,11 @@ const nextConfig = {
env: {
NEXT_PUBLIC_DOCS_BASE_PATH:
process.env.VERCEL_ENV === 'production' ? '/docs' : ''
}
},
redirects: async () => staticRedirects,
rewrites: async () => ({
beforeFiles: [{source: '/:path*.md', destination: '/api/md/:path*'}]
})
};

module.exports = async () => {
Expand Down
4 changes: 1 addition & 3 deletions src/data/redirects.ts → src/data/redirects.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {TECHNICAL_CHANGELOG_RSS_URL} from './constants';

const redirectsData = [
{
source: '/integration/img/disable_extension.png',
Expand Down Expand Up @@ -5839,7 +5837,7 @@ const redirectsData = [
// This redirect preserves existing RSS subscriptions
{
source: '/technical-changelog.rss',
destination: TECHNICAL_CHANGELOG_RSS_URL
destination: 'https://sourcegraph.com/changelog/technical-changelog.rss'
},
// Self-hosted update pages moved to /changelog/self-hosted/
{
Expand Down
22 changes: 2 additions & 20 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import docsConfig from '../docs.config.js';

import {TECHNICAL_CHANGELOG_RSS_URL} from './data/constants';

const {updatedRedirectsData} = require('./data/redirects.ts');
const {updatedRedirectsData} = require('./data/redirects.js');

function createRedirectUrl(
request: NextRequest,
Expand Down Expand Up @@ -59,24 +59,6 @@ export function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;
const pathWithoutBase = path.replace('/docs', '');

// Handle .md suffix - return raw markdown
if (pathWithoutBase.endsWith('.md')) {
const docPath = pathWithoutBase.replace(/\.md$/, '');
const url = request.nextUrl.clone();
url.pathname = `/api/md${docPath}`;
return NextResponse.rewrite(url);
}

// Handle base redirects from redirects.ts
const redirect = updatedRedirectsData.find(
(r: any) => r.source === pathWithoutBase
);
if (redirect) {
return NextResponse.redirect(
createRedirectUrl(request, redirect.destination, path)
);
}

// Handle latest version without path - redirect to main docs
const latestVersionOnlyMatch = pathWithoutBase.match(
`^\/(?:v\/|@)${docsConfig.DOCS_LATEST_VERSION}\/?$`
Expand Down Expand Up @@ -146,5 +128,5 @@ export function middleware(request: NextRequest) {
}

export const config = {
matcher: ['/((?!api/md|_next/static|_next/image|assets|favicon.ico|sw.js).*)']
matcher: ['/v/:path*', '/@:path*', '/changelog.rss']
};
Loading