scoresaber-reloaded-v2/src/middleware.ts
Liam 171dfd9cc0
All checks were successful
deploy / deploy (push) Successful in 53s
make urls look less awful (remove query params)
2023-10-24 12:56:28 +01:00

32 lines
840 B
TypeScript

import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const cookies = request.cookies;
const playerIdCookie = cookies.get("playerId");
if (pathname == "/") {
if (playerIdCookie) {
return NextResponse.redirect(
new URL(`/player/${playerIdCookie.value}/top/1`, request.url),
);
} else {
return NextResponse.redirect(new URL("/search", request.url));
}
}
const requestHeaders = new Headers(request.headers);
requestHeaders.set("x-url", request.url);
return NextResponse.next({
request: {
// New request headers
headers: requestHeaders,
},
});
}
export const config = {
matcher: "/((?!api|_next/static|_next/image|favicon.ico).*)",
};