Skip to content

Commit ea98465

Browse files
committed
feat: updated fetching example for async reqwest
1 parent 5a0e107 commit ea98465

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

examples/demos/fetching/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ perseus = { path = "../../../packages/perseus", features = [ "hydrate" ] }
1010
sycamore = "=0.8.0-beta.6"
1111
serde = { version = "1", features = ["derive"] }
1212
serde_json = "1"
13-
ureq = "2"
1413
reqwasm = "0.4"
14+
15+
# This makes sure `reqwest` is only included on the server-side (it won't compile at all for the browser)
16+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
17+
reqwest = "0.11"

examples/demos/fetching/src/templates/index.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn index_page<'a, G: Html>(
2323
// Don't worry, this doesn't need to be sent to JavaScript for execution
2424
//
2525
// We want to access the `message` `Signal`, so we'll clone it in (and then we need `move` because this has to be `'static`)
26-
perseus::spawn_local_scoped(cx, async move {
26+
perseus::spawn_local_scoped(cx, async {
2727
// This interface may seem weird, that's because it wraps the browser's Fetch API
2828
// We request from a local path here because of CORS restrictions (see the book)
2929
let body = reqwasm::http::Request::get("/.perseus/static/message.txt")
@@ -39,10 +39,10 @@ pub fn index_page<'a, G: Html>(
3939

4040
// If the future hasn't finished yet, we'll display a placeholder
4141
// 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)
42-
let browser_ip_display = match &*browser_ip.get() {
42+
let browser_ip_display = create_memo(cx, || match &*browser_ip.get() {
4343
Some(ip) => ip.to_string(),
4444
None => "fetching".to_string(),
45-
};
45+
});
4646

4747
view! { cx,
4848
p { (format!("IP address of the server was: {}", server_ip.get())) }
@@ -62,16 +62,22 @@ pub async fn get_build_state(
6262
_locale: String,
6363
) -> RenderFnResultWithCause<IndexPageState> {
6464
// 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)
65+
// 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)
66+
#[cfg(not(target_arch = "wasm32"))]
6567
let body = perseus::cache_fallible_res(
6668
"ipify",
6769
|| async {
6870
// This just gets the IP address of the machine that built the app
69-
let res = ureq::get("https://api.ipify.org").call()?.into_string()?;
70-
Ok::<String, ureq::Error>(res)
71+
let res = reqwest::get("https://api.ipify.org").await?.text().await?;
72+
Ok::<String, reqwest::Error>(res)
7173
},
7274
false,
7375
)
7476
.await?;
77+
// 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)
78+
#[cfg(target_arch = "wasm32")]
79+
let body = "".to_string();
80+
7581
Ok(IndexPageState {
7682
server_ip: body,
7783
browser_ip: None,

0 commit comments

Comments
 (0)