Skip to content

Instantly share code, notes, and snippets.

@evansd
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save evansd/0e38db4294f003493be3 to your computer and use it in GitHub Desktop.

Select an option

Save evansd/0e38db4294f003493be3 to your computer and use it in GitHub Desktop.
"""
Extends WhiteNoise so that it always re-checks the mtimes of files before serving
(and updates Content-Length headers as well).
Possibly useful if you are using WhiteNoise in development and want to modify files
without restarting the server. (Note: this won't pick up added/deleted files, you'll
still need a restart for that.)
Use the Django setting `WHITENOISE_RECHECK_MTIMES` to enable.
Don't enable in production (for one thing, it disables gzip support).
"""
class WhiteNoiseMtimes(DjangoWhiteNoise):
recheck_mtimes = False
config_attrs = DjangoWhiteNoise.config_attrs + ('recheck_mtimes',)
def file_not_modified(self, static_file, environ):
if self.recheck_mtimes:
# We make no attempt to update the gzip_headers so just
# disable gzip support to avoid possibly confusing bugs
static_file.gzip_path = None
# `url` is passed to all `all_XXX_headers` methods but isn't actually
# used in this case
self.add_stat_headers(self, static_file, url=None)
return super(WhiteNoiseMtimes, self).file_not_modified(static_file, environ)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment