Home >Web Front-end >HTML Tutorial >Web front-end optimization methods
At present, the Internet BS model applications are becoming more and more widespread, including the development of website systems, OA platforms, etc. So how to do it well, do it well, and bring a good experience to users? Let me share with you my experience in Web front-end optimization. These experiences are more practical technologies in work practice
1. Minimize the number of HTTP requests - a trade-off must be made
Merge images (such as css sprites, built-in image usage data) , Merging CSS and JS is very important, but the size of the merged file must be considered.
2. Specify Expires or Cache-Control for the file header to make the content cacheable.
Distinguish between static content and dynamic content to avoid unnecessary HTTP requests in future page visits.
3. Avoid empty src and href
Pay attention to tags with these two attributes such as link, script, img, iframe, etc.;
4. Use gzip to compress content
Gzip compresses all possible file types to reduce files Volume
5. Put CSS at the top
to achieve orderly loading of the page. This is more important for pages with more content and users with slow network speeds. At the same time, the HTML specification clearly states that the style sheet should be placed Contained within the area of the page;
6. Put JS at the bottom
The HTTP/1.1 specification recommends that the browser should download no more than two contents in parallel for each host name. The problem is that the script prevents the parallel downloading of the page. Even if the host names are different
7. Avoid using CSS expressions
When the page is displayed and zoomed, scrolled, or even moved the mouse, we should pay attention to the calculation frequency of CSS expressions. Consider one-time expressions or use event handlers instead of CSS expressions.
8. Put CSS and JS into external files
We need to weigh the compromise between the reduction in HTTP requests brought by the built-in code and the benefits of caching by using external files.
9. Streamline CSS and JS
The purpose is to reduce the size of downloaded files. You can consider compression tools JSMin and YUI Compressor.
10. Eliminate duplicate JS and CSS
Repeated calls to the script will not only add additional HTTP requests, but also waste time in multiple operations. In IE and Firefox, regardless of whether the script is cacheable or not, they all have the problem of re-evaluating JavaScript.
11. Make AJAX cacheable
Utilize timestamps to achieve a more sophisticated response cache and update the server data synchronously.
12. Flush the output buffer as early as possible
Especially for css, parallel download of js files is more meaningful
13. Use GET to complete AJAX requests
When using XMLHttpRequest, the POST method in the browser is a "two-step" "Go" process: first send the file header, and then send the data. It makes more sense when using GET to get data when the url is less than 2K.
14. Lazy loading
After confirming that the page is running normally, load the script to implement things such as drag-and-drop and animation, or hide part of the content and fold the content.
15. Preloading
Pay attention to unconditional loading, conditional loading and expected loading.
16. Minimize the number of iframes
Consider that even if the content is empty, loading will take time, which will prevent the page from loading and has no semantic meaning. Note that iframe has an overhead that is 1-2 orders of magnitude higher than other DOM elements. It will In the typical way of blocking the onload event, the main page stylesheet in IE and Firefox will block its download.
17. Avoid 404
HTTP request time consumption is very large. Some sites change the 404 error response page to "Are you looking for *". Although this improves the user experience, it also wastes server resources (such as database, etc.).
18. Reduce the size of cookies
Remove unnecessary cookies, make the cookie size as small as possible to reduce the impact on user response, and set a reasonable expiration time. Expiration time earlier and not clearing cookies too early will improve user response time.
19. Optimize images
Try converting GIF format to PNG format to see if it saves space. Run pngcrush (or other PNG optimization tools) on all PNG images
20. Don’t scale images in HTML – a trade-off
Don’t use images that are larger than necessary to set the height and width in HTML.