-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
build_state_fn doesn't work after perseus::autoserde(build_state) (Sync required) #96
Comments
Unfortunately, since the release of the Warp integration, there are stricter requirements on build functions being sendable and syncable (Actix Web does some That said, I'm happy to have a look at this and see if I can find a workaround. Would you mind posting an MRE? |
Thanks @arctic-hen7 for a quick reply. OK, here is an MRE (I'm not sure if it is really minimal, but I built it on top of hello world and the 2nd app):
[package]
name = "mre-surf"
version = "0.1.0"
edition = "2021"
[dependencies]
perseus = { version = "0.3.0", features = [ "hydrate" ] }
sycamore = "0.7"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
surf = "2.3.2"
use perseus::{
define_app,
ErrorPages,
Html,
RenderFnResultWithCause,
Template
};
use sycamore::{
view,
prelude::{component, View}
};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct IpAddr {
ip: String
}
define_app! {
templates: [
example_template::<G>()
],
error_pages: ErrorPages::new(|url, status, err, _| {
view! {
p { (format!("An error with HTTP code {} occurred at '{}': '{}'.", status, url, err)) }
}
})
}
#[perseus::template(IndexPage)]
#[component(IndexPage<G>)]
pub fn index_page(props: IpAddr) -> View<G> {
view! {
p { "Your IP is " (props.ip) }
}
}
pub fn example_template<G: Html>() -> Template<G> {
Template::new("index")
.build_state_fn(get_build_props)
.template(index_page)
}
#[perseus::autoserde(build_state)]
pub async fn get_build_props(_path: String, _locale: String) -> RenderFnResultWithCause<IpAddr> {
Ok(get_ip().await.unwrap())
}
#[cfg(not(target_arch = "wasm32"))]
pub async fn get_ip() -> Result<IpAddr, surf::Error> {
let content = surf::get("https://api.ipify.org?format=json")
.recv_json();
content.await
}
#[cfg(target_arch = "wasm32")]
pub async fn get_ip() -> Result<IpAddr, Box<dyn std::error::Error>> {
Ok(IpAddr { ip: "127.0.0.1".to_string() })
} The wasm version of |
Great, thanks. I'll look at this in detail first chance I get. |
Sorry for the delay! My initial work on this suggests that the problem code is the If these are removed, I think your code would work, but, unfortunately, they're crucial for any multi-threaded servers other than Actix Web (which seems to use Now, given that you're guaranteeing that this version of the #[cfg(not(target_arch = "wasm32"))]
pub async fn get_ip() -> Result<IpAddr, surf::Error> {
let content = surf::get("https://api.ipify.org?format=json")
.recv_json::<IpAddr>();
let content = futures::executor::block_on(content); // Works, but terrible for build times in larger apps
content
} As for the implications for Perseus, I think this is unfortunately an error that users will have to address. However, I will put this requirement in the common pitfalls section of the docs so that it's easier to understand. |
Dear @arctic-hen7, thank you for your reply and effort. It makes sense what you're suggesting, however, I have the same problem on the client side, too, with |
They should be. One of the reasons there are issues with this is the lack of a proper async runtime in Perseus for builders, which means blocking libraries like As for the client, I haven't personally used |
Okay, as of #102, we have an async runtime! That should make this easier with libraries like |
Amazing! |
Wou! That was fast! Thanks a lot.
Indeed! |
Thank you, no problem! There's now an example for fetching data in the |
Brave! 🎉 |
Yeah, I'll get hot reloading working and then v0.3.1 should push in the coming days. |
Describe the bug
Since the newest beta, or after 0.3.0beta18, I believe, and now with newly released 0.3.0, I encounter the following error when building with
perseus
:I see that the problem is related to
surf
, but this worked a few versions ago, before the introduction#[perseus::autoserde(build_state)]
. How to fix this?Here is a sample:
Environment (please complete the following information):
The text was updated successfully, but these errors were encountered: