From 21d0dab0600493eb3c76b4770088e8a2d8bf6739 Mon Sep 17 00:00:00 2001 From: Nathan Wallach Date: Sun, 23 Jan 2022 15:53:21 +0200 Subject: [PATCH 1/2] Add a setting via render_app.conf.dist to set static files from webwork2_files to be cached by the browser, and to expire a set amount of time in the future. This should reduce server load regarding static files which are used by multiple problems, but still makes it reasonable to roll out updated files in a reasonable manner. At present, we only add the response header settings to support browser caching for files under webwork2_files. However, we set expired times for those whose URL contains a match to tmp/renderer as files which were generated for a specific problem may be dangerous to cache, in case they get updated in any manner on a subsequent call. --- lib/RenderApp.pm | 31 +++++++++++++++++++++++++++++++ render_app.conf.dist | 1 + 2 files changed, 32 insertions(+) diff --git a/lib/RenderApp.pm b/lib/RenderApp.pm index f129404a0..f3b97c4d1 100644 --- a/lib/RenderApp.pm +++ b/lib/RenderApp.pm @@ -69,6 +69,37 @@ sub startup { }); } + # Add Cache-Control and Expires headers to static content from webwork2_files + if (my $STATIC_EXPIRES = $self->config('STATIC_EXPIRES')) { + $STATIC_EXPIRES = int( $STATIC_EXPIRES ); + my $cache_control_setting = "max-age=$STATIC_EXPIRES"; + my $no_cache_setting = "max-age=1, no-cache"; + $self->hook(after_dispatch => sub { + my $c = shift; + + # Only process if file requested is under webwork2_files + unless ( $c->req->url->path =~ '^/webwork2_files' ) { + return; + } + + if ( $c->req->url->path =~ '/tmp/renderer' ) { + # Treat problem generated files as already expired. + # They should not be cached. + $c->res->headers->cache_control( $no_cache_setting ); + $c->res->headers->header(Expires => + Mojo::Date->new(time - 86400) # expired 24 hours ago + ); + } else { + # Standard "static" files. + # They can be cached + $c->res->headers->cache_control( $cache_control_setting ); + $c->res->headers->header(Expires => + Mojo::Date->new(time + $STATIC_EXPIRES) + ); + } + }); + } + # Models $self->helper(newProblem => sub { shift; RenderApp::Model::Problem->new(@_) }); diff --git a/render_app.conf.dist b/render_app.conf.dist index a7c538308..d5675c618 100644 --- a/render_app.conf.dist +++ b/render_app.conf.dist @@ -6,6 +6,7 @@ webworkJWTsecret => 'private', SITE_HOST => 'http://localhost:3000', CORS_ORIGIN => '*', + STATIC_EXPIRES => 86400, STRICT_JWT => 0, hypnotoad => { listen => ['http://*:3000'], From d7960298c109b0460ba8a2bca6101f93b7d6e9ba Mon Sep 17 00:00:00 2001 From: "K. Andrew Parker" Date: Mon, 24 Jan 2022 11:18:58 -0500 Subject: [PATCH 2/2] Minor styling changes --- lib/RenderApp.pm | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/RenderApp.pm b/lib/RenderApp.pm index f3b97c4d1..d206026da 100644 --- a/lib/RenderApp.pm +++ b/lib/RenderApp.pm @@ -73,16 +73,14 @@ sub startup { if (my $STATIC_EXPIRES = $self->config('STATIC_EXPIRES')) { $STATIC_EXPIRES = int( $STATIC_EXPIRES ); my $cache_control_setting = "max-age=$STATIC_EXPIRES"; - my $no_cache_setting = "max-age=1, no-cache"; + my $no_cache_setting = 'max-age=1, no-cache'; $self->hook(after_dispatch => sub { my $c = shift; # Only process if file requested is under webwork2_files - unless ( $c->req->url->path =~ '^/webwork2_files' ) { - return; - } + return unless ($c->req->url->path =~ '^/webwork2_files/'); - if ( $c->req->url->path =~ '/tmp/renderer' ) { + if ($c->req->url->path =~ '/tmp/renderer') { # Treat problem generated files as already expired. # They should not be cached. $c->res->headers->cache_control( $no_cache_setting );