Skip to content

Commit

Permalink
Merge pull request #84 from taniwallach/add_cache_control_expires
Browse files Browse the repository at this point in the history
Add Cache-control and Expires headers for files under webwork2_files
  • Loading branch information
drdrew42 authored Jan 26, 2022
2 parents a3f99bd + d796029 commit e6dbddc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/RenderApp.pm
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@ 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
return unless ($c->req->url->path =~ '^/webwork2_files/');

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 e6dbddc

Please sign in to comment.