Describe your question/issue in detail
I implemented internationalization in Prismic following this guide, and everything is working fine except for one issue. When I navigate to any route that starts with a prefix listed in the matcher array, I encounter an application error in production, and in development, I'm seeing the following error:
middleware.ts file
import { NextRequest, NextResponse } from "next/server";
import { prismicClient } from "@/lib/prismic";
export async function middleware(request: NextRequest) {
const repository = await prismicClient.getRepository();
const locales = repository.languages.map((lang) => lang.id);
const defaultLocale = repository.languages.find((lang) => lang.is_master);
// Check if there is any supported locale in the pathname
const { pathname } = request.nextUrl;
const pathnameIsMissingLocale = locales.every(
(locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
);
// Redirect to default locale if there is no supported locale prefix
if (pathnameIsMissingLocale) {
return NextResponse.rewrite(
new URL(`/${defaultLocale.id}${pathname}`, request.url)
);
}
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - robots.txt (robots file)
*/
"/((?!api|_next/static|_next/image|favicon.ico|slice-simulator|icon.svg|robots.txt|sitemap.xml).*)",
],
};
Prismic project
https://adamdotai-website-v4.prismic.io/masks/
Working website
Steps to reproduce
Enter any URL like the following examples:
https://adamdotai.vercel.app/api-sdfsdfsd
https://adamdotai.vercel.app/apisdfsdfsd
https://adamdotai.vercel.app/sitemap.xml
https://adamdotai.vercel.app/robots.txt
https://adamdotai.vercel.app/favicon.ico
https://adamdotai.vercel.app/favicon.icosfsg