是否可以在不同域之间进行http2 推送资产?
Is it possible to http2 push assets among different domains?
我有主站点 (site.com) 和我的资源 (css,images,js) 所在的站点 (somecdn.com)。我想在登录之前推送一些资源。我发送 ajax 请求并在响应头中设置 Link-s 和资源。但是我的资源在等待且没有任何推送的情况下下载(见附图)。如何为 somecdn.com 中的资源启用 http2 推送?
with push
without push
我在 docker 中创建了两个站点:site.com 和 somecdn.com 并检查 http2 推送是否在这两个站点上正常工作(并且分别按预期工作)。
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);
我希望在 Chrome 开发工具资源中看到而无需等待,并且有 "Push" 指示。
不可以为另一个域推送资源。来自 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.
您可以使用那些 headers 预加载请求,这将提示浏览器以高优先级获取它们,甚至在它稍后看到对资源的实际请求之前。
事实上,很多人都推荐预加载而不是推送,因为 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。
在 HTTP/2 下通常不鼓励使用这样的分片域,因为它失去了 HTTP/2 的许多好处,HTTP/2 出于性能原因旨在移动到单个连接。请参阅此博客 post 以获得对此的很好解释:https://csswizardry.com/2019/05/self-host-your-static-assets/
我有主站点 (site.com) 和我的资源 (css,images,js) 所在的站点 (somecdn.com)。我想在登录之前推送一些资源。我发送 ajax 请求并在响应头中设置 Link-s 和资源。但是我的资源在等待且没有任何推送的情况下下载(见附图)。如何为 somecdn.com 中的资源启用 http2 推送? with push without push
我在 docker 中创建了两个站点:site.com 和 somecdn.com 并检查 http2 推送是否在这两个站点上正常工作(并且分别按预期工作)。
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);
我希望在 Chrome 开发工具资源中看到而无需等待,并且有 "Push" 指示。
不可以为另一个域推送资源。来自 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.
您可以使用那些 headers 预加载请求,这将提示浏览器以高优先级获取它们,甚至在它稍后看到对资源的实际请求之前。
事实上,很多人都推荐预加载而不是推送,因为 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。
在 HTTP/2 下通常不鼓励使用这样的分片域,因为它失去了 HTTP/2 的许多好处,HTTP/2 出于性能原因旨在移动到单个连接。请参阅此博客 post 以获得对此的很好解释:https://csswizardry.com/2019/05/self-host-your-static-assets/