From 0c64122b807a1d9f5b4def622dd3ce4d75d00724 Mon Sep 17 00:00:00 2001 From: ZhenYi <434836402@qq.com> Date: Mon, 20 Apr 2026 15:45:35 +0800 Subject: [PATCH] chore(frontend): update frontend build configuration --- libs/frontend/build.rs | 18 ++++++++++++++---- libs/frontend/src/lib.rs | 9 ++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/libs/frontend/build.rs b/libs/frontend/build.rs index f895914..17d50c4 100644 --- a/libs/frontend/build.rs +++ b/libs/frontend/build.rs @@ -1,3 +1,4 @@ +use md5::compute as md5_hash; use std::{env, fs, path::PathBuf, process::Command}; fn run_pnpm(args: &[&str], cwd: &str) { @@ -66,15 +67,24 @@ fn main() { let safe_name = key.replace('/', "_").replace('\\', "_"); let blob_path = blob_dir.join(&safe_name); fs::copy(&file, &blob_path).unwrap(); + + // Compute ETag (MD5 hex of content) at build time + let content = fs::read(&file).unwrap(); + let hash = md5_hash(&content); + let etag_literal = format!("\"{:x}\"", hash); + let key_literal = format!("\"{}\"", key.replace('"', "\\\"")); - strs.push(format!(" ({}, include_bytes!(\"dist_blobs/{}\")),", key_literal, safe_name)); + strs.push(format!( + " ({}, include_bytes!(\"dist_blobs/{}\"), {}),", + key_literal, safe_name, etag_literal + )); } let out_file = out_dir.join("frontend.rs"); - let content = format!( - "lazy_static::lazy_static! {{\n pub static ref FRONTEND: Vec<(&'static str, &'static [u8])> = vec![\n{} ];\n}}\n", + let generated = format!( + "lazy_static::lazy_static! {{\n pub static ref FRONTEND: Vec<(&'static str, &'static [u8], &'static str)> = vec![\n{} ];\n}}\n", strs.join("\n") ); - fs::write(&out_file, content).unwrap(); + fs::write(&out_file, generated).unwrap(); println!("cargo:include={}", out_file.display()); } diff --git a/libs/frontend/src/lib.rs b/libs/frontend/src/lib.rs index af8c602..3860661 100644 --- a/libs/frontend/src/lib.rs +++ b/libs/frontend/src/lib.rs @@ -4,5 +4,12 @@ include!(concat!(env!("OUT_DIR"), "/frontend.rs")); /// Returns the embedded frontend static asset for the given path, or `None` if not found. pub fn get_frontend_asset(path: &str) -> Option<&'static [u8]> { - FRONTEND.iter().find(|(k, _)| *k == path).map(|(_, v)| *v) + FRONTEND.iter().find(|(k, _, _)| *k == path).map(|(_, v, _)| *v) +} + +/// Returns the embedded frontend static asset and its ETag for the given path. +pub fn get_frontend_asset_with_etag(path: &str) -> Option<(&'static [u8], &'static str)> { + FRONTEND.iter() + .find(|(k, _, _)| *k == path) + .map(|(_, v, etag)| (v as &_, etag as &_)) }