I've been struggling to effectively implement NextJs's middleware for redirecting users from a specific country to another web domain. I've encountered some issues with my setup:
Main domain: https://www.example.com sub-domain:
src/middleware.js
import { NextResponse } from "next/server";
export function middleware(req) {
const { nextUrl: geo } = req;
const country = geo.country;
if (country === "CA") {
return NextResponse.redirect(new URL(`https://ca.example.com`));
}
}
The above approach hasn't yielded the desired results, and I suspect that my understanding of how middleware works in this context may be flawed. Any insights or guidance on this matter would be greatly welcomed!