Replies: 3 comments 3 replies
|
Global request middleware context doesn't flow into route The idea is: // routes/__root.tsx
export const Route = createRootRouteWithContext<{ locale: string; messages: Record<string, string> }>()({
beforeLoad: async () => {
const locale = getLocale() // createIsomorphicFn: server reads request URL, client reads window.location
const { messages } = await loadTranslations({ data: locale })
return { locale, messages }
},
component: RootComponent,
})Because The middleware approach would be cleaner if #6395 lands, but |
This comment was marked as spam.
This comment was marked as spam.
|
If locale is part of the URL, I’d strongly prefer modeling it in the route tree ( Then a good shape is:
That keeps things typed and works much better with SSR/static generation than string-parsing the request path in random places. If you need request-derived values before that, the current workaround is usually an isomorphic helper in root loading logic, but conceptually I’d still keep locale as a real route param so child routes inherit it naturally. |
Uh oh!
There was an error while loading. Please reload this page.
I need to do these things at root level:
langattribute of<html>element to resolved locale.Obviously non-root routes can't change the content of
<html>element nor pass any values to context providers, which are declared way higher in the tree than route pages.I tried to do something revolving around global middleware returning locale and translation resources to context in
src/start.ts, but the root loader doesn't pick on context signature change from that file.All reactions