Skip to content

Commit ed63577

Browse files
danlapidclaude
andcommitted
feat: add CacheMode to RequestInit
Add support for the `cache` option in RequestInit, allowing control over how requests interact with the HTTP cache. Supports NoStore, NoCache, and Reload modes matching the Cloudflare Workers runtime. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 402f050 commit ed63577

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ web-sys = { version = "0.3.83", features = [
5959
"ReadableStreamDefaultReader",
6060
"Request",
6161
"RequestInit",
62+
"RequestCache",
6263
"RequestRedirect",
6364
"Response",
6465
"ResponseInit",

worker/src/request_init.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub struct RequestInit {
2323
/// follow. Note, however, that the incoming Request property of a FetchEvent will have redirect
2424
/// mode manual.
2525
pub redirect: RequestRedirect,
26+
/// The cache mode for the request. `None` means use the default behavior.
27+
pub cache: Option<CacheMode>,
2628
}
2729

2830
impl RequestInit {
@@ -45,6 +47,11 @@ impl RequestInit {
4547
self
4648
}
4749

50+
pub fn with_cache(&mut self, cache: CacheMode) -> &mut Self {
51+
self.cache = Some(cache);
52+
self
53+
}
54+
4855
pub fn with_body(&mut self, body: Option<JsValue>) -> &mut Self {
4956
self.body = body;
5057
self
@@ -62,6 +69,9 @@ impl From<&RequestInit> for web_sys::RequestInit {
6269
inner.set_headers(req.headers.as_ref());
6370
inner.set_method(req.method.as_ref());
6471
inner.set_redirect(req.redirect.into());
72+
if let Some(cache) = req.cache {
73+
inner.set_cache(cache.into());
74+
}
6575
if let Some(body) = req.body.as_ref() {
6676
inner.set_body(body);
6777
}
@@ -90,6 +100,7 @@ impl Default for RequestInit {
90100
cf: CfProperties::default(),
91101
method: Method::Get,
92102
redirect: RequestRedirect::default(),
103+
cache: None,
93104
}
94105
}
95106
}
@@ -538,3 +549,33 @@ impl From<RequestRedirect> for web_sys::RequestRedirect {
538549
}
539550
}
540551
}
552+
553+
/// Cache mode for controlling how requests interact with the cache.
554+
/// Corresponds to JavaScript's `RequestInit.cache` property.
555+
#[derive(Clone, Copy, Debug, Serialize)]
556+
#[serde(rename_all = "kebab-case")]
557+
pub enum CacheMode {
558+
NoStore,
559+
NoCache,
560+
Reload,
561+
}
562+
563+
impl From<CacheMode> for &str {
564+
fn from(mode: CacheMode) -> Self {
565+
match mode {
566+
CacheMode::NoStore => "no-store",
567+
CacheMode::NoCache => "no-cache",
568+
CacheMode::Reload => "reload",
569+
}
570+
}
571+
}
572+
573+
impl From<CacheMode> for web_sys::RequestCache {
574+
fn from(mode: CacheMode) -> Self {
575+
match mode {
576+
CacheMode::NoStore => web_sys::RequestCache::NoStore,
577+
CacheMode::NoCache => web_sys::RequestCache::NoCache,
578+
CacheMode::Reload => web_sys::RequestCache::Reload,
579+
}
580+
}
581+
}

0 commit comments

Comments
 (0)