Skip to content

Commit 82086e7

Browse files
authored
feat: add request cache options for wasm (#2775)
1 parent 2a0f7a3 commit 82086e7

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ features = [
210210
"ServiceWorkerGlobalScope",
211211
"RequestCredentials",
212212
"File",
213-
"ReadableStream"
213+
"ReadableStream",
214+
"RequestCache"
214215
]
215216

216217
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]

src/wasm/client.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ async fn fetch(req: Request) -> crate::Result<Response> {
216216
init.credentials(creds);
217217
}
218218

219+
if let Some(cache) = req.cache {
220+
init.set_cache(cache);
221+
}
222+
219223
if let Some(body) = req.body() {
220224
if !body.is_empty() {
221225
init.body(Some(body.to_js_value()?.as_ref()));

src/wasm/request.rs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde::Serialize;
88
#[cfg(feature = "json")]
99
use serde_json;
1010
use url::Url;
11-
use web_sys::RequestCredentials;
11+
use web_sys::{RequestCache, RequestCredentials};
1212

1313
use super::{Body, Client, Response};
1414
use crate::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE};
@@ -22,6 +22,7 @@ pub struct Request {
2222
timeout: Option<Duration>,
2323
pub(super) cors: bool,
2424
pub(super) credentials: Option<RequestCredentials>,
25+
pub(super) cache: Option<RequestCache>,
2526
}
2627

2728
/// A builder to construct the properties of a `Request`.
@@ -42,6 +43,7 @@ impl Request {
4243
timeout: None,
4344
cors: true,
4445
credentials: None,
46+
cache: None,
4547
}
4648
}
4749

@@ -122,6 +124,7 @@ impl Request {
122124
timeout: self.timeout,
123125
cors: self.cors,
124126
credentials: self.credentials,
127+
cache: self.cache,
125128
})
126129
}
127130
}
@@ -375,6 +378,102 @@ impl RequestBuilder {
375378
self
376379
}
377380

381+
/// Set fetch cache mode to 'default'.
382+
///
383+
/// # WASM
384+
///
385+
/// This option is only effective with WebAssembly target.
386+
///
387+
/// The [request cache][mdn] will be set to 'default'.
388+
///
389+
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
390+
pub fn fetch_cache_default(mut self) -> RequestBuilder {
391+
if let Ok(ref mut req) = self.request {
392+
req.cache = Some(RequestCache::Default);
393+
}
394+
self
395+
}
396+
397+
/// Set fetch cache mode to 'no-store'.
398+
///
399+
/// # WASM
400+
///
401+
/// This option is only effective with WebAssembly target.
402+
///
403+
/// The [request cache][mdn] will be set to 'no-store'.
404+
///
405+
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
406+
pub fn fetch_cache_no_store(mut self) -> RequestBuilder {
407+
if let Ok(ref mut req) = self.request {
408+
req.cache = Some(RequestCache::NoStore);
409+
}
410+
self
411+
}
412+
413+
/// Set fetch cache mode to 'reload'.
414+
///
415+
/// # WASM
416+
///
417+
/// This option is only effective with WebAssembly target.
418+
///
419+
/// The [request cache][mdn] will be set to 'reload'.
420+
///
421+
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
422+
pub fn fetch_cache_reload(mut self) -> RequestBuilder {
423+
if let Ok(ref mut req) = self.request {
424+
req.cache = Some(RequestCache::Reload);
425+
}
426+
self
427+
}
428+
429+
/// Set fetch cache mode to 'no-cache'.
430+
///
431+
/// # WASM
432+
///
433+
/// This option is only effective with WebAssembly target.
434+
///
435+
/// The [request cache][mdn] will be set to 'no-cache'.
436+
///
437+
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
438+
pub fn fetch_cache_no_cache(mut self) -> RequestBuilder {
439+
if let Ok(ref mut req) = self.request {
440+
req.cache = Some(RequestCache::NoCache);
441+
}
442+
self
443+
}
444+
445+
/// Set fetch cache mode to 'force-cache'.
446+
///
447+
/// # WASM
448+
///
449+
/// This option is only effective with WebAssembly target.
450+
///
451+
/// The [request cache][mdn] will be set to 'force-cache'.
452+
///
453+
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
454+
pub fn fetch_cache_force_cache(mut self) -> RequestBuilder {
455+
if let Ok(ref mut req) = self.request {
456+
req.cache = Some(RequestCache::ForceCache);
457+
}
458+
self
459+
}
460+
461+
/// Set fetch cache mode to 'only-if-cached'.
462+
///
463+
/// # WASM
464+
///
465+
/// This option is only effective with WebAssembly target.
466+
///
467+
/// The [request cache][mdn] will be set to 'only-if-cached'.
468+
///
469+
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
470+
pub fn fetch_cache_only_if_cached(mut self) -> RequestBuilder {
471+
if let Ok(ref mut req) = self.request {
472+
req.cache = Some(RequestCache::OnlyIfCached);
473+
}
474+
self
475+
}
476+
378477
/// Build a `Request`, which can be inspected, modified and executed with
379478
/// `Client::execute()`.
380479
pub fn build(self) -> crate::Result<Request> {
@@ -493,6 +592,7 @@ where
493592
timeout: None,
494593
cors: true,
495594
credentials: None,
595+
cache: None,
496596
})
497597
}
498598
}

0 commit comments

Comments
 (0)