-
I guess something like this already exists and I just cannot find it. I am trying to set up server side translation for different languages. pub(crate) async fn leptos_routes_handler(
app_state: AppState,
State(options): State<LeptosOptions>,
req: Request<Body>,
) -> Response {
// some logic stuff up here
let app_fn = |cx| view! { cx, <App/> };
let t = I18nIndex::translate(&lang);
let handler = render_app_to_stream_with_context(
options.clone(),
move |cx| {
provide_context(cx, t.clone());
},
app_fn,
);
let (mut parts, body) = handler(req).await.into_response().into_parts()
// ... and then add some more custom headers an stuff... I can then extract this value inside the // `.unwrap_or_else(|| I18nIndex::default())` to not panic during initial leptos route generation in main
let t = use_context::<I18nIndex>(cx).unwrap_or_else(|| I18nIndex::default()); My only question now is, if I can somehow access this same pre-rendered context on the client later on and if so, how. My other idea was to just do it like now and on the client do another fetch for the same resource to initialize the context. edit: Usually, I would just use a server fn right away, but especially the i18n is a bit tricky so have it available on the very first render for SSO. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
I just found a part of the answer for the server fn question in the examples: The server fn will begin loading on the server and resolve on the client, which means it will not be available on the very first render. |
Beta Was this translation helpful? Give feedback.
If someone else might have a similar problem, I finally have a clean and nice setup for this problem.
I got into some trouble when it should work with routing too, but here is how I got it working:
<nav>
needs some special treatment here to make the client side routing flicker free and nice and pretty: