From 622d010cb1fc36128e2d12023c50b5c34f889f92 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Tue, 24 Mar 2026 16:57:39 +0800 Subject: [PATCH] fix(server): add Cache-Control: no-store to static file responses Prevents browser from caching JS/CSS modules, so frontend changes take effect immediately on page refresh without needing hard refresh. Co-Authored-By: Claude Sonnet 4.6 --- src/main.rs | 16 ++++++++++++++-- web/index.html | 4 ++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 36aa5ff..fe9bb62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,9 +21,19 @@ use event::EventManager; use middleware::simple_logger; use std::sync::Arc; use tokio::sync::mpsc; +use axum::{extract::Request, middleware::Next, response::Response}; use tower_http::cors::{Any, CorsLayer}; use tower_http::services::ServeDir; +async fn no_cache(req: Request, next: Next) -> Response { + let mut res = next.run(req).await; + res.headers_mut().insert( + axum::http::header::CACHE_CONTROL, + axum::http::HeaderValue::from_static("no-store"), + ); + res +} + #[derive(Clone)] pub struct AppState { pub config: AppConfig, @@ -259,9 +269,11 @@ fn build_router(state: AppState) -> Router { Router::new() .merge(all_route) - .nest_service( + .nest( "/ui", - ServeDir::new("web").append_index_html_on_directories(true), + Router::new() + .fallback_service(ServeDir::new("web").append_index_html_on_directories(true)) + .layer(axum::middleware::from_fn(no_cache)), ) .route("/ws/public", get(websocket::public_websocket_handler)) .route( diff --git a/web/index.html b/web/index.html index 26175d2..9f6dc8a 100644 --- a/web/index.html +++ b/web/index.html @@ -4,7 +4,7 @@ PLC Control - +
@@ -20,6 +20,6 @@
- +