Comments (5)
Not sure what the goal is during the example -- I see the panic and it is during initial route generation on the server. This kind of error would be suppressed during route generation using something like a resource. Is the idea here to run some async task, independent of the rendering process, but while rendering? There's no way to wait for the task you spawned to finish before rendering HTML.
from leptos.
Or of course you can just not unwrap in the places you're unwrapping; this version works fine:
#[server]
pub async fn get_user_data() -> Result<UserData, ServerFnError> {
use actix_web::HttpRequest;
use leptos_actix::extract;
let req: HttpRequest = extract().await?;
println!("Method: {:?}", req.method());
return Ok(UserData {
name: "Luxalpa".to_string(),
});
}
#[component]
pub fn FrontPage() -> impl IntoView {
spawn_local(async move {
if let Ok(site_data) = get_user_data().await {
println!("Site Data: {:?}", site_data);
}
});
view! { <div> "Front Page" </div> }
}
from leptos.
The code above is just a minimalistic example. My application is actually using a resource that is set as pending like this:
let resource = create_blocking_resource(
move || {},
// TODO: Figure out if this future needs to be manually cleaned up somehow
move |_| future::pending::<CardWithPrinting>(),
);
and then later (in the same function) it is fetching the data:
spawn_local(async move {
let result = get_card_printings(ids).await.unwrap().0;
self.add_to_cache(result);
});
and in add_to_cache
it is setting the resource:
// Notify resources
if let Some(resources) = in_use.remove(&card.printing.id) {
resources.iter().for_each(|r| {
r.try_set(card.clone());
});
}
Because the application needs to cache the data that comes from these resources while also providing SSR for them too. This architecture was inspired by leptos_query, but apparently it does not work well with server functions.
from leptos.
To be clear the panic is unrelated to server functions, it specifically has to do with unwrapping the attempt to extract an HttpRequest
during the server's initial route generation, which runs the application once to figure out all the routes you've defined. It suppresses resource loads during this, but the spawn_local
still runs, and panics when unwrapping the HttpRequest
extractor. (There is no HTTP request during that route generation process.)
In the actual example you provide, I assume that just not unwrapping here will fix your issue:
spawn_local(async move {
let result = get_card_printings(ids).await.unwrap().0;
self.add_to_cache(result);
});
// instead
spawn_local(async move {
// I'm missing the .0 here but hopefully you see what I mean
if let Ok(result) = get_card_printings(ids).await {
self.add_to_cache(result);
}
});
and likewise removing the .unwrap()
in get_card_printings
and using ?
instead if the extractor fails.
from leptos.
ok, thanks a lot, that actually explains some things and allowed me to workaround this.
from leptos.
Related Issues (20)
- There is a bug in Safari browser where signals cannot be transmitted properly HOT 1
- DynChild is populated as a single Child in the components' children property HOT 4
- Breaking change in view macro between 0.6.10 and 0.6.11
- Allow custom derive for server_fns with custom encoding HOT 3
- Add Trigger::dispose
- leptos_macro 0.6.11 leptos::view! breaks with some comments
- Non-'static signals
- the format!() bug in <a href={format!()}> HOT 2
- Effects/Memos should't be able to "own" a signal HOT 4
- Nested Suspense ignores SSR Mode and breaks Hydration HOT 2
- Click events are registered twice when two leptos custom elements are on a page HOT 3
- about mobile support
- Recursive effects never run after recursing HOT 4
- Clippy warnings in some components with rust 1.78 HOT 1
- rkyv, avoid the copy to byte ? HOT 4
- Check ActionForm input names at compile time HOT 4
- Allow server components in islands architecture to call code behind ssr without need for #[server] HOT 1
- porting over some react code to my project, having hydration issues
- leptos_router parent Route attr view is not generating view for dynamic routes HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leptos.