mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-18 07:23:00 +00:00
cache control on static files
same principle as used in IPython, slightly different implementation.
This commit is contained in:
@@ -41,7 +41,9 @@ from IPython.config import Application, catch_config_error
|
|||||||
|
|
||||||
here = os.path.dirname(__file__)
|
here = os.path.dirname(__file__)
|
||||||
|
|
||||||
|
import jupyterhub
|
||||||
from . import handlers, apihandlers
|
from . import handlers, apihandlers
|
||||||
|
from .handlers.static import CacheControlStaticFilesHandler
|
||||||
|
|
||||||
from . import orm
|
from . import orm
|
||||||
from ._data import DATA_FILES_PATH
|
from ._data import DATA_FILES_PATH
|
||||||
@@ -201,7 +203,6 @@ class JupyterHub(Application):
|
|||||||
help="The base URL of the entire application"
|
help="The base URL of the entire application"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
jinja_environment_options = Dict(config=True,
|
jinja_environment_options = Dict(config=True,
|
||||||
help="Supply extra arguments that will be passed to Jinja environment."
|
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)
|
login_url = self.authenticator.login_url(base_url)
|
||||||
logout_url = self.authenticator.logout_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(
|
settings = dict(
|
||||||
config=self.config,
|
config=self.config,
|
||||||
log=self.log,
|
log=self.log,
|
||||||
@@ -716,8 +725,10 @@ class JupyterHub(Application):
|
|||||||
logout_url=logout_url,
|
logout_url=logout_url,
|
||||||
static_path=os.path.join(self.data_files_path, 'static'),
|
static_path=os.path.join(self.data_files_path, 'static'),
|
||||||
static_url_prefix=url_path_join(self.hub.server.base_url, 'static/'),
|
static_url_prefix=url_path_join(self.hub.server.base_url, 'static/'),
|
||||||
|
static_handler_class=CacheControlStaticFilesHandler,
|
||||||
template_path=template_path,
|
template_path=template_path,
|
||||||
jinja2_env=jinja_env,
|
jinja2_env=jinja_env,
|
||||||
|
version_hash=version_hash,
|
||||||
)
|
)
|
||||||
# allow configured settings to have priority
|
# allow configured settings to have priority
|
||||||
settings.update(self.tornado_settings)
|
settings.update(self.tornado_settings)
|
||||||
|
@@ -38,7 +38,11 @@ class BaseHandler(RequestHandler):
|
|||||||
@property
|
@property
|
||||||
def base_url(self):
|
def base_url(self):
|
||||||
return self.settings.get('base_url', '/')
|
return self.settings.get('base_url', '/')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def version_hash(self):
|
||||||
|
return self.settings.get('version_hash', '')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def db(self):
|
def db(self):
|
||||||
return self.settings['db']
|
return self.settings['db']
|
||||||
@@ -240,6 +244,7 @@ class BaseHandler(RequestHandler):
|
|||||||
login_url=self.settings['login_url'],
|
login_url=self.settings['login_url'],
|
||||||
logout_url=self.settings['logout_url'],
|
logout_url=self.settings['logout_url'],
|
||||||
static_url=self.static_url,
|
static_url=self.static_url,
|
||||||
|
version_hash=self.version_hash,
|
||||||
)
|
)
|
||||||
|
|
||||||
def write_error(self, status_code, **kwargs):
|
def write_error(self, status_code, **kwargs):
|
||||||
|
14
jupyterhub/handlers/static.py
Normal file
14
jupyterhub/handlers/static.py
Normal 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")
|
||||||
|
|
@@ -35,6 +35,9 @@
|
|||||||
<script src="{{static_url("components/requirejs/require.js") }}" type="text/javascript" charset="utf-8"></script>
|
<script src="{{static_url("components/requirejs/require.js") }}" type="text/javascript" charset="utf-8"></script>
|
||||||
<script>
|
<script>
|
||||||
require.config({
|
require.config({
|
||||||
|
{% if version_hash %}
|
||||||
|
urlArgs: "v={{version_hash}}",
|
||||||
|
{% endif %}
|
||||||
baseUrl: '{{static_url("js", include_version=False)}}',
|
baseUrl: '{{static_url("js", include_version=False)}}',
|
||||||
paths: {
|
paths: {
|
||||||
components: '../components',
|
components: '../components',
|
||||||
|
Reference in New Issue
Block a user