From 9f5c464dcb88a6c2a8969ea471fa12ed2e39a105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Ma=C5=82ysa?= Date: Sat, 25 May 2024 15:01:34 +0200 Subject: [PATCH] sim: fix static files not being cached --- .../sim/src/web_server/http/response.cc | 6 ++--- .../sim/src/web_server/http/response.hh | 2 +- subprojects/sim/src/web_server/ui/ui.cc | 25 ++++++++++++++++--- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/subprojects/sim/src/web_server/http/response.cc b/subprojects/sim/src/web_server/http/response.cc index 3bfc0c15..d43398ee 100644 --- a/subprojects/sim/src/web_server/http/response.cc +++ b/subprojects/sim/src/web_server/http/response.cc @@ -7,12 +7,12 @@ namespace web_server::http { -void Response::set_cache(bool to_public, uint max_age, bool must_revalidate) { +void Response::set_cache(bool to_public, uint max_age_in_seconds, bool must_revalidate) { time_t curr_time; if (time(&curr_time) == static_cast(-1)) { THROW("time()", errmsg()); } - curr_time += max_age; + curr_time += max_age_in_seconds; struct tm t; if (!gmtime_r(&curr_time, &t)) { THROW("gmtime_r()", errmsg()); @@ -26,7 +26,7 @@ void Response::set_cache(bool to_public, uint max_age, bool must_revalidate) { (to_public ? "public" : "private"), (must_revalidate ? "; must-revalidate" : ""), "; max-age=", - max_age + max_age_in_seconds ); } diff --git a/subprojects/sim/src/web_server/http/response.hh b/subprojects/sim/src/web_server/http/response.hh index f2503e61..e196a606 100644 --- a/subprojects/sim/src/web_server/http/response.hh +++ b/subprojects/sim/src/web_server/http/response.hh @@ -27,7 +27,7 @@ public: ~Response() = default; - void set_cache(bool to_public, uint max_age, bool must_revalidate); + void set_cache(bool to_public, uint max_age_in_seconds, bool must_revalidate); }; } // namespace web_server::http diff --git a/subprojects/sim/src/web_server/ui/ui.cc b/subprojects/sim/src/web_server/ui/ui.cc index f43f2285..6037ff46 100644 --- a/subprojects/sim/src/web_server/ui/ui.cc +++ b/subprojects/sim/src/web_server/ui/ui.cc @@ -7,22 +7,39 @@ using web_server::http::Response; using web_server::web_worker::Context; +namespace { + +Response&& with_public_cache_valid_for_a_year_and_non_obligator_revalidation(Response&& resp) { + resp.set_cache(true, 365 * 24 * 60 * 60, false); + return std::move(resp); +} + +} // namespace + namespace web_server::ui { Response scripts_js(Context& ctx, StringView /*timestamp*/) { - return ctx.response_file("static/kit/scripts.js", "text/javascript; charset=utf-8"); + return with_public_cache_valid_for_a_year_and_non_obligator_revalidation( + ctx.response_file("static/kit/scripts.js", "text/javascript; charset=utf-8") + ); } Response jquery_js(Context& ctx, StringView /*timestamp*/) { - return ctx.response_file("static/kit/jquery.js", "text/javascript; charset=utf-8"); + return with_public_cache_valid_for_a_year_and_non_obligator_revalidation( + ctx.response_file("static/kit/jquery.js", "text/javascript; charset=utf-8") + ); } Response styles_css(Context& ctx, StringView /*timestamp*/) { - return ctx.response_file("static/kit/styles.css", "text/css; charset=utf-8"); + return with_public_cache_valid_for_a_year_and_non_obligator_revalidation( + ctx.response_file("static/kit/styles.css", "text/css; charset=utf-8") + ); } Response favicon_ico(Context& ctx) { - return ctx.response_file("static/favicon.ico", "image/x-icon"); + return with_public_cache_valid_for_a_year_and_non_obligator_revalidation( + ctx.response_file("static/favicon.ico", "image/x-icon") + ); } } // namespace web_server::ui