Skip to content

Commit 3ad5e72

Browse files
authored
feat(core): cache dev server proxy responses for 304 status code (#5818)
1 parent 76204b8 commit 3ad5e72

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Implement response cache on the dev server proxy, used when the server responds with status 304.

core/tauri/src/manager.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,7 @@ impl<R: Runtime> WindowManager<R> {
449449
window_labels_array = serde_json::to_string(&window_labels)?,
450450
current_window_label = serde_json::to_string(&label)?,
451451
))
452-
.initialization_script(&self.initialization_script(&ipc_init.into_string(),&pattern_init.into_string(),&plugin_init, is_init_global)?)
453-
;
452+
.initialization_script(&self.initialization_script(&ipc_init.into_string(),&pattern_init.into_string(),&plugin_init, is_init_global)?);
454453

455454
#[cfg(feature = "isolation")]
456455
if let Pattern::Isolation { schema, .. } = self.pattern() {
@@ -897,6 +896,17 @@ impl<R: Runtime> WindowManager<R> {
897896
let manager = self.clone();
898897
let window_origin = window_origin.to_string();
899898

899+
#[cfg(dev)]
900+
#[derive(Clone)]
901+
struct CachedResponse {
902+
status: http::StatusCode,
903+
headers: http::HeaderMap,
904+
body: Vec<u8>,
905+
}
906+
907+
#[cfg(dev)]
908+
let response_cache = Arc::new(Mutex::new(HashMap::new()));
909+
900910
Box::new(move |request| {
901911
// use the entire URI as we are going to proxy the request
902912
#[cfg(dev)]
@@ -929,14 +939,30 @@ impl<R: Runtime> WindowManager<R> {
929939
}
930940
match proxy_builder.send() {
931941
Ok(r) => {
932-
for (name, value) in r.headers() {
933-
builder = builder.header(name, value);
942+
let mut response_cache_ = response_cache.lock().unwrap();
943+
let mut response = None;
944+
if r.status() == StatusCode::NOT_MODIFIED {
945+
response = response_cache_.get(&url);
934946
}
935-
let mut status = r.status();
936-
if status == StatusCode::NOT_MODIFIED {
937-
status = StatusCode::OK;
947+
let response = if let Some(r) = response {
948+
r
949+
} else {
950+
let (status, headers, reader) = r.split();
951+
let body = reader.bytes()?;
952+
let response = CachedResponse {
953+
status,
954+
headers,
955+
body,
956+
};
957+
response_cache_.insert(url.clone(), response);
958+
response_cache_.get(&url).unwrap()
959+
};
960+
for (name, value) in &response.headers {
961+
builder = builder.header(name, value);
938962
}
939-
builder.status(status).body(r.bytes()?)?
963+
builder
964+
.status(response.status)
965+
.body(response.body.clone())?
940966
}
941967
Err(e) => {
942968
debug_eprintln!("Failed to request {}: {}", url.as_str(), e);

0 commit comments

Comments
 (0)