diff --git a/Cargo.toml b/Cargo.toml index bcfda7c9b..29f92b3ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -210,7 +210,8 @@ features = [ "ServiceWorkerGlobalScope", "RequestCredentials", "File", - "ReadableStream" + "ReadableStream", + "RequestCache" ] [target.'cfg(target_arch = "wasm32")'.dev-dependencies] diff --git a/src/wasm/client.rs b/src/wasm/client.rs index f6d303e77..ecb58bddb 100644 --- a/src/wasm/client.rs +++ b/src/wasm/client.rs @@ -216,6 +216,10 @@ async fn fetch(req: Request) -> crate::Result { init.credentials(creds); } + if let Some(cache) = req.cache { + init.set_cache(cache); + } + if let Some(body) = req.body() { if !body.is_empty() { init.body(Some(body.to_js_value()?.as_ref())); diff --git a/src/wasm/request.rs b/src/wasm/request.rs index 5d7ff22ea..3e2bb8ed8 100644 --- a/src/wasm/request.rs +++ b/src/wasm/request.rs @@ -8,7 +8,7 @@ use serde::Serialize; #[cfg(feature = "json")] use serde_json; use url::Url; -use web_sys::RequestCredentials; +use web_sys::{RequestCache, RequestCredentials}; use super::{Body, Client, Response}; use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE}; @@ -22,6 +22,7 @@ pub struct Request { timeout: Option, pub(super) cors: bool, pub(super) credentials: Option, + pub(super) cache: Option, } /// A builder to construct the properties of a `Request`. @@ -42,6 +43,7 @@ impl Request { timeout: None, cors: true, credentials: None, + cache: None, } } @@ -122,6 +124,7 @@ impl Request { timeout: self.timeout, cors: self.cors, credentials: self.credentials, + cache: self.cache, }) } } @@ -375,6 +378,102 @@ impl RequestBuilder { self } + /// Set fetch cache mode to 'default'. + /// + /// # WASM + /// + /// This option is only effective with WebAssembly target. + /// + /// The [request cache][mdn] will be set to 'default'. + /// + /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache + pub fn fetch_cache_default(mut self) -> RequestBuilder { + if let Ok(ref mut req) = self.request { + req.cache = Some(RequestCache::Default); + } + self + } + + /// Set fetch cache mode to 'no-store'. + /// + /// # WASM + /// + /// This option is only effective with WebAssembly target. + /// + /// The [request cache][mdn] will be set to 'no-store'. + /// + /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache + pub fn fetch_cache_no_store(mut self) -> RequestBuilder { + if let Ok(ref mut req) = self.request { + req.cache = Some(RequestCache::NoStore); + } + self + } + + /// Set fetch cache mode to 'reload'. + /// + /// # WASM + /// + /// This option is only effective with WebAssembly target. + /// + /// The [request cache][mdn] will be set to 'reload'. + /// + /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache + pub fn fetch_cache_reload(mut self) -> RequestBuilder { + if let Ok(ref mut req) = self.request { + req.cache = Some(RequestCache::Reload); + } + self + } + + /// Set fetch cache mode to 'no-cache'. + /// + /// # WASM + /// + /// This option is only effective with WebAssembly target. + /// + /// The [request cache][mdn] will be set to 'no-cache'. + /// + /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache + pub fn fetch_cache_no_cache(mut self) -> RequestBuilder { + if let Ok(ref mut req) = self.request { + req.cache = Some(RequestCache::NoCache); + } + self + } + + /// Set fetch cache mode to 'force-cache'. + /// + /// # WASM + /// + /// This option is only effective with WebAssembly target. + /// + /// The [request cache][mdn] will be set to 'force-cache'. + /// + /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache + pub fn fetch_cache_force_cache(mut self) -> RequestBuilder { + if let Ok(ref mut req) = self.request { + req.cache = Some(RequestCache::ForceCache); + } + self + } + + /// Set fetch cache mode to 'only-if-cached'. + /// + /// # WASM + /// + /// This option is only effective with WebAssembly target. + /// + /// The [request cache][mdn] will be set to 'only-if-cached'. + /// + /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache + pub fn fetch_cache_only_if_cached(mut self) -> RequestBuilder { + if let Ok(ref mut req) = self.request { + req.cache = Some(RequestCache::OnlyIfCached); + } + self + } + /// Build a `Request`, which can be inspected, modified and executed with /// `Client::execute()`. pub fn build(self) -> crate::Result { @@ -493,6 +592,7 @@ where timeout: None, cors: true, credentials: None, + cache: None, }) } }