-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
43 lines (40 loc) · 1.51 KB
/
middleware.js
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
38
39
40
41
42
43
import {NextResponse} from 'next/server';
export const config = {
matcher: [
// Skip all files with URL extensions and internal paths (_next)
"/((?!.*\\..*|_next).*)",
],
}
export async function middleware(request) {
let requestHeaders = new Headers(request.headers);
const clientPathname = request.nextUrl.pathname;
const nextJsPathname = getNextJsPathname(request, requestHeaders)
let response;
console.log("Client path: " + clientPathname);
// Rewrite on server-side only if path changed
if (clientPathname !== nextJsPathname) {
console.log("Next.js rewrite path: " + nextJsPathname);
response = NextResponse.rewrite(new URL(nextJsPathname, request.url))
} else {
response = NextResponse.next();
}
return response;
}
/**
* Makes any needed adjustments from client path to server path so matches Next.js server routing syntax.
*/
function getNextJsPathname(request, requestHeaders) {
let nextJsPathname = request.nextUrl.pathname;
// Check for custom locale at start of URL with 2 characters without or without proceeding slashes e.g. /fr/
const localeRegex = /^\/?(\w{2,2})(?:\/|$)/;
const localeMatch = nextJsPathname.match(localeRegex);
let locale;
if (localeMatch) {
locale = localeMatch[1];
} else {
// No locale specified so adjust path to default locale for server-side processing only
locale = "en";
nextJsPathname = `/${locale}${nextJsPathname}`;
}
return nextJsPathname;
}