Skip to content

Commit

Permalink
cleanup: Simplify construction of preload Link header
Browse files Browse the repository at this point in the history
  • Loading branch information
charmander committed Dec 19, 2024
1 parent 3ccbb6b commit 4f565cc
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions weasyl/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,34 +298,31 @@ def cleanup(request):
return database_session_cleanup_tween


def _generate_preload_link() -> str:
"""
Generates the `Link` header to preload resources needed on every webpage.
"""
css_preload = [
'<' + item + '>; rel=preload; as=style' for item in [
d.get_resource_path('css/site.css'),
# The value of the `Link` header that will be set in the `preload_tween_factory` function below.
# Only constructed once on application load; not affected by `WEASYL_RELOAD_ASSETS`.
_WEBPAGE_PRELOADS_LINK = ", ".join([
# CSS
*(
f"<{d.get_resource_path(item)}>;rel=preload;as=style" for item in [
"css/site.css",
]
]
),

js_preload = [
'<' + item + '>; rel=preload; as=script' for item in [
d.get_resource_path('js/jquery-2.2.4.min.js'),
d.get_resource_path('js/scripts.js'),
# JavaScript
*(
f"<{d.get_resource_path(item)}>;rel=preload;as=script" for item in [
"js/jquery-2.2.4.min.js",
"js/scripts.js",
]
]
),

esm_preload = [
'<' + item + '>; rel=modulepreload' for item in [
d.get_resource_path('js/main.js'),
# ES modules
*(
f"<{d.get_resource_path(item)}>;rel=modulepreload" for item in [
"js/main.js",
]
]

return ", ".join(css_preload + js_preload + esm_preload)


# part of the `Link` header that will be set in the `preload_tween_factory` function below
_WEBPAGE_PRELOADS_LINK = _generate_preload_link()
),
])


def preload_tween_factory(handler, registry):
Expand Down

0 comments on commit 4f565cc

Please sign in to comment.