-
I am using axum + leptos and the database pool is inside my let app_state = AppState { db_pool, leptos_options };
let (routes, static_routes) = generate_route_list_with_ssg({
let app_state = app_state.clone();
move || shell(app_state.clone())
});
// static_routes.generate(&app_state.leptos_options).await;
// Construct the axum router.
let app = Router::new()
.leptos_routes(&app_state, routes, App)
.nest("/api", api_routes())
.with_state(app_state); I want to pre-generate my static pages, since they rely on static database data that is populated once during a migration and never changed again. My page looks like this: pub fn shell(app_state: AppState) -> impl IntoView {
println!("{:?}", app_state);
let options = app_state.0.leptos_options.clone();
provide_context(app_state);
view! {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<AutoReload options=options.clone()/>
<HydrationScripts options/>
<MetaTags/>
</head>
<body>
<App/>
</body>
</html>
}
}
#[component]
fn App() -> impl IntoView {
view! {
<Router>
<nav>
</nav>
<main>
<Routes fallback=fallback>
<Route
path=path!("/path/:version")
view=Game
ssr=SsrMode::Static(
StaticRoute::new()
.prerender_params(|| async move {
[
(
"version".into(),
list_data().await.unwrap_or_default(),
),
]
.into_iter()
.collect()
}),
)
/>
</Routes>
</main>
</Router>
}
}
#[server]
async fn list_data() -> Result<Vec<String>, ServerFnError> {
let app_state = use_context::<AppState>().expect("AppState not found"); // line 76
let db = app_state.db_pool.get();
/// ...
Ok(results) This fails with the error message:
I can see that the states gets printed before the error appears, but the state is not available, even if I do the provide_context call. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This fails -- when? I would expect it to fail if the client ever calls the server function, because the context isn't provided for server function endpoints (server function endpoints don't call I might also expect it to fail during route list generation. I forget how exactly the shell function is used in SSG because it's been a long time since I've touched it, but there is a |
Beta Was this translation helpful? Give feedback.
This fails -- when?
I would expect it to fail if the client ever calls the server function, because the context isn't provided for server function endpoints (server function endpoints don't call
shell
when called from the client, because they're not generating an HTML page; use.leptos_routes_with_context
to provide it to both HTML pages and server function endpoints)I might also expect it to fail during route list generation. I forget how exactly the shell function is used in SSG because it's been a long time since I've touched it, but there is a
generate_route_list
variant that allows you to pass additional context.