@@ -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
2830impl 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