allow limited http caching of static assets in prod

This commit is contained in:
Brent Schroeter 2025-12-11 03:50:10 +00:00
parent 6cd15e380a
commit e376582f12
2 changed files with 28 additions and 10 deletions

View file

@ -55,16 +55,24 @@ pub(crate) fn new_router(app: App) -> Router<()> {
ServiceBuilder::new()
.layer(SetResponseHeaderLayer::if_not_present(
CACHE_CONTROL,
// FIXME: restore production value
// HeaderValue::from_static("max-age=21600, stale-while-revalidate=86400"),
HeaderValue::from_static("no-cache"),
HeaderValue::from_static(if cfg!(debug_assertions) {
// Disable caching when developing locally.
"no-cache"
} else {
"max-age=120, stale-while-revalidate=86400"
}),
))
.service(
ServeDir::new("js_dist").not_found_service(
ServiceBuilder::new()
.layer(SetResponseHeaderLayer::if_not_present(
CACHE_CONTROL,
HeaderValue::from_static("no-cache"),
HeaderValue::from_static(if cfg!(debug_assertions) {
// Disable caching when developing locally.
"no-cache"
} else {
"max-age=120, stale-while-revalidate=86400"
}),
))
.service(ServeFile::new("static/_404.html")),
),
@ -75,9 +83,12 @@ pub(crate) fn new_router(app: App) -> Router<()> {
ServiceBuilder::new()
.layer(SetResponseHeaderLayer::if_not_present(
CACHE_CONTROL,
// FIXME: restore production value
// HeaderValue::from_static("max-age=21600, stale-while-revalidate=86400"),
HeaderValue::from_static("no-cache"),
HeaderValue::from_static(if cfg!(debug_assertions) {
// Disable caching when developing locally.
"no-cache"
} else {
"max-age=120, stale-while-revalidate=86400"
}),
))
.service(
ServeDir::new("css_dist").not_found_service(
@ -94,13 +105,20 @@ pub(crate) fn new_router(app: App) -> Router<()> {
ServiceBuilder::new()
.layer(SetResponseHeaderLayer::if_not_present(
CACHE_CONTROL,
HeaderValue::from_static("max-age=21600, stale-while-revalidate=86400"),
HeaderValue::from_static(if cfg!(debug_assertions) {
// Disable caching when developing locally.
"no-cache"
} else {
"max-age=120, stale-while-revalidate=86400"
}),
))
.service(
ServeDir::new("static").not_found_service(
ServiceBuilder::new()
.layer(SetResponseHeaderLayer::if_not_present(
CACHE_CONTROL,
// Do not allow caching of paths if they return
// a 404 error.
HeaderValue::from_static("no-cache"),
))
.service(ServeFile::new("static/_404.html")),

View file

@ -15,8 +15,8 @@ export default defineConfig({
output: {
dir: path.fromFileUrl(new URL("../js_dist", import.meta.url)),
entryFileNames: "[name].mjs",
chunkFileNames: "[name].mjs",
assetFileNames: "[name].[ext]",
chunkFileNames: "[name]-[hash].mjs",
assetFileNames: "[name]-[hash].[ext]",
},
},
sourcemap: true,