gpt4 book ai didi

html - 优化首屏 CSS

转载 作者:行者123 更新时间:2023-11-28 17:43:16 25 4
gpt4 key购买 nike

我想修复“消除首屏内容中阻止呈现的 JavaScript 和 CSS”要求以获得更好的效果 PageSpeed Insights得分,但我不太确定解决此问题的最佳方法是什么。

  • 如何才能最好地平衡新访问者和回访者的页面负载?
  • 什么时候应该异步加载我的 CSS,什么时候不需要?
  • 我是否应该只为小屏幕内联 CSS?

相关介绍:Optimizing the Critical Rendering Path

例子

由于内联大量 CSS 会导致后续访问时页面加载速度变慢,因此我可以根据 cookie 为重复访问者提供不同的版本。为了检测首屏 CSS,我可以使用本文中的书签:paul.kinlan.me/detecting-critical-above-the-fold-css/

对于新访客:

<!DOCTYPE HTML>
<html>
<head>
<title>New Visitor</title>
<style><!-- insert above the fold css here --></style>
<noscript><link rel="stylesheet" href="style.css"></noscript>
</head>
<body>
<!-- insert content here -->
<script>
// load css
var node = document.createElement('link');
node.rel = 'stylesheet';
node.href = 'style.css';
document.head.appendChild(node);
// set cookie
var exp = new Date();
exp.setTime(exp.getTime() + 3600 * 1000);
document.cookie = 'returning=true; expires=' + exp.toUTCString() + '; path=/';
</script>
</body>
</html>

对于回头客:

<!DOCTYPE HTML>
<html>
<head>
<title>Returning Visitor</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- content here -->
</body>
</html>

这种方法有什么问题吗?

最佳答案

存在过度优化这样的事情。您的示例方法增加了不必要的复杂性。您应该关注的是头部部分的最小样式表,它将使首屏上方的所有内容都有意义并符合设计和布局(但不一定 100% 尊重它)。

对于页面的其余部分,只需加载页面末尾的其余 CSS,就可以了。如果对您来说重要的是首屏,那么您加载的第一个 CSS 也是如此。其他一切都可以延迟。

关于html - 优化首屏 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23705992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com