awesome-interview/assets/js/5825b5f0.f3cdcaad.js

1 line
27 KiB
JavaScript

"use strict";(self.webpackChunkjjbook=self.webpackChunkjjbook||[]).push([[5131],{3905:function(e,t,n){n.d(t,{Zo:function(){return c},kt:function(){return k}});var a=n(7294);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function l(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?l(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):l(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function p(e,t){if(null==e)return{};var n,a,r=function(e,t){if(null==e)return{};var n,a,r={},l=Object.keys(e);for(a=0;a<l.length;a++)n=l[a],t.indexOf(n)>=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(a=0;a<l.length;a++)n=l[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var o=a.createContext({}),s=function(e){var t=a.useContext(o),n=t;return e&&(n="function"==typeof e?e(t):i(i({},t),e)),n},c=function(e){var t=s(e.components);return a.createElement(o.Provider,{value:t},e.children)},m={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},u=a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,l=e.originalType,o=e.parentName,c=p(e,["components","mdxType","originalType","parentName"]),u=s(n),k=r,g=u["".concat(o,".").concat(k)]||u[k]||m[k]||l;return n?a.createElement(g,i(i({ref:t},c),{},{components:n})):a.createElement(g,i({ref:t},c))}));function k(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var l=n.length,i=new Array(l);i[0]=u;var p={};for(var o in t)hasOwnProperty.call(t,o)&&(p[o]=t[o]);p.originalType=e,p.mdxType="string"==typeof e?e:r,i[1]=p;for(var s=2;s<l;s++)i[s]=n[s];return a.createElement.apply(null,i)}return a.createElement.apply(null,n)}u.displayName="MDXCreateElement"},6809:function(e,t,n){n.r(t),n.d(t,{frontMatter:function(){return p},contentTitle:function(){return o},metadata:function(){return s},toc:function(){return c},default:function(){return u}});var a=n(7462),r=n(3366),l=(n(7294),n(3905)),i=["components"],p={sidebar_label:"\u7efc\u5408\uff1a\u591a\u56fe\u7ad9\u70b9\u6027\u80fd\u4f18\u5316",sidebar_position:12},o="\u591a\u56fe\u7ad9\u70b9\u6027\u80fd\u4f18\u5316",s={unversionedId:"book2/topic-multi-pics-site-optimize",id:"book2/topic-multi-pics-site-optimize",isDocsHomePage:!1,title:"\u591a\u56fe\u7ad9\u70b9\u6027\u80fd\u4f18\u5316",description:"\u56de\u7b54\u5173\u952e\u70b9",source:"@site/docs/book2/topic-multi-pics-site-optimize.md",sourceDirName:"book2",slug:"/book2/topic-multi-pics-site-optimize",permalink:"/awesome-interview/book2/topic-multi-pics-site-optimize",tags:[],version:"current",sidebarPosition:12,frontMatter:{sidebar_label:"\u7efc\u5408\uff1a\u591a\u56fe\u7ad9\u70b9\u6027\u80fd\u4f18\u5316",sidebar_position:12},sidebar:"tutorialSidebar",previous:{title:"\u7b97\u6cd5\uff1a\u53cd\u8f6c\u94fe\u8868",permalink:"/awesome-interview/book2/algorithm-reverse-linked-list"},next:{title:"\u6d4f\u89c8\u5668\uff1a\u6d4f\u89c8\u5668\u4e8b\u4ef6\u5faa\u73af",permalink:"/awesome-interview/book3/browser-event-loop"}},c=[{value:"\u56de\u7b54\u5173\u952e\u70b9",id:"\u56de\u7b54\u5173\u952e\u70b9",children:[]},{value:"\u77e5\u8bc6\u70b9\u6df1\u5165",id:"\u77e5\u8bc6\u70b9\u6df1\u5165",children:[{value:"1. \u56fe\u7247\u4f18\u5316",id:"1-\u56fe\u7247\u4f18\u5316",children:[]},{value:"2. \u7f51\u7edc\u4f20\u8f93\u4f18\u5316",id:"2-\u7f51\u7edc\u4f20\u8f93\u4f18\u5316",children:[]},{value:"3. \u56fe\u7247\u52a0\u8f7d\u7b56\u7565\u4f18\u5316",id:"3-\u56fe\u7247\u52a0\u8f7d\u7b56\u7565\u4f18\u5316",children:[]}]},{value:"\u53c2\u8003\u8d44\u6599",id:"\u53c2\u8003\u8d44\u6599",children:[]}],m={toc:c};function u(e){var t=e.components,n=(0,r.Z)(e,i);return(0,l.kt)("wrapper",(0,a.Z)({},m,n,{components:t,mdxType:"MDXLayout"}),(0,l.kt)("h1",{id:"\u591a\u56fe\u7ad9\u70b9\u6027\u80fd\u4f18\u5316"},"\u591a\u56fe\u7ad9\u70b9\u6027\u80fd\u4f18\u5316"),(0,l.kt)("h2",{id:"\u56de\u7b54\u5173\u952e\u70b9"},"\u56de\u7b54\u5173\u952e\u70b9"),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"\u56fe\u7247\u4f18\u5316")," ",(0,l.kt)("inlineCode",{parentName:"p"},"\u4f20\u8f93\u4f18\u5316")," ",(0,l.kt)("inlineCode",{parentName:"p"},"\u52a0\u8f7d\u7b56\u7565")),(0,l.kt)("p",null,"\u63d0\u9ad8\u7f51\u7ad9\u6027\u80fd\u7684\u4e00\u9879\u91cd\u8981\u6307\u6807\u662f\u63d0\u9ad8\u8bbf\u95ee\u901f\u5ea6\uff0c\u8fd9\u4e0e\u7528\u6237\u7559\u5b58\u7387\u548c\u8f6c\u6362\u7387\u5448\u6b63\u76f8\u5173\u3002\u6839\u636e ",(0,l.kt)("a",{parentName:"p",href:"https://httparchive.org/reports/page-weight?start=2017_03_01&end=latest&view=list#bytesImg"},"HTTPArchive")," \u7684\u6570\u636e\u53ef\u77e5\uff0c\u56fe\u50cf\u662f\u5927\u591a\u6570\u7f51\u7ad9\u9700\u6c42\u6700\u591a\u7684\u8d44\u6e90\u7c7b\u578b\uff0c\u901a\u5e38\u6bd4\u5176\u4ed6\u8d44\u6e90\u5360\u7528\u66f4\u591a\u5e26\u5bbd\u3002\u5728\u591a\u56fe\u7ad9\u70b9\u4e2d\uff0c\u56fe\u7247\u8d44\u6e90\u5bf9\u4e8e\u9875\u9762\u7684\u52a0\u8f7d\u548c\u6574\u4f53\u7684\u7528\u6237\u4f53\u9a8c\u6709\u66f4\u660e\u663e\u7684\u5f71\u54cd\u3002\u6700\u5e38\u89c1\u7684\u95ee\u9898\u662f\u56fe\u7247\u52a0\u8f7d\u6162\u3002\u5bf9\u5e94\u7684\u4f18\u5316\u7b56\u7565\u5305\u62ec\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"\u56fe\u7247\u4f18\u5316\uff1a\u8fdb\u884c\u56fe\u7247\u538b\u7f29/\u7f29\u653e\u548c\u9009\u62e9\u6b63\u786e\u7684\u56fe\u7247\u683c\u5f0f\u3002"),(0,l.kt)("li",{parentName:"ul"},"\u7f51\u7edc\u4f20\u8f93\u4f18\u5316\uff1a\u4f7f\u7528 HTTP/2 \u548c CDN \u670d\u52a1\u3002"),(0,l.kt)("li",{parentName:"ul"},"\u56fe\u7247\u52a0\u8f7d\u7b56\u7565\u4f18\u5316\uff1a\u6309\u9700\u4f7f\u7528\u61d2\u52a0\u8f7d\u3001\u9884\u52a0\u8f7d\uff0c\u54cd\u5e94\u5f0f\u56fe\u7247\u52a0\u8f7d\u7b49\u7b56\u7565\u3002")),(0,l.kt)("h2",{id:"\u77e5\u8bc6\u70b9\u6df1\u5165"},"\u77e5\u8bc6\u70b9\u6df1\u5165"),(0,l.kt)("h3",{id:"1-\u56fe\u7247\u4f18\u5316"},"1. \u56fe\u7247\u4f18\u5316"),(0,l.kt)("h4",{id:"11-\u9009\u62e9\u5408\u9002\u7684\u56fe\u7247\u683c\u5f0f"},"1.1 \u9009\u62e9\u5408\u9002\u7684\u56fe\u7247\u683c\u5f0f"),(0,l.kt)("p",null,"\u4e3a\u63a7\u5236\u7bc7\u5e45\uff0c\u4ee5\u4e0b\u63d0\u5230\u7684\u56fe\u7247\u683c\u5f0f\uff0c\u4e3a\u622a\u6b62\u81f3 2021 \u5e74 8 \u6708\uff0c\u5e02\u573a\u4efd\u989d\u5927\u4e8e 0.1% \u7684\u683c\u5f0f\u3002"),(0,l.kt)("ol",null,(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("p",{parentName:"li"},"JPEG \u7684\u538b\u7f29\u6548\u7387\u9ad8\uff0c\u662f\u4e00\u79cd\u9ad8\u6548\u4e14\u8f7b\u5de7\u7684\u6709\u635f\u538b\u7f29\u56fe\u7247\u683c\u5f0f\u3002\u4f46\u4e0d\u9002\u5408\u5bf9\u77e2\u91cf\u6216\u5bf9\u6bd4\u5ea6\u5f3a\u7684\u56fe\u50cf\u538b\u7f29\uff0c\u4f1a\u6709\u660e\u663e\u7684\u56fe\u7247\u8d28\u91cf\u4e0b\u964d\u3002\u8d85\u8fc7\u4e00\u5b9a\u7684\u538b\u7f29\u9608\u503c\uff0c\u538b\u7f29\u7684\u56fe\u50cf\u4e5f\u4f1a\u51fa\u73b0\u660e\u663e\u7684\u56fe\u7247\u8d28\u91cf\u4e0b\u964d\u3002")),(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("p",{parentName:"li"},"PNG \u662f\u4e00\u79cd\u65e0\u635f\u538b\u7f29\u7684\u9ad8\u4fdd\u771f\u56fe\u7247\u683c\u5f0f\u3002\u76f8\u6bd4 JPEG \u6709\u66f4\u5f3a\u8272\u5f69\u8868\u73b0\u529b\uff0c\u4e14\u652f\u6301\u900f\u660e\u901a\u9053\u3002")),(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("p",{parentName:"li"},"GIF \u662f\u4e00\u79cd\u6700\u591a\u652f\u6301 256 \u79cd\u989c\u8272\u7684 8 \u4f4d\u65e0\u635f\u56fe\u7247\u683c\u5f0f\uff0c\u652f\u6301\u52a8\u56fe\u3002")),(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("p",{parentName:"li"},"WebP \u662f\u4e00\u79cd\u540c\u65f6\u63d0\u4f9b\u6709\u635f\u538b\u7f29\u4e0e\u65e0\u635f\u538b\u7f29\u7684\u56fe\u7247\u683c\u5f0f\u3002\u4e0d\u4ec5\u652f\u6301\u900f\u660e\u56fe\u7247\uff0c\u6709\u4f18\u79c0\u7684\u8272\u5f69\u8868\u73b0\uff0c\u4e5f\u80fd\u652f\u6301\u52a8\u753b\u3002\u652f\u6301\u65e0\u635f\u538b\u7f29\u4e14\u901a\u5e38\u6bd4 PNG \u683c\u5f0f\u7684\u76f8\u540c\u56fe\u50cf\u5c0f 26%\u3002\u652f\u6301\u6709\u635f\u538b\u7f29\u4e14\u6bd4\u89c6\u89c9\u4e0a\u76f8\u4f3c\u538b\u7f29\u6c34\u5e73\u7684 JPEG \u56fe\u50cf\u5e73\u5747\u5c0f 25-35%\u3002\u4f46\u662f\u6d4f\u89c8\u5668\u517c\u5bb9\u6027\u5dee\u3002")),(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("p",{parentName:"li"},"SVG \u662f\u4e00\u79cd\u57fa\u4e8e XML \u8bed\u6cd5\u7684\u56fe\u50cf\u683c\u5f0f\uff0c\u5168\u79f0\u662f\u53ef\u7f29\u653e\u77e2\u91cf\u56fe\uff08Scalable Vector Graphics\uff09\u3002\u9002\u5408\u975e\u7167\u7247\u7c7b\u578b\u7684\u56fe\u7247\u7684\u7f29\u653e\u6216\u9ad8\u4fdd\u771f\u573a\u666f\u3002"))),(0,l.kt)("p",null,"\u56fe\u4e2d\u6240\u793a\u4e3a 2012 \u5e74 1 \u6708\u81f3 2021 \u5e74 8 \u6708\u7684\u4e3b\u6d41\u56fe\u7247\u683c\u5f0f\u7684\u4f7f\u7528\u8d8b\u52bf\u3002"),(0,l.kt)("p",null,(0,l.kt)("img",{parentName:"p",src:"https://user-images.githubusercontent.com/17002181/128595023-a0ceb0f4-9066-4145-b1e3-345e0c0d2b36.png",alt:"image"})),(0,l.kt)("blockquote",null,(0,l.kt)("p",{parentName:"blockquote"},"\u56fe\u7247\u6765\u6e90 ",(0,l.kt)("a",{parentName:"p",href:"https://w3techs.com/technologies/history_overview/image_format/all/y"},"w3techs.com"))),(0,l.kt)("p",null,"\u6309\u9700\u9009\u62e9\u66f4\u9ad8\u6548\u7684\u56fe\u7247\u683c\u5f0f\uff0c\u4e0d\u4ec5\u80fd\u63d0\u5347\u7528\u6237\u89c6\u89c9\u4f53\u9a8c\uff0c\u4e5f\u53ef\u4ee5\u63d0\u5347\u7f51\u7ad9\u52a0\u8f7d\u6548\u7387\u3002\u5728\u9009\u7528\u56fe\u7247\u683c\u5f0f\u65f6\uff0c\u4e00\u822c\u53ef\u4ee5\u57fa\u4e8e\u4e00\u4e9b\u7b80\u5355\u89c4\u5219\u6765\u7b5b\u9009\uff1a\u5728\u517c\u5bb9\u6027\u652f\u6301\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u9009\u7528 WebP\uff0c\u5426\u5219\u53ef\u4ee5\u901a\u8fc7\u52a8\u56fe\u548c\u900f\u660e\u5ea6\u4e24\u4e2a\u9700\u6c42\u70b9\u6765\u8fdb\u884c\u7b5b\u9009\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("p",{parentName:"li"},(0,l.kt)("strong",{parentName:"p"},"\u52a8\u56fe")),(0,l.kt)("p",{parentName:"li"},"\u53ef\u4ee5\u4f7f\u7528 GIF \u6216\u8005\u662f\u89c6\u9891\u683c\u5f0f\u3002\u524d\u8005\u7684\u95ee\u9898\u5728\u4e8e\u652f\u6301\u7684\u8272\u5f69\u5c11\uff0c\u4f4e\u5e27\u7387\u4f4e\u5206\u8fa8\u7387\uff0c\u6587\u4ef6\u4f53\u79ef\u5229\u7528\u7387\u4f4e\uff0c\u800c\u89c6\u9891\u65b9\u6848\u5219\u53ef\u4ee5\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528 APNG\uff0c\u652f\u6301\u66f4\u591a\u8272\u5f69\u7684\u524d\u63d0\u4e0b\uff0c\u548c\u5bf9\u5e94 GIF \u683c\u5f0f\u7684\u6587\u4ef6\u4f53\u79ef\u76f8\u8fd1\u3002")),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("p",{parentName:"li"},(0,l.kt)("strong",{parentName:"p"},"\u900f\u660e\u5ea6")),(0,l.kt)("p",{parentName:"li"},"PNG \u548c GIF \u90fd\u652f\u6301\u900f\u660e\u56fe\u7247\uff0c\u53ef\u4ee5\u6309\u9700\u4f7f\u7528\u3002"))),(0,l.kt)("p",null,"\u5728\u6ca1\u6709\u900f\u660e\u548c\u52a8\u753b\u9700\u6c42\u7684\u60c5\u51b5\u4e0b\uff0cJPEG \u683c\u5f0f\u56fe\u7247\u80dc\u4efb\u5927\u90e8\u5206\u573a\u666f\uff0c\u5982\u679c\u5bf9\u56fe\u7247\u7684\u5c55\u793a\u8d28\u91cf\u6709\u8f83\u9ad8\u8981\u6c42\u65f6\uff0c\u53ef\u4f7f\u7528 PNG \u683c\u5f0f\u56fe\u7247\u3002"),(0,l.kt)("p",null,"\u7ed8\u5236 LOGO\u3001ICON \u7b49\u975e\u7167\u7247\u7684\u56fe\u7247\u5185\u5bb9\u65f6\uff0c\u4e00\u822c\u4f7f\u7528 SVG \u683c\u5f0f\u3002\u6bd4\u5982 iconfont \u7b49\u77e2\u91cf\u56fe\u6807\u7ba1\u7406\u5e73\u53f0\u4e2d\u5927\u91cf\u4f7f\u7528 SVG \u683c\u5f0f\u3002"),(0,l.kt)("h4",{id:"12-\u56fe\u7247\u538b\u7f29\u548c\u7f29\u653e\u5904\u7406"},"1.2 \u56fe\u7247\u538b\u7f29\u548c\u7f29\u653e\u5904\u7406"),(0,l.kt)("p",null,"\u7531\u4e8e\u5b9e\u9645\u5e94\u7528\u573a\u666f\u7684\u5dee\u5f02\uff0c\u5bf9\u5e94\u56fe\u7247\u7684\u5e03\u5c40\u5927\u5c0f\u4ee5\u53ca\u56fe\u7247\u7ec6\u8282\u8981\u6c42\u5404\u6709\u4e0d\u540c\uff0c\u5927\u91cf\u672a\u7ecf\u538b\u7f29\u6216\u7f29\u653e\u8c03\u6574\u7684\u56fe\u7247\u4f1a\u4f7f\u7f51\u9875\u52a0\u8f7d\u8bb8\u591a\u4e0d\u5fc5\u8981\u7684\u5b57\u8282\uff0c\u4e14\u5bf9\u7528\u6237\u7684\u89c6\u89c9\u6548\u679c\u6ca1\u6709\u592a\u5927\u7684\u63d0\u5347\u3002\u524d\u7aef\u5e38\u89c1\u7684\u538b\u7f29\u548c\u7f29\u653e\u7684\u5904\u7406\u65b9\u6848\u5305\u62ec\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"\u9759\u6001\u8d44\u6e90\u901a\u8fc7\u5de5\u5177\uff08\u6bd4\u5982 imagemin\uff09\u6309\u9700\u8fdb\u884c\u6709\u635f\u6216\u65e0\u635f\u538b\u7f29\u3002"),(0,l.kt)("li",{parentName:"ul"},"\u5c06\u7528\u6237\u4e0a\u4f20\u7684\u56fe\u7247\u7ed8\u5236\u5230 Canvas \u753b\u5e03\u4e0a\uff0c\u5229\u7528",(0,l.kt)("inlineCode",{parentName:"li"},"CanvasRenderingContext2D.drawImage(image, dx, dy, dWidth, dHeight)")," API \u8fdb\u884c\u56fe\u7247\u7f29\u653e\uff1b\u5229\u7528 ",(0,l.kt)("inlineCode",{parentName:"li"},"HTMLCanvasElement.toDataURL(type, encoderOptions)")," API \u8fdb\u884c\u6709\u635f\u538b\u7f29\u3002"),(0,l.kt)("li",{parentName:"ul"},"\u6839\u636e\u7528\u6237\u4fa7\u7684\u663e\u793a\u9700\u6c42\uff08\u5982\u5934\u50cf\u3001\u7f29\u7565\u56fe\u3001\u5546\u54c1\u56fe\u7b49\uff09\uff0c\u901a\u8fc7\u5bf9\u8c61\u5b58\u50a8\u670d\u52a1\uff08\u5982\u4e03\u725b\u3001\u963f\u91cc\u4e91 OSS\uff09\u6240\u63d0\u4f9b\u7684\u538b\u7f29\u6216\u7f29\u653e\u7b49\u529f\u80fd\u5904\u7406\u540e\u8fd4\u56de\u4f7f\u7528\u3002")),(0,l.kt)("h3",{id:"2-\u7f51\u7edc\u4f20\u8f93\u4f18\u5316"},"2. \u7f51\u7edc\u4f20\u8f93\u4f18\u5316"),(0,l.kt)("h4",{id:"21-\u4f7f\u7528-http2-\u534f\u8bae"},"2.1 \u4f7f\u7528 HTTP/2 \u534f\u8bae"),(0,l.kt)("p",null,"\u4f7f\u7528 HTTP/1.X \u534f\u8bae\u65f6\uff0c\u6d4f\u89c8\u5668\u6709\u540c\u6e90\u6700\u5927\u5e76\u53d1\u8fde\u63a5\u6570\u7684\u9650\u5236\uff0c\u4e14 HTTP/1.X \u4e0d\u652f\u6301\u591a\u8def\u590d\u7528\uff0c\u56e0\u6b64\u4e00\u4e2a\u591a\u56fe\u7ad9\u70b9\u60f3\u8981\u83b7\u5f97\u8f83\u5b8c\u6574\u7684\u89c6\u89c9\u5448\u73b0\uff0c\u4f1a\u6709\u4e00\u5b9a\u7a0b\u5ea6\u7684\u5ef6\u8fdf\uff1a\u6240\u6709\u7684\u8d44\u6e90\u8bf7\u6c42\uff08\u5305\u62ec\u56fe\u7247\u8d44\u6e90\uff09\u4f1a\u8fdb\u5165\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u961f\u5217\u7b49\u5f85\u88ab\u4e0b\u8f7d\u3002"),(0,l.kt)("p",null,(0,l.kt)("img",{parentName:"p",src:"https://user-images.githubusercontent.com/17002181/128637043-8810bdb4-f190-4196-abc2-37d78896b71b.png",alt:"image"})),(0,l.kt)("p",null,"\u4f7f\u7528 HTTP/2 \u524d\u7684\u5e38\u89c1\u4f18\u5316\u65b9\u6848\u5305\u62ec\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"\u4f7f\u7528\u7cbe\u7075\u56fe / \u96ea\u78a7\u56fe\uff0c\u51cf\u5c11 HTTP \u8bf7\u6c42\u6570\u3002"),(0,l.kt)("li",{parentName:"ul"},"10kb \u5927\u5c0f\u4ee5\u5185\u7684\u56fe\u7247\u8d44\u6e90\u4f7f\u7528 base64 \u7f16\u7801\uff0c\u51cf\u5c11 HTTP \u8bf7\u6c42\u6570\u3002"),(0,l.kt)("li",{parentName:"ul"},"\u901a\u8fc7\u4f7f\u7528\u591a\u4e2a\u57df\u540d\uff0c\u5f00\u542f\u591a\u4e2a TCP \u8fde\u63a5\uff0c\u7a81\u7834\u6d4f\u89c8\u5668\u540c\u6e90\u6700\u5927\u5e76\u53d1\u8fde\u63a5\u6570\u7684\u9650\u5236\u3002")),(0,l.kt)("p",null,"\u7531\u4e8e HTTP/2 \u652f\u6301\u591a\u8def\u590d\u7528\uff0c\u56e0\u6b64\u4f7f\u7528 HTTP/2 \u53ef\u4ee5\u8fdb\u4e00\u6b65\u51cf\u5c11\u7f51\u7edc\u5ef6\u8fdf\uff0c\u66f4\u52a0\u5feb\u901f\u7684\u52a0\u8f7d\u56fe\u7247\u8d44\u6e90\u3002"),(0,l.kt)("p",null,"\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u89c2\u5bdf Connection ID \u4e00\u5217\u53ef\u77e5\uff0c\u4f7f\u7528 HTTP/2 \u7684\u60c5\u51b5\u4e0b\uff0c\u8d44\u6e90\u91cd\u7528\u540c\u4e00\u6761 TCP \u8fde\u63a5\uff0c\u5e76\u53d1\u8bf7\u6c42\u5927\u91cf\u56fe\u7247\u8d44\u6e90\u3002"),(0,l.kt)("p",null,(0,l.kt)("img",{parentName:"p",src:"https://user-images.githubusercontent.com/17002181/128636947-43337c16-3517-41c0-aea9-06c5ff3741a2.png",alt:"image"})),(0,l.kt)("h4",{id:"22-\u4f7f\u7528-cdn"},"2.2 \u4f7f\u7528 CDN"),(0,l.kt)("p",null,"CDN \u5c06\u6e90\u7ad9\u8d44\u6e90\u7f13\u5b58\u5230\u5404\u52a0\u901f\u8282\u70b9\u540e\uff0c\u7528\u6237\u8bf7\u6c42\u6e90\u7ad9\u8d44\u6e90\u65f6\u65e0\u9700\u56de\u6e90\uff0c\u53ef\u5c31\u8fd1\u83b7\u53d6 CDN \u8282\u70b9\u4e0a\u5df2\u7f13\u5b58\u7684\u8d44\u6e90\uff0c\u4ece\u800c\u63d0\u9ad8\u8d44\u6e90\u8bbf\u95ee\u901f\u5ea6\uff0c\u5206\u62c5\u6e90\u7ad9\u538b\u529b\u3002\u5e38\u89c1\u7684 CDN \u670d\u52a1\u8fd8\u652f\u6301\u5bf9\u56fe\u7247\u8fdb\u884c\u538b\u7f29\u3001\u7f29\u653e\u3001\u88c1\u526a\u7b49\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002"),(0,l.kt)("h3",{id:"3-\u56fe\u7247\u52a0\u8f7d\u7b56\u7565\u4f18\u5316"},"3. \u56fe\u7247\u52a0\u8f7d\u7b56\u7565\u4f18\u5316"),(0,l.kt)("h4",{id:"31-\u56fe\u7247\u61d2\u52a0\u8f7d"},"3.1 \u56fe\u7247\u61d2\u52a0\u8f7d"),(0,l.kt)("p",null,"\u61d2\u52a0\u8f7d\u7684\u7b56\u7565\u662f\u63a8\u8fdf\u52a0\u8f7d\u79bb\u5c4f\u56fe\u7247\u8d44\u6e90\uff0c\u4ece\u800c\u51cf\u5c11\u8d44\u6e90\u8bf7\u6c42\u6570\u3002\u5b9e\u73b0\u61d2\u52a0\u8f7d\u7684\u4e3b\u6d41\u65b9\u6848\u6709\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"\u4f7f\u7528 img \u6807\u7b7e\u7684 loading \u5c5e\u6027\u3002"),(0,l.kt)("li",{parentName:"ul"},"\u4f7f\u7528 Intersection Observer API\u3002"),(0,l.kt)("li",{parentName:"ul"},"\u4f7f\u7528 scroll\u3001resize \u548c orientationchange \u4e8b\u4ef6\u3002")),(0,l.kt)("p",null,"\u540e\u4e24\u79cd\u65b9\u6848\u7684\u5b9e\u73b0\u539f\u7406\u662f\u901a\u8fc7\u5728 img \u6807\u7b7e\u4e0a\u6dfb\u52a0 data-src \u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u5c5e\u6027\u5b58\u653e\u56fe\u7247\u94fe\u63a5\uff0c\u800c src \u5c5e\u6027\u4e0d\u88ab\u8bbe\u7f6e\u6216\u8bbe\u7f6e\u4e3a\u5360\u4f4d\u56fe\u94fe\u63a5\u3002\u901a\u8fc7 Intersection Observer \u6216 scroll \u7b49 API \u68c0\u6d4b\u79bb\u5c4f\u56fe\u7247\u662f\u5426\u6eda\u52a8\u5230\u9884\u671f\u4f4d\u7f6e\uff0c\u5982\u679c\u662f\u5219\u5c06 data-src \u7684\u503c\u8d4b\u7ed9 src \u5c5e\u6027\uff0c\u4ece\u800c\u8fbe\u5230\u61d2\u52a0\u8f7d\u7684\u76ee\u7684\u3002"),(0,l.kt)("p",null,"\u4e00\u822c\u4f7f\u7528\u56fe\u7247\u61d2\u52a0\u8f7d\u65f6\uff0c\u56fe\u7247\u7684\u5360\u4f4d\u5904\u4f1a\u4f7f\u7528\u5404\u79cd\u65b9\u5f0f\u6765\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"\u8272\u5757 / \u9aa8\u67b6\u5c4f\u5360\u4f4d\u3002"),(0,l.kt)("li",{parentName:"ul"},"LOGO \u7b49\u54c1\u724c\u5143\u7d20\u505a\u9ed8\u8ba4\u56fe\u7247\u3002"),(0,l.kt)("li",{parentName:"ul"},"\u4f7f\u7528\u56fe\u7247\u7f29\u7565\u56fe\u505a\u6a21\u7cca\u6548\u679c\u5360\u4f4d\u3002")),(0,l.kt)("p",null,(0,l.kt)("strong",{parentName:"p"},"img loading")),(0,l.kt)("p",null,"\u4ece Chrome 76+ \u7248\u672c\u8d77\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u4f7f\u7528 loading \u5c5e\u6027\u6765\u63a8\u8fdf\u52a0\u8f7d\u53ef\u901a\u8fc7\u6eda\u52a8\u8fdb\u5165\u89c6\u53e3\u5185\u7684\u79bb\u5c4f\u56fe\u50cf\u3002\u901a\u8fc7\u7ed9 loading \u5c5e\u6027\u8bbe\u7f6e lazy \u503c\uff0c\u53ef\u4ee5\u63a8\u8fdf\u52a0\u8f7d\u8d44\u6e90\uff0c\u76f4\u5230\u5b83\u4e0e\u89c6\u53e3\u8fbe\u5230\u4e00\u5b9a\u8ddd\u79bb\u3002",(0,l.kt)("a",{parentName:"p",href:"https://caniuse.com/loading-lazy-attr"},"caniuse.com")," \u53ef\u67e5\u9605\u8de8\u6d4f\u89c8\u5668\u517c\u5bb9\u6027\u652f\u6301\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u4e0d\u652f\u6301 loading \u5c5e\u6027\u7684\u6d4f\u89c8\u5668\u4f1a\u5ffd\u7565\u8be5\u5c5e\u6027\uff0c\u4e0d\u4f1a\u4ea7\u751f\u526f\u4f5c\u7528\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-html"},'<img src="image.png" loading="lazy" alt="\u2026" width="200" height="200" />\n')),(0,l.kt)("p",null,(0,l.kt)("strong",{parentName:"p"},"Intersection Observer")),(0,l.kt)("p",null,"Intersection Observer API \u53ef\u7528\u4e8e\u5f02\u6b65\u89c2\u5bdf\u76ee\u6807\u5143\u7d20\u4e0e\u7956\u5148\u5143\u7d20\u6216\u4e0e\u9876\u7ea7\u6587\u6863\u89c6\u53e3\u7684\u4ea4\u53c9\u70b9\u53d8\u5316\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-html"},'<img data-src="https://hzfe-blah.com/anyphoto1.jpg" />\n<img data-src="https://hzfe-blah.com/anyphoto2.jpg" />\n')),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},'const config = {\n /** any option */\n};\n\nconst observer = new IntersectionObserver(function (entries, self) {\n entries.forEach(({ isIntersecting, target }) => {\n if (isIntersecting) {\n if (target.dataset.src) {\n target.src = target.dataset.src;\n target.removeAttribute("data-src");\n }\n\n self.unobserve(target);\n }\n });\n}, config);\n\nconst images = document.querySelectorAll("[data-src]");\nimages.forEach((image) => {\n observer.observe(image);\n});\n')),(0,l.kt)("p",null,(0,l.kt)("strong",{parentName:"p"},"scroll")),(0,l.kt)("p",null,"\u5982\u679c Intersection Observer \u5b58\u5728\u517c\u5bb9\u6027\u95ee\u9898\uff0c\u9664\u4e86\u53ef\u4ee5\u6dfb\u52a0\u5bf9\u5e94 polyfill \u4e4b\u5916\uff0c\u4e5f\u53ef\u4ee5\u8003\u8651\u964d\u7ea7\u4e3a\u76d1\u542c scroll\u3001resize\u3001orientationchange \u4e8b\u4ef6\u7684\u65b9\u6848\u3002\u5b9e\u73b0\u601d\u8def\u548c Intersection Observer \u4e00\u81f4\u3002\u5177\u4f53\u7ec6\u8282\u4e0a\uff0c\u9700\u8981\u81ea\u884c\u8ba1\u7b97\u56fe\u7247\u8282\u70b9\u4e0e\u76ee\u6807\u89c6\u53e3\u7684\u7eb5\u5411\u6216\u6a2a\u5411\u8ddd\u79bb\uff0c\u4e14\u9700\u4f7f\u7528\u8282\u6d41\u51fd\u6570\u6765\u907f\u514d\u6027\u80fd\u95ee\u9898\u3002"),(0,l.kt)("h4",{id:"32-\u56fe\u7247\u9884\u52a0\u8f7d"},"3.2 \u56fe\u7247\u9884\u52a0\u8f7d"),(0,l.kt)("p",null,"\u56fe\u7247\u9884\u52a0\u8f7d\u673a\u5236\u662f\u4e3a\u4e86\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\uff0c\u5c3d\u5feb\u5730\u52a0\u8f7d\u51fa\u56fe\u7247\uff0c\u4f7f\u5f97\u7528\u6237\u4f53\u9a8c\u66f4\u4e3a\u6d41\u7545\u3002"),(0,l.kt)("p",null,"\u5982\u679c\u9884\u52a0\u8f7d\u7684\u56fe\u7247\u662f\u786e\u5207\u4e14\u6709\u9650\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u786c\u7f16\u7801 link \u6807\u7b7e\u6765\u5b9e\u73b0\u9884\u52a0\u8f7d\u3002\u4f46\u662f\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9884\u52a0\u8f7d\u7684\u4f7f\u7528\u573a\u666f\u662f\u52a8\u6001\u7684\u3002"),(0,l.kt)("p",null,(0,l.kt)("strong",{parentName:"p"},"link")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},'<link rel="preload">')," \u5141\u8bb8\u5f00\u53d1\u8005\u5728 HTML \u7684 head \u6807\u7b7e\u4e2d\u58f0\u660e\u8d44\u6e90\u8bf7\u6c42\uff0c\u6307\u5b9a\u9875\u9762\u9700\u8981\u9884\u52a0\u8f7d\u7684\u8d44\u6e90\uff0c\u5e76\u4e14\u5728\u6d4f\u89c8\u5668\u7684\u4e3b\u8981\u6e32\u67d3\u673a\u5236\u542f\u52a8\u4e4b\u524d\u52a0\u8f7d\uff0c\u907f\u514d\u963b\u585e\u9875\u9762\u6e32\u67d3\u4e14\u4fdd\u8bc1\u8d44\u6e90\u5c3d\u65e9\u53ef\u7528\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-html"},'<link rel="preload" as="image" href="important.png" />\n')),(0,l.kt)("p",null,(0,l.kt)("strong",{parentName:"p"},"\u52a8\u6001\u573a\u666f")),(0,l.kt)("p",null,"\u4e00\u822c\u5e38\u89c1\u65b9\u6848\u662f\u52a8\u6001\u521b\u5efa Image \u6807\u7b7e\u6216\u8005\u662f Ajax \u8bf7\u6c42\u3002\u4f7f\u7528 Ajax \u65f6\u9700\u8981\u6ce8\u610f\u53ef\u80fd\u5b58\u5728\u8de8\u57df\u95ee\u9898\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-js"},"// \u52a8\u6001\u521b\u5efa Image\nfunction preloadImage(url) {\n var img = new Image();\n img.src = url;\n}\n")),(0,l.kt)("h4",{id:"33-\u54cd\u5e94\u5f0f\u56fe\u7247\u52a0\u8f7d"},"3.3 \u54cd\u5e94\u5f0f\u56fe\u7247\u52a0\u8f7d"),(0,l.kt)("p",null,"\u7531\u4e8e\u7528\u6237\u7ec8\u7aef\u8bbe\u5907\u4e0d\u540c\uff0c\u5982\u679c\u56fe\u7247\u8d44\u6e90\u65e0\u5dee\u522b\u4f7f\u7528\u76f8\u540c\u56fe\u7247\uff0c\u53ef\u80fd\u9020\u6210\u5e26\u5bbd\u6d6a\u8d39\u6216\u8005\u662f\u56fe\u7247\u4e0d\u6e05\u6670\u4ee5\u53ca\u89c6\u89c9\u4f53\u9a8c\u5dee\u7684\u95ee\u9898\u3002"),(0,l.kt)("p",null,"\u4e00\u822c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 picture \u6807\u7b7e\u6765\u5b9a\u4e49\u96f6\u6216\u591a\u4e2a source \u8282\u70b9\u548c\u4e00\u4e2a img \u8282\u70b9\uff0c\u7528\u4e8e\u63d0\u4f9b\u56fe\u7247\u5728\u4e0d\u540c\u8bbe\u5907/\u663e\u793a\u573a\u666f\u4e0b\u5bf9\u5e94\u7684\u5185\u5bb9\u5c55\u793a\u3002picture \u7684\u5e38\u89c1\u4f5c\u7528\u5305\u62ec\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("p",{parentName:"li"},(0,l.kt)("strong",{parentName:"p"},"\u827a\u672f\u6307\u5bfc\uff08Art direction\uff09")),(0,l.kt)("p",{parentName:"li"},"\u4e3a\u4e0d\u540c\u7684\u5a92\u4f53\u6761\u4ef6\u88c1\u526a\u6216\u4fee\u6539\u56fe\u50cf\u3002\u6bd4\u5982\u5728\u8f83\u5c0f\u7684\u663e\u793a\u5668\u4e0a\uff0c\u52a0\u8f7d\u4e00\u4e2a\u66f4\u7a81\u51fa\u91cd\u70b9\u7684\u56fe\u50cf\u3002"),(0,l.kt)("p",{parentName:"li"},(0,l.kt)("img",{parentName:"p",src:"https://user-images.githubusercontent.com/17002181/128734922-3d28cc38-b3c1-4a6e-afd9-5a8c0f5113b1.jpg",alt:"38666B5C621028DD6F3050D95EF1889E"})),(0,l.kt)("pre",{parentName:"li"},(0,l.kt)("code",{parentName:"pre",className:"language-html"},'<picture>\n <source srcset="hzfe-avatar-desktop.png" media="(min-width: 990px)" />\n <source srcset="hzfe-avatar-tablet.png" media="(min-width: 750px)" />\n <img src="hzfe-avatar.png" alt="hzfe-default-avatar" />\n</picture>\n'))),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("p",{parentName:"li"},(0,l.kt)("strong",{parentName:"p"},"\u63d0\u4f9b\u56fe\u7247\u683c\u5f0f\u56de\u9000\u65b9\u6848")),(0,l.kt)("p",{parentName:"li"},"\u5728\u652f\u6301\u7684\u6d4f\u89c8\u5668\u4e2d\u4f18\u5148\u4f7f\u7528\u66f4\u9002\u5408\u7684\u56fe\u7247\u683c\u5f0f\uff0c\u6bd4\u5982 WebP \u7b49\u3002\u540c\u65f6\u652f\u6301\u5728\u6709\u517c\u5bb9\u6027\u95ee\u9898\u7684\u6d4f\u89c8\u5668\u4e2d\u56de\u9000\u52a0\u8f7d\u5176\u4ed6\u683c\u5f0f\u7684\u56fe\u7247\u3002"),(0,l.kt)("pre",{parentName:"li"},(0,l.kt)("code",{parentName:"pre",className:"language-html"},'<picture>\n <source srcset="hzfe-avatar.webp" type="image/webp" />\n <source srcset="hzfe-avatar.jpg" type="image/jpeg" />\n <img src="hzfe-avatar.jpg" alt="hzfe-default-avatar" />\n</picture>\n'))),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("p",{parentName:"li"},(0,l.kt)("strong",{parentName:"p"},"\u8282\u7701\u5e26\u5bbd\u5e76\u63d0\u5347\u9875\u9762\u52a0\u8f7d\u901f\u5ea6")),(0,l.kt)("p",{parentName:"li"},"\u901a\u8fc7\u6309\u9700\u52a0\u8f7d\u5e76\u663e\u793a\u6700\u9002\u5408\u7528\u6237\u8bbe\u5907\u7684\u56fe\u50cf\uff0c\u4ece\u800c\u8282\u7701\u5e26\u5bbd\u548c\u52a0\u5feb\u9875\u9762\u52a0\u8f7d\u65f6\u95f4\u3002"),(0,l.kt)("pre",{parentName:"li"},(0,l.kt)("code",{parentName:"pre",className:"language-html"},'<picture>\n <source\n srcset="hzfe-avatar-desktop.png, hzfe-avatar-desktop-2x.png 2x"\n media="(min-width: 990px)"\n />\n <source\n srcset="hzfe-avatar-tablet.png, hzfe-avatar-tablet-2x.png 2x"\n media="(min-width: 750px)"\n />\n <img\n srcset="hzfe-avatar.png, hzfe-avatar-2x.png 2x"\n src="hzfe-avatar.png"\n alt="hzfe-default-avatar"\n />\n</picture>\n')))),(0,l.kt)("h2",{id:"\u53c2\u8003\u8d44\u6599"},"\u53c2\u8003\u8d44\u6599"),(0,l.kt)("ol",null,(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("a",{parentName:"li",href:"https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types"},"image types")),(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("a",{parentName:"li",href:"https://web.dev/fast"},"Fast load times")),(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("a",{parentName:"li",href:"https://w3techs.com/technologies/details/im-webp"},"Usage statistics of WebP for websites")),(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("a",{parentName:"li",href:"https://web.dev/browser-level-image-lazy-loading/"},"Browser-level image lazy-loading for the web"))))}u.isMDXComponent=!0}}]);