wsmwzms123 / blog Goto Github PK
View Code? Open in Web Editor NEWHomerWong.blog
License: MIT License
HomerWong.blog
License: MIT License
最近在某些大厂官网上看网页结构的时候,发现了一个很有意思的地方,那就是dns-prefech。
虽然是老生常谈,但是对于某些用得频率不是很高的技术细节,我的印象往往都是模模糊糊的,为了加深巩固相关印象,特意在此著一篇小文。
首先放上一篇看过的写的还不错的技术文章,我的观点大都提炼于此
最简单的使用方法:
<link rel="dns-prefetch" href="[//host_name_to_prefetch.com](http://the_worlds_best_vendor.com/)">
DNS prefetching is an attempt to resolve domain names before a user tries to follow a link. This is done using the computer's normal DNS resolution mechanism; no connection to Google is used. Once a domain name has been resolved, if the user does navigate to that domain, there will be no effective delay due to DNS resolution time. The most obvious example where DNS prefetching can help is when a user is looking at a page with many links to various domains, such as a search results page. When we encounter hyperlinks in pages, we extract the domain name from each one and resolving each domain to an IP address. All this work is done in parallel with the user's reading of the page, using minimal CPU and network resources. When a user clicks on any of these pre-resolved names, they will on average save about 200 milliseconds in their navigation (assuming the user hadn't already visited the domain recently). More importantly than the average savings, users won't tend to experience the "worst case" delays for DNS resolution, which are regularly over 1 second.
The most obvious example where DNS prefetching can help is when a user is looking at a page with many links to various domains, such as a search results page.
我们如何解决呢
对于以上问题我们怎么解决呢--> 使用 meta里面http-equiv来强制启动功能
说到这里,我们来总结一下
在Google的官方文档中,最后介绍到Chrome针对这个功能的各种优化。
浏览器实现方式
Chromium直接启动了8个完全异步的线程来做这个预解析,每个线程都处理一个队列,等待域名响应,最终操作系统会响应一个DNS解析给线程,然后线程剔除队列,开始下一个。因为有8个同时,基本上最多一两个会阻塞,其他都很快执行完。所以从上也可以看出直接修改本机的hosts会影响浏览器的解析。也算一种简单的翻墙方式。以下命令可以查看队列状态
about:histograms/DNS.PrefetchQueue
浏览器启动
Chrome浏览器启动的时候,就会自动的快速解析浏览器最近一次启动时记录的domin的前10个。所以一般说来你经常访问的网址打开的速度就比没有DNS解析得更快
Browser Startup
Chromium automatically remembers the first 10 domains that were resolved the last time the Chromium was started, and automatically starts to resolve these namesvery early in the startup process. As a result, the domains for a user's home page(s), along with any embedded domains (or anything the user "always" visits just after startup), are generally resolved before much of Chromium has ever loaded. When Chromium finally starts to try to load and render those pages, there is typically no DNS induced latency, and the application effectively "starts up" (becoming usable) faster. Average startup savings are 200ms or more, with common acceleration over 1 second.
功能的有效性
大家可以记住以下两个命令在chrome的地址栏输入查看
about:histograms/DNS
about:dns
在说浮动之前,先说几个前置概念
常规流 脱离常规流 包含块
网页中的流就是对文档的读取和输出的顺序. 其遵循从左到右, 从上到下的读取, 输出和显示顺序.
通常的资料中,常常称之为文档流,但是标准中没有这种说法,只有normal flow一词,应称之为普通流,或常规流.
在普通流中, 元素按照其在HTML 中的先后位置至上而下布局, 在这个过程中, 行内元素水平排列, 直到当行被占满然后换行; 块级元素则会被渲染为完整的一个新行.
除非另外指定, 否则所有元素默认都是普通流定位, 也可以说, 普通流中元素的位置由该元素在HTML 文档中的位置决定.
脱离常规流的元素会创建一个新的块级格式化上下文(Block Formatting Context: BFC)环境,其中包含的所有元素构成了一个小的布局环境,与页面中的其他内容分隔开来。而根元素,作为页面中所有内容的容器,自身脱离常规流,为整个文档创建了一个块级格式化上下文环境。
下列元素会脱离常规流:
一个元素的尺寸和位置经常受其包含块(containing block)的影响。大多数情况下,包含块就是这个元素最近的祖先块元素的内容区,但也不是总是这样。
确定一个元素的包含块的过程完全依赖于这个元素的 position 属性:
- transform 或 perspective 的值不是 none
- will-change 的值是 transform 或 perspective
- filter 的值不是 none 或 will-change 的值是 filter(只在 Firefox 下生效).
- contain 的值是 paint (例如: contain: paint;)
如果某些属性被赋予一个百分值的话,它的计算值是由这个元素的包含块计算而来的。这些属性包括盒模型属性和偏移属性:
上面两点可以总结为只能进行下左方向的运动
- 行内元素会出现在左浮动元素的右边一集右浮动元素的左边
- 左浮动元素的左边和右浮动元素的右边不能摆放行内元素
clear: none |left | right | both
块元素下移到某一变没有浮动元素为止
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.