Skip to content

Commit

Permalink
fix: ヘッダーにx-urlを設定
Browse files Browse the repository at this point in the history
  • Loading branch information
yuto-trd committed Nov 15, 2024
1 parent 69b7c30 commit 8ccace9
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const getNegotiatedLanguage = (
};

export function middleware(request: NextRequest) {
const newRequest = request.clone();
const url = `${request.headers.get("x-forwarded-proto")}://${request.headers.get("x-forwarded-host")}${request.nextUrl.pathname}${request.nextUrl.search}`;
newRequest.headers.set('x-url', url);

const headers = {
'accept-language': request.headers.get('accept-language') ?? '',
Expand All @@ -19,7 +21,11 @@ export function middleware(request: NextRequest) {
const pathname = `${request.nextUrl.pathname}${request.nextUrl.search}`;

if (["/img", "/robots.txt", "/_next"].find(i => pathname.startsWith(i))) {
return NextResponse.next();
return NextResponse.next({
request: {
headers: newRequest.headers
}
});
}

const pathnameIsMissingLocale = availableLanguages.every(
Expand All @@ -33,11 +39,19 @@ export function middleware(request: NextRequest) {
);
} else {
const newPathname = `/${defaultLanguage}${pathname}`;
return NextResponse.rewrite(new URL(newPathname, request.url));
return NextResponse.rewrite(new URL(newPathname, request.url), {
request: {
headers: newRequest.headers
}
});
}
}

return NextResponse.next();
return NextResponse.next({
request: {
headers: newRequest.headers
}
});
}

export const config = {
Expand Down

0 comments on commit 8ccace9

Please sign in to comment.