cache control on static files

same principle as used in IPython,
slightly different implementation.
This commit is contained in:
Min RK
2015-01-28 14:21:43 -08:00
parent dee8d4af00
commit 7753428dd7
4 changed files with 35 additions and 2 deletions

View File

@@ -41,7 +41,9 @@ from IPython.config import Application, catch_config_error
here = os.path.dirname(__file__)
import jupyterhub
from . import handlers, apihandlers
from .handlers.static import CacheControlStaticFilesHandler
from . import orm
from ._data import DATA_FILES_PATH
@@ -201,7 +203,6 @@ class JupyterHub(Application):
help="The base URL of the entire application"
)
jinja_environment_options = Dict(config=True,
help="Supply extra arguments that will be passed to Jinja environment."
)
@@ -701,6 +702,14 @@ class JupyterHub(Application):
login_url = self.authenticator.login_url(base_url)
logout_url = self.authenticator.logout_url(base_url)
# if running from git, disable caching of require.js
# otherwise cache based on server start time
parent = os.path.dirname(os.path.dirname(jupyterhub.__file__))
if os.path.isdir(os.path.join(parent, '.git')):
version_hash = ''
else:
version_hash=datetime.now().strftime("%Y%m%d%H%M%S"),
settings = dict(
config=self.config,
log=self.log,
@@ -716,8 +725,10 @@ class JupyterHub(Application):
logout_url=logout_url,
static_path=os.path.join(self.data_files_path, 'static'),
static_url_prefix=url_path_join(self.hub.server.base_url, 'static/'),
static_handler_class=CacheControlStaticFilesHandler,
template_path=template_path,
jinja2_env=jinja_env,
version_hash=version_hash,
)
# allow configured settings to have priority
settings.update(self.tornado_settings)

View File

@@ -38,7 +38,11 @@ class BaseHandler(RequestHandler):
@property
def base_url(self):
return self.settings.get('base_url', '/')
@property
def version_hash(self):
return self.settings.get('version_hash', '')
@property
def db(self):
return self.settings['db']
@@ -240,6 +244,7 @@ class BaseHandler(RequestHandler):
login_url=self.settings['login_url'],
logout_url=self.settings['logout_url'],
static_url=self.static_url,
version_hash=self.version_hash,
)
def write_error(self, status_code, **kwargs):

View File

@@ -0,0 +1,14 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from tornado.web import StaticFileHandler
class CacheControlStaticFilesHandler(StaticFileHandler):
"""StaticFileHandler subclass that sets Cache-Control: no-cache without `?v=`
rather than relying on default browser cache behavior.
"""
def set_extra_headers(self, path):
if "v" not in self.request.arguments:
self.add_header("Cache-Control", "no-cache")

View File

@@ -35,6 +35,9 @@
<script src="{{static_url("components/requirejs/require.js") }}" type="text/javascript" charset="utf-8"></script>
<script>
require.config({
{% if version_hash %}
urlArgs: "v={{version_hash}}",
{% endif %}
baseUrl: '{{static_url("js", include_version=False)}}',
paths: {
components: '../components',