TanStackT
TanStack•10mo ago•
14 replies
moderate-tomato

Best way to restrict i18n route params?

Hey, I'm exploring to migrate from Next.js :⁠)
How do I restrict my $lang route param to only allow valid values of the languages I support? Right now I tried something like this but I would have to maintain a list of reserved paths.

Also favicon.ico etc trigger this param which makes this error prone and annoying to maintain šŸ¤”

params: {
    parse: (params) => {
      if (params.lang === "_build") {
        return { lang: params.lang as Language }; // Type-cast is needed for type inference to work properly
      }

      if (!languages.includes(params.lang as Language)) {
        throw new Error(`Invalid language: ${params.lang}`);
      }

      return { lang: params.lang as Language };
    },
  },
Was this page helpful?