Skip to content

Commit

Permalink
Set Cache-control immutable for static files (#394)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Jun 13, 2023
1 parent 6429fab commit 19aa4d6
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions jupyterlab_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from glob import glob
from os.path import relpath

from jupyter_server.extension.application import ExtensionApp, ExtensionAppJinjaMixin
from jupyter_server.utils import url_path_join as ujoin
from traitlets import Dict, Integer, Unicode, observe

from ._version import __version__
Expand Down Expand Up @@ -97,6 +101,27 @@ def _deprecated_trait(self, change):
)
setattr(self, new_attr, change.new)

def initialize_settings(self):
"""Initialize the settings:
set the static files as immutable, since they should have all hashed name.
"""
immutable_cache = set(self.settings.get("static_immutable_cache", []))

# Set lab static files as immutables
immutable_cache.add(self.static_url_prefix)

# Set extensions static files as immutables
for extension_path in self.labextensions_path + self.extra_labextensions_path:
extensions_url = [
ujoin(self.labextensions_url, relpath(path, extension_path))
for path in glob(f'{extension_path}/**/static', recursive=True)
]

immutable_cache.update(extensions_url)

self.settings.update({"static_immutable_cache": list(immutable_cache)})

def initialize_templates(self):
"""Initialize templates."""
self.static_paths = [self.static_dir]
Expand Down

0 comments on commit 19aa4d6

Please sign in to comment.