Skip to content

Commit 184381f

Browse files
committed
feat: re-exported spawn_local for convenience
1 parent 2b251be commit 184381f

File tree

5 files changed

+5
-5
lines changed

5 files changed

+5
-5
lines changed

docs/0.3.x/en-US/server-communication.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ It's fairly trivial to communicate with a server at build-time in Perseus, which
3131

3232
### In the Browser
3333

34-
In some cases, it's just not possible to fetch the data you need on the server, and the client needs to fetch it themselves. This is often the case in [exported](:exporting) apps. This is typically done with the browser's inbuilt Fetch API, which is conveniently wrapped by [`reqwasm`](https://docs.rs/reqwasm).
34+
In some cases, it's just not possible to fetch the data you need on the server, and the client needs to fetch it themselves. This is often the case in [exported](:exporting) apps. This is typically done with the browser's inbuilt Fetch API, which is conveniently wrapped by [`reqwasm`](https://docs.rs/reqwasm). Note that you'll need to do this in a `Future`, which you can spawn using [`wasm_bindgen_futures::spawn_local`](https://docs.rs/wasm-bindgen-futures/latest/wasm_bindgen_futures/fn.spawn_local.html), conveniently re-exported from Perseus as `perseus::spawn_local`. You can then modify a `Signal` in there that holds the data you want. It's common practice in web development today to show a page skeleton (those pulsating bars instead of text) while data are still being loaded.
3535

3636
However, if you try to request from a public API in this way, you may run into problems with [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS), which can be very confusing, especially if you're not used to web development! The simple explanation of this is that CORS is a *thing* that browsers use to make sure your code can't send requests to servers that haven't allowed it (as in your code specifically). If you're querying your own server and getting this problem, make sure to set the `Access-Control-Allow-Origin` header to allow your site to make requests (see [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) for more details). However, if a public API hasn't set this, you're up the creek! In these cases, it's best to query through your own server or through one of Perseus' rendering strategies (if possible).
3737

docs/next/en-US/server-communication.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ It's fairly trivial to communicate with a server at build-time in Perseus, which
3131

3232
### In the Browser
3333

34-
In some cases, it's just not possible to fetch the data you need on the server, and the client needs to fetch it themselves. This is often the case in [exported](:exporting) apps. This is typically done with the browser's inbuilt Fetch API, which is conveniently wrapped by [`reqwasm`](https://docs.rs/reqwasm).
34+
In some cases, it's just not possible to fetch the data you need on the server, and the client needs to fetch it themselves. This is often the case in [exported](:exporting) apps. This is typically done with the browser's inbuilt Fetch API, which is conveniently wrapped by [`reqwasm`](https://docs.rs/reqwasm). Note that you'll need to do this in a `Future`, which you can spawn using [`wasm_bindgen_futures::spawn_local`](https://docs.rs/wasm-bindgen-futures/latest/wasm_bindgen_futures/fn.spawn_local.html), conveniently re-exported from Perseus as `perseus::spawn_local`. You can then modify a `Signal` in there that holds the data you want. It's common practice in web development today to show a page skeleton (those pulsating bars instead of text) while data are still being loaded.
3535

3636
However, if you try to request from a public API in this way, you may run into problems with [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS), which can be very confusing, especially if you're not used to web development! The simple explanation of this is that CORS is a *thing* that browsers use to make sure your code can't send requests to servers that haven't allowed it (as in your code specifically). If you're querying your own server and getting this problem, make sure to set the `Access-Control-Allow-Origin` header to allow your site to make requests (see [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) for more details). However, if a public API hasn't set this, you're up the creek! In these cases, it's best to query through your own server or through one of Perseus' rendering strategies (if possible).
3737

examples/fetching/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ serde = { version = "1", features = ["derive"] }
1212
serde_json = "1"
1313
ureq = "2"
1414
reqwasm = "0.4"
15-
wasm-bindgen-futures = "0.4"

examples/fetching/src/index.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ pub fn index_page(IndexProps { ip }: IndexProps) -> View<G> {
1717
// This will only run in the browser
1818
// `reqwasm` wraps browser-specific APIs, so we don't want it running on the server
1919
if G::IS_BROWSER {
20-
// Spawn a `Future` on this thread to fetch the data
20+
// Spawn a `Future` on this thread to fetch the data (`spawn_local` is re-exported from `wasm-bindgen-futures`)
2121
// Don't worry, this doesn't need to be sent to JavaScript for execution
2222
//
2323
// We want to access the `message` `Signal`, so we'll clone it in (and then we need `move` because this has to be `'static`)
24-
wasm_bindgen_futures::spawn_local(cloned!(message => async move {
24+
perseus::spawn_local(cloned!(message => async move {
2525
// This interface may seem weird, that's because it wraps the browser's Fetch API
2626
// We request from a local path here because of CORS restrictions (see the book)
2727
let body = reqwasm::http::Request::get("/message")

packages/perseus/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ mod translator;
6666
// Re-exports
6767
pub use http;
6868
pub use http::Request as HttpRequest;
69+
pub use wasm_bindgen_futures::spawn_local;
6970
/// All HTTP requests use empty bodies for simplicity of passing them around. They'll never need payloads (value in path requested).
7071
pub type Request = HttpRequest<()>;
7172
pub use perseus_macro::{autoserde, head, template, test};

0 commit comments

Comments
 (0)