Skip to content

Commit

Permalink
Rename identifiers related to preload Link header to reflect curren…
Browse files Browse the repository at this point in the history
…t Cloudflare behavior

HTTP/2 server push has been dropped by Cloudflare and all browsers.
  • Loading branch information
charmander committed Dec 19, 2024
1 parent d4b63eb commit 3ccbb6b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
24 changes: 9 additions & 15 deletions weasyl/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion weasyl/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 3ccbb6b

Please sign in to comment.