浏览器是否在重定向响应中使用资源提示 (Link) headers?
Do browsers use resource hint (Link) headers in redirect responses?
问题
我有一个网站,它为新访问者使用 302 重定向来建立新的 session。我在我的个人网站上创建了一个基本流程的演示:
- https://crenshaw.dev/redirect/index.php (302)
- https://crenshaw.dev/session/redirect/index.html (200)
index.html 使用资源 https://mac9416.com/demos/style.css
(我只需要一个外部域名)。
我想指示浏览器在 302 而不是 200 中连接到 mac9416.com。
可能的解决方案
一些浏览器支持资源提示 headers。以下是一些示例:
EXAMPLE 5
Link: https://widget.com; rel=dns-prefetch
Link: https://example.com; rel=preconnect
Link: https://example.com/next-page.html; rel=prerender;
Link: https://example.com/logo-hires.jpg; rel=prefetch; as=image;
出了什么问题
开发工具在初始请求中显示 Link
header:
我看到 mac9416.com 的连接是在 200 之后建立的,就在资源下载之前。
我希望看到连接在 302 之后立即建立,或者只是从时间轴中消失了。
我使用 Chrome 73 进行了测试。WebPageTest waterfall 看起来几乎相同。
注意: 我对 server-push 解决方案很感兴趣,但目前该站点运行 HTTP1.1。
问题
我是在尝试一些甚至不受支持的事情吗?必须仅在 non-redirect 响应中提供资源提示 header 吗?
The "Anonymizing redirect" section of the resource hint working draft 让我相信它可以帮助解决我的情况。但也许我看错了。
Many sites rely on redirect services for analytics, malware protection, and to anonymize the referrer before sending the user to the final destination. Because the destination is known ahead of time, a preconnect hint can be used to initiate the connection handshake with the destination origin (without revealing any private information) in parallel with the processing of the redirect - this masks the redirect latency and reduces navigation time to final destination.
好吧,我检查了你的实验(感谢你把它搞定了!),没有发现任何问题。我也在我自己的服务器上自己重复了一遍,可以证实你的发现:none 的主要浏览器似乎支持跨页面重定向的资源提示。
关于您指出的规范草案中的段落,我可以想到两种解释:
- 这个是一个潜在的用例,浏览器还不支持它。
- 此措辞更适用于当前页面上必须先通过重定向的资源。因此,如果当前页面 (index.html) 加载
www.example.com/latest/framework.js
并重定向到 cdn.example.com/latest/framework.js
以实际加载此资源。值得注意的是,只有 prefetch 和 prerender 明确指出它们适用于 "next navigation"(尽管我出于兴趣尝试了这两个,但它们也不适用于重定向场景)。
无论哪种方式,我都同意它可以更清楚。我打算建议在 Github 上打开一个问题,但请参阅 you already did that!希望规范作者可以证实这一点。
顺便说一句,我尝试使用 JavaScript 重定向而不是 301/302,它在 Chrome 中适用于 HTTP link header 和 HTML link 预连接说明 - 再次建议仅查看 200 个响应以获取资源提示。
你提出的一个有趣的问题!不确定 Whosebug 是否离题但肯定激起了我的兴趣...
问题
我有一个网站,它为新访问者使用 302 重定向来建立新的 session。我在我的个人网站上创建了一个基本流程的演示:
- https://crenshaw.dev/redirect/index.php (302)
- https://crenshaw.dev/session/redirect/index.html (200)
index.html 使用资源 https://mac9416.com/demos/style.css
(我只需要一个外部域名)。
我想指示浏览器在 302 而不是 200 中连接到 mac9416.com。
可能的解决方案
一些浏览器支持资源提示 headers。以下是一些示例:
EXAMPLE 5
Link: https://widget.com; rel=dns-prefetch
Link: https://example.com; rel=preconnect
Link: https://example.com/next-page.html; rel=prerender;
Link: https://example.com/logo-hires.jpg; rel=prefetch; as=image;
出了什么问题
开发工具在初始请求中显示 Link
header:
我看到 mac9416.com 的连接是在 200 之后建立的,就在资源下载之前。
我希望看到连接在 302 之后立即建立,或者只是从时间轴中消失了。
我使用 Chrome 73 进行了测试。WebPageTest waterfall 看起来几乎相同。
注意: 我对 server-push 解决方案很感兴趣,但目前该站点运行 HTTP1.1。
问题
我是在尝试一些甚至不受支持的事情吗?必须仅在 non-redirect 响应中提供资源提示 header 吗?
The "Anonymizing redirect" section of the resource hint working draft 让我相信它可以帮助解决我的情况。但也许我看错了。
Many sites rely on redirect services for analytics, malware protection, and to anonymize the referrer before sending the user to the final destination. Because the destination is known ahead of time, a preconnect hint can be used to initiate the connection handshake with the destination origin (without revealing any private information) in parallel with the processing of the redirect - this masks the redirect latency and reduces navigation time to final destination.
好吧,我检查了你的实验(感谢你把它搞定了!),没有发现任何问题。我也在我自己的服务器上自己重复了一遍,可以证实你的发现:none 的主要浏览器似乎支持跨页面重定向的资源提示。
关于您指出的规范草案中的段落,我可以想到两种解释:
- 这个是一个潜在的用例,浏览器还不支持它。
- 此措辞更适用于当前页面上必须先通过重定向的资源。因此,如果当前页面 (index.html) 加载
www.example.com/latest/framework.js
并重定向到cdn.example.com/latest/framework.js
以实际加载此资源。值得注意的是,只有 prefetch 和 prerender 明确指出它们适用于 "next navigation"(尽管我出于兴趣尝试了这两个,但它们也不适用于重定向场景)。
无论哪种方式,我都同意它可以更清楚。我打算建议在 Github 上打开一个问题,但请参阅 you already did that!希望规范作者可以证实这一点。
顺便说一句,我尝试使用 JavaScript 重定向而不是 301/302,它在 Chrome 中适用于 HTTP link header 和 HTML link 预连接说明 - 再次建议仅查看 200 个响应以获取资源提示。
你提出的一个有趣的问题!不确定 Whosebug 是否离题但肯定激起了我的兴趣...