Commit 45c9761d by Archer Committed by GitHub

redirect (#6885)

parent 0fac7c2b
...@@ -88,13 +88,15 @@ const prefixMap: Record<string, string> = { ...@@ -88,13 +88,15 @@ const prefixMap: Record<string, string> = {
const i18nMiddleware = createI18nMiddleware(i18n); const i18nMiddleware = createI18nMiddleware(i18n);
function applyRedirectMaps(path: string): string | null { function normalizePath(path: string) {
const normalizedPath = path.length > 1 ? path.replace(/\/+$/, '') : path; return path.length > 1 ? path.replace(/\/+$/, '') : path;
}
if (normalizedPath in exactMap) return exactMap[normalizedPath]; function applyRedirectMaps(path: string): string | null {
if (path in exactMap) return exactMap[path];
for (const [oldPrefix, newPrefix] of Object.entries(prefixMap)) { for (const [oldPrefix, newPrefix] of Object.entries(prefixMap)) {
if (normalizedPath.startsWith(oldPrefix)) { if (path.startsWith(oldPrefix)) {
return newPrefix + normalizedPath.slice(oldPrefix.length); return newPrefix + path.slice(oldPrefix.length);
} }
} }
return null; return null;
...@@ -126,7 +128,7 @@ export default function middleware(request: NextRequest) { ...@@ -126,7 +128,7 @@ export default function middleware(request: NextRequest) {
// Legacy /docs/* URLs: strip the /docs prefix and run redirect-match. // Legacy /docs/* URLs: strip the /docs prefix and run redirect-match.
// Always 301 to the canonical (no-/docs) path, even if no map entry matches. // Always 301 to the canonical (no-/docs) path, even if no map entry matches.
if (pathWithoutLang === '/docs' || pathWithoutLang.startsWith('/docs/')) { if (pathWithoutLang === '/docs' || pathWithoutLang.startsWith('/docs/')) {
const stripped = pathWithoutLang.slice('/docs'.length); // '' for /docs, /foo for /docs/foo const stripped = normalizePath(pathWithoutLang).slice('/docs'.length); // '' for /docs, /foo for /docs/foo
const mapped = applyRedirectMaps(stripped); const mapped = applyRedirectMaps(stripped);
const finalPath = mapped ?? stripped; const finalPath = mapped ?? stripped;
return NextResponse.redirect(createRedirectUrl(finalPath, request, lang), 301); return NextResponse.redirect(createRedirectUrl(finalPath, request, lang), 301);
...@@ -134,7 +136,8 @@ export default function middleware(request: NextRequest) { ...@@ -134,7 +136,8 @@ export default function middleware(request: NextRequest) {
// Non-/docs paths still consult the same maps so that direct hits on old // Non-/docs paths still consult the same maps so that direct hits on old
// canonical paths (e.g. /upgrading) get redirected to their new home. // canonical paths (e.g. /upgrading) get redirected to their new home.
const mapped = applyRedirectMaps(pathWithoutLang); const normalizedPath = normalizePath(pathWithoutLang);
const mapped = applyRedirectMaps(normalizedPath);
if (mapped !== null && mapped !== pathWithoutLang) { if (mapped !== null && mapped !== pathWithoutLang) {
return NextResponse.redirect(createRedirectUrl(mapped, request, lang), 301); return NextResponse.redirect(createRedirectUrl(mapped, request, lang), 301);
} }
......
...@@ -3,10 +3,11 @@ import type { NextConfig } from 'next'; ...@@ -3,10 +3,11 @@ import type { NextConfig } from 'next';
const withMDX = createMDX(); const withMDX = createMDX();
const config:NextConfig = { const config: NextConfig = {
output: 'standalone', output: 'standalone',
reactStrictMode: true, reactStrictMode: true,
compress: true, compress: true,
skipTrailingSlashRedirect: true,
async headers() { async headers() {
return [ return [
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment