gpt4 book ai didi

javascript - Form in view 破坏 CSS/JS 性能优化

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:10:00 25 4
gpt4 key购买 nike

我正在为 Asp.Net Core MVC 应用程序中脚本和 CSS 的性能优化而苦苦挣扎。

我使用带有 LoadCSS 的 Bootstrap 来预加载它。 FontsAwesome CSS 和 JavaScript 在内容之后加载。

我的 _Layout 页面看起来像这样。

<head>

<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1">

<script>
LoadCSS script here...
</script>

<script async defer src="https://use.fontawesome.com/releases/v5.2.0/js/all.js" integrity="sha384-4oV5EgaV02iISL2ban6c/RmotsABqE4yZxZLcYMAdG7FAPsyHYAPpywE9PJo+Khy" crossorigin="anonymous"></script>

<link rel="preload" as="style" type="text/css" onload="this.onload=null;this.rel='stylesheet'" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">

<noscript>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
</noscript>


<style>
inline critical path CSS here...
</style>

</head>
<body>

<div class="container-fluid body-content">
@RenderBody()
@RenderSection("scripts", false)
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script async src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
</body>

这很好用,只要页面不包含输入表单,我就会获得良好的性能分数(在 Google PageSpped 上为 94)。

对于具有输入表单的页面,性能优化停止工作,Google PageSpeed 提示渲染阻塞 jquery.js 和 bootstrap CSS。出于某种原因,Bootstrap 似乎加载了两次!?以下是 PageSpeed 对带有表单的页面的说明,其他页面没有此问题。

Remove render-blocking JavaScript:

https://code.jquery.com/jquery-3.3.1.slim.min.js

Optimize CSS Delivery of the following:

https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css

我不太明白这里发生了什么。不知何故,表单的存在似乎迫使 Bootstrap 提前加载并预加载,而 LoadCSS 不“工作”。

表单确实使用了客户端数据验证,这可能是罪魁祸首。

最佳答案

我设法解决了问题。我在许多网站上读到,为了获得最佳性能,JavaScript 应该放在关闭 body 标记之前,但这个建议似乎有些过时了。将脚本放在头部并使用 defer 实际上更有意义。 Defer 会在不阻塞渲染的情况下并行加载它们,并在它们之后按顺序执行,这是完美的。

CSS 问题是由于内联关键 CSS 中缺少一些元素,即表单控制。这导致整个 CSS 被加载,出于某种原因两次。我用了penthouse为每个页面生成关键 CSS,然后合并它们。

改进的布局页面:

<head>

<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1">

<script>
LoadCSS script here...
</script>

<script async defer src="https://use.fontawesome.com/releases/v5.2.0/js/all.js" integrity="sha384-4oV5EgaV02iISL2ban6c/RmotsABqE4yZxZLcYMAdG7FAPsyHYAPpywE9PJo+Khy" crossorigin="anonymous"></script>

<link rel="preload" as="style" type="text/css" onload="this.onload=null;this.rel='stylesheet'" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">

<noscript>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
</noscript>


<style>
inline critical path CSS here...
</style>

<script defer src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script defer src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

</head>

<body>

<div class="container-fluid body-content">
@RenderBody()
@RenderSection("scripts", false)
</div>

</body>

关于javascript - Form in view 破坏 CSS/JS 性能优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51569626/

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