From b22564cb000febe9a871f2121d96993ff5569c7b Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 27 Mar 2021 13:30:40 +0000 Subject: [PATCH 1/3] Cache icons on the client This should make the vault pages load much faster, and massively reduce the number of requests. --- src/api/icons.rs | 13 ++++++++----- src/util.rs | 14 +++++++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/api/icons.rs b/src/api/icons.rs index 5abcf375..9e6984cb 100644 --- a/src/api/icons.rs +++ b/src/api/icons.rs @@ -51,7 +51,10 @@ fn icon(domain: String) -> Option>>> { return None; } - get_icon(&domain).map(|icon| Cached::long(Content(ContentType::new("image", "x-icon"), icon))) + get_icon(&domain).map(|(icon, cached)| { + let cache_ttl = if cached {CONFIG.icon_cache_ttl()} else {CONFIG.icon_cache_negttl()}; + Cached::ttl(Content(ContentType::new("image", "x-icon"), icon), cache_ttl) + }) } /// Returns if the domain provided is valid or not. @@ -238,7 +241,7 @@ fn is_domain_blacklisted(domain: &str) -> bool { is_blacklisted } -fn get_icon(domain: &str) -> Option> { +fn get_icon(domain: &str) -> Option<(Vec, bool)> { let path = format!("{}/{}.png", CONFIG.icon_cache_folder(), domain); // Check for expiration of negatively cached copy @@ -247,7 +250,7 @@ fn get_icon(domain: &str) -> Option> { } if let Some(icon) = get_cached_icon(&path) { - return Some(icon); + return Some((icon, true)); } if CONFIG.disable_icon_download() { @@ -258,7 +261,7 @@ fn get_icon(domain: &str) -> Option> { match download_icon(&domain) { Ok(icon) => { save_icon(&path, &icon); - Some(icon) + Some((icon, false)) } Err(e) => { error!("Error downloading icon: {:?}", e); @@ -472,7 +475,7 @@ fn get_icon_url(domain: &str) -> Result { let dom = html5ever::parse_document(markup5ever_rcdom::RcDom::default(), Default::default()) .from_utf8() .read_from(&mut limited_reader)?; - + get_favicons_node(&dom.document, &mut iconlist, &url); } else { // Add the default favicon.ico to the list with just the given domain diff --git a/src/util.rs b/src/util.rs index de663583..ff0373d4 100644 --- a/src/util.rs +++ b/src/util.rs @@ -92,17 +92,21 @@ impl Fairing for CORS { } } -pub struct Cached(R, &'static str); +pub struct Cached(R, String); impl Cached { - pub const fn long(r: R) -> Cached { + pub fn long(r: R) -> Cached { // 7 days - Self(r, "public, max-age=604800") + Self(r, String::from("public, max-age=604800")) } - pub const fn short(r: R) -> Cached { + pub fn short(r: R) -> Cached { // 10 minutes - Self(r, "public, max-age=600") + Self(r, String::from("public, max-age=600")) + } + + pub fn ttl(r: R, ttl: u64) -> Cached { + Self(r, format!("public, immutable, max-age={}", ttl)) } } From 131348a49fd94f3fd63758c8419e70968464d4a0 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 27 Mar 2021 13:37:56 +0000 Subject: [PATCH 2/3] Add immutable caching for vault assets The URLs are cachebusted, so updates will still be applied cleanly and immediately --- src/util.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.rs b/src/util.rs index ff0373d4..40256eb7 100644 --- a/src/util.rs +++ b/src/util.rs @@ -97,7 +97,7 @@ pub struct Cached(R, String); impl Cached { pub fn long(r: R) -> Cached { // 7 days - Self(r, String::from("public, max-age=604800")) + Self::ttl(r, 604800) } pub fn short(r: R) -> Cached { From 6209e778e59c44d38288579a105c9e46c829c405 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 28 Mar 2021 10:39:12 +0100 Subject: [PATCH 3/3] Icons should always be cached using full TTL --- src/api/icons.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/api/icons.rs b/src/api/icons.rs index 9e6984cb..14cfa25f 100644 --- a/src/api/icons.rs +++ b/src/api/icons.rs @@ -51,10 +51,7 @@ fn icon(domain: String) -> Option>>> { return None; } - get_icon(&domain).map(|(icon, cached)| { - let cache_ttl = if cached {CONFIG.icon_cache_ttl()} else {CONFIG.icon_cache_negttl()}; - Cached::ttl(Content(ContentType::new("image", "x-icon"), icon), cache_ttl) - }) + get_icon(&domain).map(|icon| Cached::ttl(Content(ContentType::new("image", "x-icon"), icon), CONFIG.icon_cache_ttl())) } /// Returns if the domain provided is valid or not. @@ -241,7 +238,7 @@ fn is_domain_blacklisted(domain: &str) -> bool { is_blacklisted } -fn get_icon(domain: &str) -> Option<(Vec, bool)> { +fn get_icon(domain: &str) -> Option> { let path = format!("{}/{}.png", CONFIG.icon_cache_folder(), domain); // Check for expiration of negatively cached copy @@ -250,7 +247,7 @@ fn get_icon(domain: &str) -> Option<(Vec, bool)> { } if let Some(icon) = get_cached_icon(&path) { - return Some((icon, true)); + return Some(icon); } if CONFIG.disable_icon_download() { @@ -261,7 +258,7 @@ fn get_icon(domain: &str) -> Option<(Vec, bool)> { match download_icon(&domain) { Ok(icon) => { save_icon(&path, &icon); - Some((icon, false)) + Some(icon) } Err(e) => { error!("Error downloading icon: {:?}", e);