Skip to content

Commit

Permalink
feat: updated fetching example for async reqwest
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Jun 8, 2022
1 parent 5a0e107 commit ea98465
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
5 changes: 4 additions & 1 deletion examples/demos/fetching/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ perseus = { path = "../../../packages/perseus", features = [ "hydrate" ] }
sycamore = "=0.8.0-beta.6"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
ureq = "2"
reqwasm = "0.4"

# This makes sure `reqwest` is only included on the server-side (it won't compile at all for the browser)
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
reqwest = "0.11"
16 changes: 11 additions & 5 deletions examples/demos/fetching/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn index_page<'a, G: Html>(
// Don't worry, this doesn't need to be sent to JavaScript for execution
//
// We want to access the `message` `Signal`, so we'll clone it in (and then we need `move` because this has to be `'static`)
perseus::spawn_local_scoped(cx, async move {
perseus::spawn_local_scoped(cx, async {
// This interface may seem weird, that's because it wraps the browser's Fetch API
// We request from a local path here because of CORS restrictions (see the book)
let body = reqwasm::http::Request::get("/.perseus/static/message.txt")
Expand All @@ -39,10 +39,10 @@ pub fn index_page<'a, G: Html>(

// If the future hasn't finished yet, we'll display a placeholder
// We use the wacky `&*` syntax to get the content of the `browser_ip` `Signal` and then we tell Rust to take a reference to that (we can't move it out because it might be used later)
let browser_ip_display = match &*browser_ip.get() {
let browser_ip_display = create_memo(cx, || match &*browser_ip.get() {
Some(ip) => ip.to_string(),
None => "fetching".to_string(),
};
});

view! { cx,
p { (format!("IP address of the server was: {}", server_ip.get())) }
Expand All @@ -62,16 +62,22 @@ pub async fn get_build_state(
_locale: String,
) -> RenderFnResultWithCause<IndexPageState> {
// We'll cache the result with `try_cache_res`, which means we only make the request once, and future builds will use the cached result (speeds up development)
// Currently, target gating isn't fully sorted out in the latest version, so, because `reqwest` is only available on the server-side, we have to note that (in future, this won't be necessary)
#[cfg(not(target_arch = "wasm32"))]
let body = perseus::cache_fallible_res(
"ipify",
|| async {
// This just gets the IP address of the machine that built the app
let res = ureq::get("https://api.ipify.org").call()?.into_string()?;
Ok::<String, ureq::Error>(res)
let res = reqwest::get("https://api.ipify.org").await?.text().await?;
Ok::<String, reqwest::Error>(res)
},
false,
)
.await?;
// To be clear, this will never ever run, we just need it in the current version to appease the compiler (soon, this will be totally unnecessary)
#[cfg(target_arch = "wasm32")]
let body = "".to_string();

Ok(IndexPageState {
server_ip: body,
browser_ip: None,
Expand Down

0 comments on commit ea98465

Please sign in to comment.