简体   繁体   English

网页优化:缩小后是否应将CSS和JS插入HTML资源?

[英]Webpage optimization: Should CSS and JS be inserted into the HTML resource after minification?

Squishing all the JS and all the CSS into single minified files and linking to them from the HTML page is a reasonable way to go and this appears to be the place that a lot of websites take their optimization to. 将所有JS和所有CSS压缩到单个缩小的文件中,然后从HTML页面链接到它们是一种合理的方法,这似乎是许多网站对其进行优化的地方。

However I seldom see a website which has just one resource load. 但是,我很少看到只有一个资源负载的网站。

My question is about whether having three resources like this is exactly the same as taking the contents of them and putting it into the tags to produce one single resource to load. 我的问题是,拥有三个这样的资源是否获取它们的内容并将其放入标签中以产生一个要加载的资源完全相同 For JS this is straightforward with the <script> tags (take out src and put the minified JS into the tag), and for CSS this is simply replacing <link rel="stylesheet" ... /> with <style type="text/css" /> ...minified CSS... </style> . 对于JS,使用<script>标记很简单(取出src并将缩小的JS放入标记中),对于CSS而言,只需将<link rel="stylesheet" ... />替换为<style type="text/css" /> ...minified CSS... </style>

Is this a common practice to squeeze the page into the smallest package possible (One single TCP transaction, hopefully)? 将页面压缩到最小的包中是否是一种常见的做法(希望有一个TCP事务)?

If nothing else it drastically simplifies the job of the webserver software. 如果没有其他要求,它将大大简化Web服务器软件的工作。

I would keep them separate for these two reasons: 由于以下两个原因,我将它们分开:

  1. the browser can use multiple sockets to download the content 浏览器可以使用多个套接字下载内容

  2. scripts/stylesheets can be cached an reused in other html pages and will therefore only be downloaded once 脚本/样式表可以被缓存,可以在其他HTML页面中重复使用,因此仅下载一次

I'm sure this wouldn't hurt for speed optimisation for devices such as mobiles however on broadband connections it wouldn't make to much difference (obviously speed is a good optimisation and I'm not saying is not) but from what I have read isn't it better to server all content/images from multiple servers where possible as well as code optimisation? 我敢肯定,这不会对移动设备等设备的速度优化造成损害,但是在宽带连接上,它并没有太大的区别(显然,速度是一种很好的优化,我不是说不是),但是根据我的经验读取是否最好将多个服务器中的所有内容/图像同时进行服务器优化以及代码优化? Mumford content will save on total page size. Mumford的内容将节省总页面大小。 But I assumed it didn't matter if the code was on page or through a src file aslong as you don't have multiple CSS pages and multiple js documents and optimise them Ie minify the content and get rid of duplicate tags Ie 但是我认为代码是在页面上还是通过src文件都没有关系,只要您没有多个CSS页面和多个js文档并对其进行优化即可(例如,缩小内容并摆脱重复的标签)

.header{ Color:#000000; .header {颜色:#000000; } .test{ Color:#000000; } .test {颜色:#000000; } Into .header, .test{ Color:#000000; }放入.header,.test {颜色:#000000; } }

And minify the CSS, not sure if this is what you are looking for but I'm sure if you optimise too much you will spend much more time then is worth on the optimisation as the speed you maybe increasing maybe only 0.001 second. 然后缩小CSS,不确定这是否是您要寻找的东西,但是我确定如果您进行过多的优化,您将花费更多的时间进行优化,因为您增加的速度可能仅为0.001秒。

It is the same to have linked resources and to embed them in the html page. 链接资源并将其嵌入html页面是相同的。 However not convenient to use on multiple pages. 但是不方便在多个页面上使用。 Then you have to repeat yourself. 然后,您必须重复自己。

If you put all scripts and css together keep this in mind: 如果将所有脚本和CSS放在一起,请记住以下几点:

In most cases, the bulk of the JavaScript code handles user-initiated events, such as mouse-clicking and dragging. 在大多数情况下,大部分JavaScript代码都处理用户启动的事件,例如鼠标单击和拖动。 All of these user-triggered events occur after the page is loaded and the onload event is triggered. 所有这些用户触发的事件都在页面加载并触发onload事件之后发生。 In order to defer loading of these JavaScripts insert a JavaScript event listener in the head of the containing document that forces the external file to be loaded after the onload event. 为了推迟加载这些JavaScript,请在​​包含文档的头部插入JavaScript事件侦听器,以强制在onload事件之后加载外部文件。 ( Google developers tips ) Google开发人员提示

This is the code-example to make it more clear, the delaying scripts will be loading after the essential page elements have been formed: 这是使更清楚的代码示例,延迟脚本将在基本页面元素形成之后加载:

 // Add a script element as a child of the body
 function downloadJSAtOnload() {
 var element = document.createElement("script");
 element.src = "deferredfunctions.js";
 document.body.appendChild(element);
 }

 // Check for browser support of event handling capability
 if (window.addEventListener)
 window.addEventListener("load", downloadJSAtOnload, false);
 else if (window.attachEvent)
 window.attachEvent("onload", downloadJSAtOnload);
 else window.onload = downloadJSAtOnload;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM