I have main site(site.com) and site where my resources(css,images,js) are located(somecdn.com). I want before login push some of this resources. I send ajax request and in response header set Link-s with resources. But my resources downloading with waiting and without any pushing(see attached picture). How can I enable http2 push for resources in somecdn.com? with push without push
I create in docker two sites: site.com and somecdn.com and check that http2 push works correctly on both of them (and separately it works as supposed).
https://127.0.0.1:8081/index.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>IndexPHP</h1></iframe>-->
<script>
window.onload = function () {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://127.0.0.1:8083/push.php', true);
xhr.send();
}
</script>
</body>
</html>
https://127.0.0.1:8083/push.php
<?php
header("Link: </style.css>; rel=preload; as=style", false);
header("Link: </image.png>; rel=preload; as=image", false);
I expect to see in Chrome dev-tools resources without waiting and with "Push" indication.
No it is not possible to push resources for another domain. From the HTTP/2 spec:
The server MUST include a value in the ":authority" pseudo-header field for which the server is authoritative (see Section 10.1). A client MUST treat a PUSH_PROMISE for which the server is not authoritative as a stream error (Section 5.4.2) of type PROTOCOL_ERROR.
You can preload the requests with those headers which will give the browser the hint to fetch them with a high priority even before it sees the actual requests for the resources later.
In fact many people recommend preload over push anyway, as push is very complex, not cache aware so too easy to abuse by over-pushing so causing a performance penalty, and so the benefits of it are questionable. That is why usage of HTTP/2 push remains so low.
Use of a sharded domain like this is generally discouraged under HTTP/2 as it loses many of the benefits of HTTP/2 which aims to move to a single connection for performance reasons. See this blog post for a good explanation on this: https://csswizardry.com/2019/05/self-host-your-static-assets/