Skip to content

Commit 19aa4d6

Browse files
brichetpre-commit-ci[bot]fcollonval
authored
Set Cache-control immutable for static files (#394)
* Set Cache-control immutable for static files * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Revert previous changes and use the jupyter_server feature instead --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Frédéric Collonval <[email protected]>
1 parent 6429fab commit 19aa4d6

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

jupyterlab_server/app.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
# Copyright (c) Jupyter Development Team.
44
# Distributed under the terms of the Modified BSD License.
55

6+
from glob import glob
7+
from os.path import relpath
8+
69
from jupyter_server.extension.application import ExtensionApp, ExtensionAppJinjaMixin
10+
from jupyter_server.utils import url_path_join as ujoin
711
from traitlets import Dict, Integer, Unicode, observe
812

913
from ._version import __version__
@@ -97,6 +101,27 @@ def _deprecated_trait(self, change):
97101
)
98102
setattr(self, new_attr, change.new)
99103

104+
def initialize_settings(self):
105+
"""Initialize the settings:
106+
107+
set the static files as immutable, since they should have all hashed name.
108+
"""
109+
immutable_cache = set(self.settings.get("static_immutable_cache", []))
110+
111+
# Set lab static files as immutables
112+
immutable_cache.add(self.static_url_prefix)
113+
114+
# Set extensions static files as immutables
115+
for extension_path in self.labextensions_path + self.extra_labextensions_path:
116+
extensions_url = [
117+
ujoin(self.labextensions_url, relpath(path, extension_path))
118+
for path in glob(f'{extension_path}/**/static', recursive=True)
119+
]
120+
121+
immutable_cache.update(extensions_url)
122+
123+
self.settings.update({"static_immutable_cache": list(immutable_cache)})
124+
100125
def initialize_templates(self):
101126
"""Initialize templates."""
102127
self.static_paths = [self.static_dir]

0 commit comments

Comments
 (0)