Skip to content

Commit 9e327f1

Browse files
committed
perf: cache CSS bundle in LazyLock to avoid per-request allocation
1 parent feb0a1b commit 9e327f1

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,23 @@ async fn resource(body: &str, content_type: &str, cache: bool) -> Result<Respons
8989
Ok(res)
9090
}
9191

92-
async fn style() -> Result<Response<Body>, String> {
92+
static STYLE_CSS: LazyLock<String> = LazyLock::new(|| {
9393
let mut res = include_str!("../static/style.css").to_string();
9494
for file in ThemeAssets::iter() {
9595
res.push('\n');
9696
let theme = ThemeAssets::get(file.as_ref()).unwrap();
9797
res.push_str(std::str::from_utf8(theme.data.as_ref()).unwrap());
9898
}
99+
res
100+
});
101+
102+
async fn style() -> Result<Response<Body>, String> {
99103
Ok(
100104
Response::builder()
101105
.status(200)
102106
.header("content-type", "text/css")
103107
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
104-
.body(res.to_string().into())
108+
.body(Body::from(STYLE_CSS.clone()))
105109
.unwrap_or_default(),
106110
)
107111
}

0 commit comments

Comments
 (0)