This is my folder structure and i have middleware
// Purpose: Adding locale and Redirect user to the correct locale based on the accept-language header
import { NextResponse } from "next/server";
import { match } from "@formatjs/intl-localematcher";
import Negotiator from "negotiator";
// Supported locales and default locale
let locales = ["en-US", "en-GB", "en-NZ", "en-AU", "en-CA", "fr-CA"];
let defaultLocale = "en-US";
function getLocale(request) {
let headers = { "accept-language": request.headers.get("accept-language") };
const negotiator = new Negotiator({ headers });
const languages = negotiator.languages();
// if header is not present, return default locale (en-US) and if headers give no match, return default locale
return languages.length > 0
? match(languages, locales, defaultLocale)
: defaultLocale;
}
// Middleware function to add locale to the pathname
// If the pathname already has locale, and it is supported, do nothing and return
// Else, add the default locale to the pathname and redirect the user to the correct locale
export function middleware(request) {
const { pathname } = request.nextUrl;
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(/${locale}/) || pathname === /${locale},
);
if (pathnameHasLocale) return;
const locale = getLocale(request);
request.nextUrl.pathname = /${locale}${pathname};
return NextResponse.redirect(request.nextUrl);
}
export const config = {
matcher: ["/((?!_next|sitemap|favicon).*)"],
};
like this but problem is when i direclty hit localhost:3000
its showing me 404