diff --git a/weasyl/middleware.py b/weasyl/middleware.py index 6e09a79d3..99383e450 100644 --- a/weasyl/middleware.py +++ b/weasyl/middleware.py @@ -298,14 +298,9 @@ def cleanup(request): return database_session_cleanup_tween -def _generate_http2_server_push_headers(): +def _generate_preload_link() -> str: """ - Generates the Link headers to load HTTP/2 Server Push resources which are needed on each pageload. Written - as a separate function to only execute this code a single time, since we just need to generate this each - time the code is relaunched (e.g., each time the web workers are kicked to a new version of the code). - - A component of ``http2_server_push_tween_factory`` - :return: An ASCII encoded string to be loaded into the Link header set inside of ``http2_server_push_tween_factory`` + Generates the `Link` header to preload resources needed on every webpage. """ css_preload = [ '<' + item + '>; rel=preload; as=style' for item in [ @@ -329,23 +324,22 @@ def _generate_http2_server_push_headers(): return ", ".join(css_preload + js_preload + esm_preload) -# Part of the `Link` header that will be set in the `http2_server_push_tween_factory` function, below -HTTP2_LINK_HEADER_PRELOADS = _generate_http2_server_push_headers() +# part of the `Link` header that will be set in the `preload_tween_factory` function below +_WEBPAGE_PRELOADS_LINK = _generate_preload_link() -def http2_server_push_tween_factory(handler, registry): +def preload_tween_factory(handler, registry): """ - Add the 'Link' header to outgoing responses to HTTP/2 Server Push render-blocking resources + Add the `Link` header to outgoing responses to preload resources needed on every webpage, which is served ahead of time by Cloudflare as an HTTP 103 Early Hints message. """ - def http2_server_push(request): + def preload_tween(request): resp = handler(request) if resp.headers['Content-Type'].startswith("text/html"): - # Combined HTTP/2 headers indicating which resources to server push - resp.headers['Link'] = HTTP2_LINK_HEADER_PRELOADS + resp.headers['Link'] = _WEBPAGE_PRELOADS_LINK return resp - return http2_server_push + return preload_tween # Properties and methods to enhance the pyramid `request`. diff --git a/weasyl/wsgi.py b/weasyl/wsgi.py index 86209e272..3d948967b 100644 --- a/weasyl/wsgi.py +++ b/weasyl/wsgi.py @@ -27,7 +27,7 @@ config.add_tween("weasyl.middleware.db_timer_tween_factory") config.add_tween("weasyl.middleware.cache_clear_tween_factory") config.add_tween("weasyl.middleware.database_session_cleanup_tween_factory") -config.add_tween("weasyl.middleware.http2_server_push_tween_factory") +config.add_tween("weasyl.middleware.preload_tween_factory") config.add_tween("weasyl.middleware.query_debug_tween_factory") config.add_tween("pyramid.tweens.excview_tween_factory") # Required to catch exceptions thrown in tweens. config.add_tween("weasyl.middleware.utf8_path_tween_factory")