我已经在 http2 上配置了我的网站,但即使阅读了这么多文章,我也无法弄清楚它的实现。
我已从网站的头文件中删除了 common.css
。并将这些行添加到 .htaccess
文件中。
<IfModule http2_module>
#Check if there's a cookie saying the css has already been loaded:
SetEnvIf Cookie "cssloaded=1" cssloaded
#If no cookie, and it's an html file, then push the css file
#and set a session level cookie so next time it won't be pushed:
<filesMatch "\.([pP][hH][pP]?)">
Header add Link "</assets/css/common.css>;rel=preload;as=style" env=!cssloaded
Header add Set-Cookie "cssloaded=1; Path=/; Secure; HttpOnly" env=!cssloaded
</filesMatch>
</IfModule>
但是我的网站根本没有加载common.css
。它坏了。我的网站位于 Apache 服务器后面,网站完全构建在 codeIgniter 上。
我也将这些添加到我的 common_head.php
文件中
<?php
header: header('Link: </assets/css/jquery-ui.css>; rel=preload; as=style,</assets/css/jquery.mCustomScrollbar.min.css>; rel=preload; as=style,</assets/css/slick.min.css>; rel=preload; as=style,</assets/css/slick-theme.min.css>; rel=preload; as=style,</assets/css/bootstrap.min.css>; rel=preload; as=style,</assets/css/common.css>; rel=preload; as=style,,</assets/css/jplayer.blue.monday.min.css>; rel=preload; as=style');
?>
现在我可以在检查元素中看到所有CSS文件,也可以看到initialtor Push/其他,但它没有应用在页面上。页面已损坏。 Apache服务器:2.4.6
请告诉我我哪里做错了?
最佳答案
这段代码看起来很熟悉!很高兴知道my blog post事实证明很有用:-)
I have removed common.css from header file of my website. and added the these lines to .htaccess file.
这就是你出错的地方。它需要在 HEAD 中正常引用并推送。
当浏览器看到 common.css
引用时,它会去获取它并看到它已经被推送,并且只使用推送的资源。
如果没有引用,服务器将推送它,但浏览器将忽略它。
注意 HTTP/2 Push 很复杂,并且有很多类似的事情可能会出错。有关更多信息,请参阅此帖子:https://jakearchibald.com/2017/h2-push-tougher-than-i-thought/ 。许多人说,使用它的 yield 不值得,而且无论如何, yield 都是值得怀疑的,因为你很容易过度推送并导致页面加载速度变慢而不是更快。
关于php - 在apache php上实现http2推送方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56990794/