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 <noreply@anthropic.com>
This commit is contained in:
caoqianming 2026-03-24 16:57:39 +08:00
parent 3e277cdb52
commit 622d010cb1
2 changed files with 16 additions and 4 deletions

View File

@ -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(

View File

@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>PLC Control</title>
<link rel="stylesheet" href="/ui/styles.css?v=20260323e" />
<link rel="stylesheet" href="/ui/styles.css?v=20260324a" />
</head>
<body>
<div data-partial="/ui/html/topbar.html"></div>
@ -20,6 +20,6 @@
<div data-partial="/ui/html/modals.html"></div>
<div data-partial="/ui/html/api-doc-drawer.html"></div>
<script type="module" src="/ui/js/index.js?v=20260323f"></script>
<script type="module" src="/ui/js/index.js?v=20260324a"></script>
</body>
</html>