Skip to content

Commit

Permalink
Add a setting via render_app.conf.dist to set static files from
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
taniwallach committed Jan 23, 2022
1 parent a3f99bd commit 21d0dab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/RenderApp.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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(@_) });

Expand Down
1 change: 1 addition & 0 deletions render_app.conf.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
webworkJWTsecret => 'private',
SITE_HOST => 'http://localhost:3000',
CORS_ORIGIN => '*',
STATIC_EXPIRES => 86400,
STRICT_JWT => 0,
hypnotoad => {
listen => ['http://*:3000'],
Expand Down

0 comments on commit 21d0dab

Please sign in to comment.