T
TanStack6mo ago
optimistic-gold

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 };
},
},
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 };
},
},
6 Replies
ambitious-aqua
ambitious-aqua6mo ago
hi there! what kind of "reserved" paths do you mean here? any static file/folder in the public dir should not reach the route
optimistic-gold
optimistic-goldOP6mo ago
Oh yes, I had the public dir in the wrong location. Yesterday I often got some $lang params with _build but I can't reproduce it now. Thanks for your fast response 🙏 @Manuel Schiller I got the playback. If any error is thrown and the error overlay is displayed, then the Error: Invalid language: _build is thrown. Somehow the route is matched and the param is trying to get parsed.
ambitious-aqua
ambitious-aqua6mo ago
what kind of error is thrown here? and where? the initial one I mean that is causing this
optimistic-gold
optimistic-goldOP6mo ago
For example: [vite] Internal server error: /PATH_TO_PROJECT/app/components/theme.tsx: Export 'useTheme' is not defined. But also importing a package that is not installed
ambitious-aqua
ambitious-aqua6mo ago
well in such cases vite dev server is totally borked, so I wouldn't expect it to work properly so this is not a normal operation mode or an error that could be properly handled
optimistic-gold
optimistic-goldOP6mo ago
Okay, I don't mind it. I just got confused because of the favicon.ico error so I thought it's the same. Thanks again :⁠)

Did you find this page helpful?