aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/ui
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-10-22 15:57:18 +0200
committermetamuffin <metamuffin@disroot.org>2023-10-22 15:57:18 +0200
commitc4de8d95619d6d8da8640801aeecc912cdb8d0b5 (patch)
tree1283fe3535eff74d71639a63149946aac5df2628 /server/src/routes/ui
parent1e239240a072a14a1af41785477946435eaf2101 (diff)
downloadjellything-c4de8d95619d6d8da8640801aeecc912cdb8d0b5.tar
jellything-c4de8d95619d6d8da8640801aeecc912cdb8d0b5.tar.bz2
jellything-c4de8d95619d6d8da8640801aeecc912cdb8d0b5.tar.zst
cache control assets
Diffstat (limited to 'server/src/routes/ui')
-rw-r--r--server/src/routes/ui/style.rs35
1 files changed, 25 insertions, 10 deletions
diff --git a/server/src/routes/ui/style.rs b/server/src/routes/ui/style.rs
index b8a5fdb..bdaddd2 100644
--- a/server/src/routes/ui/style.rs
+++ b/server/src/routes/ui/style.rs
@@ -4,7 +4,11 @@
Copyright (C) 2023 metamuffin <metamuffin.org>
Copyright (C) 2023 tpart
*/
-use rocket::{get, http::ContentType};
+use rocket::{
+ get,
+ http::{ContentType, Header},
+ response::Responder,
+};
macro_rules! concat_files {
([$base: expr], $($files:literal),*) => {{
@@ -42,6 +46,17 @@ fn css_bundle() -> String {
)
}
+pub struct CachedAsset<T>(pub T);
+impl<'r, 'o: 'r, T: Responder<'r, 'o>> Responder<'r, 'o> for CachedAsset<T> {
+ fn respond_to(self, request: &'r rocket::Request<'_>) -> rocket::response::Result<'o> {
+ let mut res = self.0.respond_to(request)?;
+ if cfg!(not(debug_assertions)) {
+ res.set_header(Header::new("cache-control", "max-age=86400"));
+ }
+ Ok(res)
+ }
+}
+
fn js_bundle() -> String {
concat_files!([env!("OUT_DIR")], "bundle.js")
}
@@ -50,23 +65,23 @@ fn js_bundle_map() -> String {
}
#[get("/assets/style.css")]
-pub fn r_assets_style() -> (ContentType, String) {
- (ContentType::CSS, css_bundle())
+pub fn r_assets_style() -> CachedAsset<(ContentType, String)> {
+ CachedAsset((ContentType::CSS, css_bundle()))
}
#[get("/assets/cantarell.woff2")]
-pub fn r_assets_font() -> (ContentType, &'static [u8]) {
- (
+pub fn r_assets_font() -> CachedAsset<(ContentType, &'static [u8])> {
+ CachedAsset((
ContentType::WOFF2,
include_bytes!("../../../../web/cantarell.woff2"),
- )
+ ))
}
#[get("/assets/bundle.js")]
-pub fn r_assets_js() -> (ContentType, String) {
- (ContentType::JavaScript, js_bundle())
+pub fn r_assets_js() -> CachedAsset<(ContentType, String)> {
+ CachedAsset((ContentType::JavaScript, js_bundle()))
}
#[get("/assets/bundle.js.map")]
-pub fn r_assets_js_map() -> (ContentType, String) {
- (ContentType::JSON, js_bundle_map())
+pub fn r_assets_js_map() -> CachedAsset<(ContentType, String)> {
+ CachedAsset((ContentType::JSON, js_bundle_map()))
}