Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Improve dev caching by naming static files based on mtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
chromakode committed Mar 21, 2013
1 parent b3436f3 commit 643afc0
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions r2/r2/lib/template_helpers.py
Expand Up @@ -37,6 +37,18 @@
from pylons.i18n import _, ungettext from pylons.i18n import _, ungettext




def static_mtime(path):
static_dirs = set(plugin.static_dir for plugin in g.plugins)
static_dirs.add(g.paths['static_files'])

for static_dir in static_dirs:
file_path = os.path.join(static_dir, path.lstrip('/'))
try:
return os.path.getmtime(file_path)
except OSError:
continue


static_text_extensions = { static_text_extensions = {
'.js': 'js', '.js': 'js',
'.css': 'css', '.css': 'css',
Expand All @@ -56,31 +68,28 @@ def static(path, allow_gzip=True):
is_text = extension in static_text_extensions is_text = extension in static_text_extensions
can_gzip = is_text and 'gzip' in request.accept_encoding can_gzip = is_text and 'gzip' in request.accept_encoding
should_gzip = allow_gzip and can_gzip should_gzip = allow_gzip and can_gzip
should_cache_bust = False


path_components = [] path_components = []
actual_filename = None actual_filename = None


if not c.secure and g.static_domain: if not c.secure and g.static_domain:
scheme = 'http' scheme = 'http'
domain = g.static_domain domain = g.static_domain
query = None
suffix = '.gzip' if should_gzip and g.static_pre_gzipped else '' suffix = '.gzip' if should_gzip and g.static_pre_gzipped else ''
elif c.secure and g.static_secure_domain: elif c.secure and g.static_secure_domain:
scheme = 'https' scheme = 'https'
domain = g.static_secure_domain domain = g.static_secure_domain
query = None
suffix = '.gzip' if should_gzip and g.static_secure_pre_gzipped else '' suffix = '.gzip' if should_gzip and g.static_secure_pre_gzipped else ''
else: else:
path_components.append(c.site.static_path) path_components.append(c.site.static_path)
query = None


if g.uncompressedJS: if g.uncompressedJS:
query = 'v=' + str(random.randint(1, 1000000))

# unminified static files are in type-specific subdirectories # unminified static files are in type-specific subdirectories
if not dirname and is_text: if not dirname and is_text:
path_components.append(static_text_extensions[extension]) path_components.append(static_text_extensions[extension])


should_cache_bust = True
actual_filename = filename actual_filename = filename


scheme = None scheme = None
Expand All @@ -93,6 +102,12 @@ def static(path, allow_gzip=True):
path_components.append(actual_filename + suffix) path_components.append(actual_filename + suffix)


actual_path = os.path.join(*path_components) actual_path = os.path.join(*path_components)

query = None
if should_cache_bust:
file_id = static_mtime(actual_path) or random.randint(0, 1000000)
query = 'v=' + str(file_id)

return urlparse.urlunsplit(( return urlparse.urlunsplit((
scheme, scheme,
domain, domain,
Expand Down

0 comments on commit 643afc0

Please sign in to comment.