Skip to content

Commit

Permalink
Add cache control header and cache busting urls to components suites.
Browse files Browse the repository at this point in the history
  • Loading branch information
T4rk1n committed Sep 18, 2018
1 parent 846d071 commit edc08ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion dash/_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def env_configs():
'DASH_REQUESTS_PATHNAME_PREFIX',
'DASH_SUPPRESS_CALLBACK_EXCEPTIONS',
'DASH_ASSETS_EXTERNAL_PATH',
'DASH_INCLUDE_ASSETS_FILES'
'DASH_INCLUDE_ASSETS_FILES',
'DASH_COMPONENTS_CACHE_MAX_AGE'
)})


Expand Down
26 changes: 22 additions & 4 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def __init__(
external_scripts=None,
external_stylesheets=None,
suppress_callback_exceptions=None,
components_cache_max_age=None,
**kwargs):

# pylint-disable: too-many-instance-attributes
Expand Down Expand Up @@ -136,6 +137,9 @@ def __init__(
True),
'assets_external_path': _configs.get_config(
'assets_external_path', assets_external_path, env_configs, ''),
'components_cache_max_age': int(_configs.get_config(
'components_cache_max_age', components_cache_max_age,
env_configs, 2678400))
})

# list of dependencies
Expand Down Expand Up @@ -302,11 +306,18 @@ def _relative_url_path(relative_package_path='', namespace=''):
else:
self.registered_paths[namespace] = [relative_package_path]

return '{}_dash-component-suites/{}/{}?v={}'.format(
module_path = os.path.join(
os.path.dirname(sys.modules[namespace].__file__),
relative_package_path)

modified = int(os.stat(module_path).st_mtime)

return '{}_dash-component-suites/{}/{}?v={}&m={}'.format(
self.config['requests_pathname_prefix'],
namespace,
relative_package_path,
importlib.import_module(namespace).__version__
importlib.import_module(namespace).__version__,
modified
)

srcs = []
Expand Down Expand Up @@ -422,9 +433,16 @@ def serve_component_suites(self, package_name, path_in_package_dist):
'js': 'application/JavaScript',
'css': 'text/css'
})[path_in_package_dist.split('.')[-1]]

headers = {
'Cache-Control': 'public, max-age={}'.format(
self.config.components_cache_max_age)
}

return Response(
pkgutil.get_data(package_name, path_in_package_dist),
mimetype=mimetype
mimetype=mimetype,
headers=headers
)

def index(self, *args, **kwargs): # pylint: disable=unused-argument
Expand All @@ -435,7 +453,7 @@ def index(self, *args, **kwargs): # pylint: disable=unused-argument
title = getattr(self, 'title', 'Dash')
if self._favicon:
favicon = '<link rel="icon" type="image/x-icon" href="{}">'.format(
flask.url_for('assets.static', filename=self._favicon))
self.get_asset_url(self._favicon))
else:
favicon = ''

Expand Down

0 comments on commit edc08ab

Please sign in to comment.