0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 03:31:04 +01:00

Disable querystrings if a storage backend with hashed filenames is active

This commit is contained in:
Matt Westcott 2019-08-22 16:26:13 +01:00
parent 8711050e49
commit 779620e53b

View File

@ -1,12 +1,34 @@
import hashlib
from django.conf import settings
from django.contrib.staticfiles.storage import HashedFilesMixin
from django.core.files.storage import get_storage_class
from django.templatetags.static import static
from wagtail import __version__
if getattr(settings, 'WAGTAILADMIN_STATIC_FILE_VERSION_STRINGS', True):
# Check whether we should add cache-busting '?v=...' parameters to static file URLs
try:
# If a preference has been explicitly stated in the WAGTAILADMIN_STATIC_FILE_VERSION_STRINGS
# setting, use that
use_version_strings = settings.WAGTAILADMIN_STATIC_FILE_VERSION_STRINGS
except AttributeError:
# If WAGTAILADMIN_STATIC_FILE_VERSION_STRINGS not specified, default to version strings
# enabled, UNLESS we're using a storage backend with hashed filenames; in this case having
# a query parameter is redundant, and in some configurations (e.g. Cloudflare with the
# "No Query String" setting) it could break a previously-working cache setup
if settings.DEBUG:
# Hashed filenames are disabled in debug mode, so keep the querystring
use_version_strings = True
else:
# see if we're using a storage backend using hashed filenames
storage = get_storage_class(settings.STATICFILES_STORAGE)
use_version_strings = not issubclass(storage, HashedFilesMixin)
if use_version_strings:
VERSION_HASH = hashlib.sha1(
(__version__ + settings.SECRET_KEY).encode('utf-8')
).hexdigest()[:8]