Error: Some(api) is not a valid language code

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

Hi @anasr,

Welcome to the community!

Thank you for the details of your code.

I think your matcher may not be working the way you intend it to - as is, the pages/routes that will be excluded have to match exactly what you've included here: "/((?!api|_next/static|_next/image|favicon.ico|slice-simulator|icon.svg|robots.txt|sitemap.xml).*)", which would explain why /apidf, or /favicon.icosfsg (or anything else that starts with what you've put in the matcher and continues) returns an error.

To fix it, you'd have to add [^/]* in your regular expression to include the paths that start with these patterns and are followed by additional segments. Like this: /((?!api[^/]*|_next/static|_next/image|favicon[^/]*|slice-simulator|icon.svg|robots.txt|sitemap.xml).*)

/robots.txt works as intended for me. Can you confirm this?

/api and /sitemap.xml seem to have different issues.
For sitemap.xml, make sure this file actually exists and is accessible in the expected location. Are you using a specific tool or plugin to generate it?
For /api, do you have a route that matches this? What does your file structure look like?

Let me know how this goes and if you have any updates after trying this :slight_smile: