Skip to content

Commit 865550e

Browse files
committed
refactor(organizations): mitigate incorrect bot url path
1 parent cb48245 commit 865550e

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/middleware.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,39 @@ import type { NextRequest } from 'next/server';
22
import { NextResponse } from 'next/server';
33

44
export const config = {
5-
matcher: '/_next/static/chunks/:path*',
5+
matcher: ['/_next/static/chunks/:path*', '/organizations/:slug/details'],
66
};
77

88
export function middleware(request: NextRequest) {
99
const url = request.nextUrl.clone();
10-
const originalPathname = url.pathname;
10+
const { pathname } = url;
1111

12-
const decodedPathParts = originalPathname.split('/').map((part) => {
13-
try {
14-
return decodeURIComponent(part);
15-
} catch {
16-
return part;
17-
}
18-
});
12+
// Handle organization details redirect
13+
const orgDetailsMatch = pathname.match(/^\/organizations\/([^/]+)\/details$/);
14+
if (orgDetailsMatch) {
15+
const slug = orgDetailsMatch[1];
16+
const newUrl = new URL(`/organizations/info/${slug}`, request.url);
17+
return NextResponse.redirect(newUrl, 308); // 308 Permanent Redirect
18+
}
1919

20-
const decodedPathname = decodedPathParts.join('/');
20+
// Static chunk decoding check
21+
if (pathname.startsWith('/_next/static/chunks/')) {
22+
const originalPathname = pathname;
23+
const decodedPathParts = originalPathname.split('/').map((part) => {
24+
try {
25+
return decodeURIComponent(part);
26+
} catch {
27+
return part;
28+
}
29+
});
30+
const decodedPathname = decodedPathParts.join('/');
2131

22-
if (decodedPathname === originalPathname) {
23-
return NextResponse.next();
32+
if (decodedPathname !== originalPathname) {
33+
url.pathname = decodedPathname;
34+
return NextResponse.rewrite(url);
35+
}
2436
}
2537

26-
url.pathname = decodedPathname;
27-
return NextResponse.rewrite(url);
38+
// Allow other requests to pass through
39+
return NextResponse.next();
2840
}

0 commit comments

Comments
 (0)