chore: static: if PROFILE=dev assets are directly /static without any cache
This commit is contained in:
@@ -12,6 +12,18 @@ pub fn create_web_routes() -> Router<Arc<AppState>> {
|
|||||||
// Get config to access static path
|
// Get config to access static path
|
||||||
let config = AppConfig::from_env();
|
let config = AppConfig::from_env();
|
||||||
|
|
||||||
|
// XXX do we prefer PROFILE or ENV ?
|
||||||
|
let is_dev = std::env::var("PROFILE").is_ok_and(|profile| profile == "dev");
|
||||||
|
|
||||||
|
let assets_dir =
|
||||||
|
if is_dev {
|
||||||
|
// take directly the source (permits faster development)
|
||||||
|
"static"
|
||||||
|
} else {
|
||||||
|
// take the compiled assets
|
||||||
|
"static-dist"
|
||||||
|
};
|
||||||
|
|
||||||
// In release builds, serve from static-dist/ (processed assets).
|
// In release builds, serve from static-dist/ (processed assets).
|
||||||
// In debug builds, serve from the original static/ directory.
|
// In debug builds, serve from the original static/ directory.
|
||||||
let static_path = if cfg!(not(debug_assertions)) {
|
let static_path = if cfg!(not(debug_assertions)) {
|
||||||
@@ -19,7 +31,7 @@ pub fn create_web_routes() -> Router<Arc<AppState>> {
|
|||||||
.static_path
|
.static_path
|
||||||
.parent()
|
.parent()
|
||||||
.unwrap_or(std::path::Path::new("."))
|
.unwrap_or(std::path::Path::new("."))
|
||||||
.join("static-dist");
|
.join(assets_dir );
|
||||||
if dist.exists() {
|
if dist.exists() {
|
||||||
dist
|
dist
|
||||||
} else {
|
} else {
|
||||||
@@ -35,6 +47,14 @@ pub fn create_web_routes() -> Router<Arc<AppState>> {
|
|||||||
// do NOT pass through these layers.
|
// do NOT pass through these layers.
|
||||||
let static_service = ServeDir::new(&static_path);
|
let static_service = ServeDir::new(&static_path);
|
||||||
|
|
||||||
|
// By default assets are cached in release/production environement and no cache in dev
|
||||||
|
let cache_control_value =
|
||||||
|
if is_dev {
|
||||||
|
"max-age=0, no-cache, no-store"
|
||||||
|
} else {
|
||||||
|
"public, max-age=604800, stale-while-revalidate=86400"
|
||||||
|
};
|
||||||
|
|
||||||
Router::new()
|
Router::new()
|
||||||
// Add specific routes for clean URLs (without .html)
|
// Add specific routes for clean URLs (without .html)
|
||||||
.route("/login", get(serve_login_page))
|
.route("/login", get(serve_login_page))
|
||||||
@@ -46,7 +66,7 @@ pub fn create_web_routes() -> Router<Arc<AppState>> {
|
|||||||
.layer(CompressionLayer::new().br(true).gzip(true))
|
.layer(CompressionLayer::new().br(true).gzip(true))
|
||||||
.layer(SetResponseHeaderLayer::if_not_present(
|
.layer(SetResponseHeaderLayer::if_not_present(
|
||||||
CACHE_CONTROL,
|
CACHE_CONTROL,
|
||||||
HeaderValue::from_static("public, max-age=604800, stale-while-revalidate=86400"),
|
HeaderValue::from_static(cache_control_value),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user