-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmiddleware.ts
37 lines (33 loc) · 999 Bytes
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { NextResponse } from 'next/server';
const excludedPaths: string[] = [
`/_next/static`,
`/badges`,
`/fonts`,
`/images`,
`/icons`,
`/img`,
`/logos`,
`/js`,
'/inxt-library',
`DPA.pdf`,
];
const Middleware = (res) => {
const isExcludedPath = excludedPaths.findIndex((path) => res.nextUrl.pathname.includes(path)) !== -1;
if (isExcludedPath) return NextResponse.next();
if (res.nextUrl.pathname !== res.nextUrl.pathname.toLowerCase() || res.nextUrl.pathname.includes('%20')) {
const url = res.nextUrl.clone();
if (url.pathname.includes('%20')) {
const replaced = decodeURIComponent(url.pathname).replace(/\s/, '-');
url.pathname = replaced.toLowerCase();
return NextResponse.redirect(decodeURIComponent(url));
} else {
url.pathname = url.pathname.toLowerCase();
return NextResponse.redirect(url);
}
}
return NextResponse.next();
};
export const config = {
matcher: ['/:lang/:path*'],
};
export default Middleware;