这是我为测试cookie同意代码的影响而创建的非常基本的html.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="description" content="test page speed">   
  <title>Speed test 1</title>
  <meta content="width=device-width, initial-scale=1" name="viewport">
  <link href="favicon.png" rel="shortcut icon" type="image/x-icon">
  
    <!-- BEGIN Cookie Consent -->
    <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.css"/>
    <script src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.js"></script>
    <script>
    window.addEventListener("load", function(){
    window.cookieconsent.initialise({
      "palette": {
        "popup": {
          "background": "#eee",
          "text": "#000"
        },
        "button": {
          "background": "#0f0",
          "text": "#ffffff"
        }
      }
    })});
    </script>
    <!-- END Cookie Consent -->

</head>

<body class="body">
<h1>Hello!</h1>

<p>
This is a test.
</p>

</body>
</html>

如果没有Cookie同意部分,FCP为0.8秒(根据Google的PageSpeed Insights测量).

对于Cookie同意部分,FCP为1.9秒.性能大幅下降!

如何才能在1秒左右返回FCP?

推荐答案

为了最大限度地减少Cookie同意代码对性能的影响,您可以考虑使用HTML <script> async Attribute以异步方式加载Cookie同意脚本.

<script async src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.js"></script>

您可以try 最小化/组合外部资源.不幸的是,如果不能访问您的Cookie同意库的源代码,您就不能直接最小化或组合这些资源.但你可以确保你正在使用minified versions,就像你已经在使用的那样.如果需要进一步优化,可以考虑自己托管这些文件.

您还可以将Cookie同意的初始化推迟到DOMContentLoaded event之后.这样可以确保初始化代码不会阻止页面的呈现.

<script>
document.addEventListener('DOMContentLoaded', function () {
    if(window.cookieconsent) {
    window.cookieconsent.initialise({
        "palette": {
        "popup": {
            "background": "#eee",
            "text": "#000"
        },
        "button": {
            "background": "#0f0",
            "text": "#ffffff"
        }
        }
    });
    }
});
</script>

这也说明了在初始化cookie同意时的一个良好实践:通过判断window.cookieconsent是否可用(如this example中),您可以避免潜在的错误,并确保脚本仅在必要时运行.


你能解释一下为什么你推荐用"DOMContentLoaded"而不是"load"吗?

The "DOMContentLoaded" event is fired when the HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. That means your script will run as soon as the HTML is fully parsed, but potentially before all external resources (like images or stylesheets) have loaded.
By using this event, you make sure your cookie consent code runs earlier in the page load process, which can be advantageous because it does not have to wait for all resources to load. That can lead to a quicker interactive state for the page, improving user experience, especially on slower connections or devices.

The "load" event, on the other hand, waits for the complete page to load, including all dependent resources like stylesheets and images. That event is fired much later in the page lifecycle.
If you initialize the cookie consent script on the "load" event, the script will only execute after everything on the page has fully loaded, which can delay the display and functioning of the cookie consent popup. That delay might not be ideal, especially if user interaction with the consent is required before they can fully interact with the page.

Html相关问答推荐

如何防止下拉菜单内的Bootstrap列表行包装?

单表单和多个提交(具有多个值)

如何在不影响子列表的情况下在HTML(OL Li)上同时加数字和列表?

为什么使用Google字体的SVG徽标内的文本不能在网页上正确呈现?

放大缩小标题在外面的图像

Chrome和Safari浏览器中的CSS3动画不同

附加点的列表样式

文本css后面未显示背景 colored颜色

为什么 Select 元素在带有数据绑定的Blazor上行为怪异?

柔性盒内的物品;t覆盖整个宽度,即使设置为100%

当浏览器宽度减小时字体大小变得太大

如何使Bootstrap 5卡可点击

将图像的高度限制为固定的纵横比,并在超过时使用 object-fit

pandas `to_html()` - 如何只使特定的行有边框

在包含 html 标签的字符串的子字符串中应用不同的样式

如何将 2 种不同的 colored颜色 应用于 css 中的复选框?

涉及短代码时如何在页面上定位元素?

无法使用 HTML 中的 Bootstrap 自上而下居中

表格布局:固定;文本溢出单元格时不起作用

如何将图像高度设置为等于文本高度?