Skip to content

Commit

Permalink
sim: fix static files not being cached
Browse files Browse the repository at this point in the history
  • Loading branch information
varqox committed May 25, 2024
1 parent 640c701 commit 9f5c464
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
6 changes: 3 additions & 3 deletions subprojects/sim/src/web_server/http/response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<time_t>(-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());
Expand All @@ -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
);
}

Expand Down
2 changes: 1 addition & 1 deletion subprojects/sim/src/web_server/http/response.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 21 additions & 4 deletions subprojects/sim/src/web_server/ui/ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9f5c464

Please sign in to comment.