WordPress.org

Make WordPress Core

Opened 8 months ago

Last modified 7 months ago

#39695 new enhancement

Add preload headers in redirects

Reported by: onnimonni Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: HTTP API Keywords: has-patch needs-unit-tests
Focuses: Cc:

Description

http2 push enabled servers can immediately push linked assets or documents for the client. This saves clients from one round-trip.

At the moment at least Cloudflare and h2o web server can http2 server push assets defined in Link headers: https://www.w3.org/TR/preload/#server-push-http-2.

I believe more will follow in the next few years. WordPress powers so much of the web that it's our responsibility to try to make it as fast as we can.

This is usually quite hard topic because we have no idea what is stored in the client browser cache.

We can start implementing the Link preload headers in internal redirects because in that situation it's quite obvious that the client wants to make that request as their next one.

This is what I want to achieve

$ curl --http2 -i https://wordpress.test/wp-admin/
HTTP/2 302
...
Location: https://laextra.test/wp-login.php?redirect_to=https%3A%2F%2Flaextra.test%2Fwp-admin%2F&reauth=1
Link: </wp-login.php?redirect_to=https%3A%2F%2Flaextra.test%2Fwp-admin%2F&reauth=1>; rel=preload; as=document

I created a patch which adds these headers automatically in wp_redirect() function.

Links about http2 server push:
https://blog.cloudflare.com/announcing-support-for-http-2-server-push-2/
https://h2o.examp1e.net/configure/http2_directives.html

Attachments (1)

pluggable.patch (1.8 KB) - added by onnimonni 8 months ago.
Patch for pluggable.php

Download all attachments as: .zip

Change History (5)

@onnimonni
8 months ago

Patch for pluggable.php

#1 in reply to: ↑ description @onnimonni
8 months ago

Typo edit: The curl example shouldn't have any laextra.test references so just read them as wordpress.test.

#2 @johnbillion
7 months ago

  • Keywords has-patch needs-unit-tests added

I quite like this idea. The logic in wp_maybe_add_redirect_preload_headers() needs some tests though, so I recommend altering this function so it returns a URL instead of outputting the header directly. This means the function can have unit tests written against it, and the actual header can then be output in wp_redirect().

#3 @onnimonni
7 months ago

@johnbillion Super cool to hear that you are interested in this.

This was just a proof of concept. I can change the logic to use 3 functions like this instead:

<?php
/**
 * Checks if this url is internal url. This is be used to check if asset/document can be preloaded.
 *
 * @param $url string
 *
 * @return bool
 */
function is_internal_wp_url($url) {}

/** 
 * Creates preload header as string so we can have unit tests
 *
 * @param $url string - asset or document to preload
 * @param $type string - preload type
 *
 * @return string - Header string which can be used with header() function
 */
function wp_create_preload_header($url, $type = "document") {}

/** 
 * Convenience wrapper for wp_create_preload_header() which adds new preload header
 *
 * @param $url string - asset or document to preload
 * @param $type string - preload type
 *
 * @return void
 */
function wp_add_preload_header($url, $type = "document") {}

Do you have preference in naming these?

Last edited 7 months ago by onnimonni (previous) (diff)

#4 @johnbillion
7 months ago

Cool, let's go with those function names for now.

Note: See TracTickets for help on using tickets.