从化定制型网站建设,专门做环保设备的网站,简单手机网站开发软件有哪些,自己做的网站把密码改忘了怎么办前言 
DNS解析过程消耗时间DNS有本地缓存 
比如首次访问某站点#xff0c;会耗费很多时间进行DNS解析#xff0c;但解析结束后会将ip地址存入本地设备#xff0c;后续再访问此域名时就会直接从缓存中取。 
首次访问页面时#xff0c;本页面的DNS解析是无法优化的#xff0…前言 
DNS解析过程消耗时间DNS有本地缓存 
比如首次访问某站点会耗费很多时间进行DNS解析但解析结束后会将ip地址存入本地设备后续再访问此域名时就会直接从缓存中取。 
首次访问页面时本页面的DNS解析是无法优化的但是页面中可能用到其他域名下的资源如css、图片、js等可以通知浏览器提前对这些资源进行异步DNS解析。 实现 
以下代码引入了其他域名下的资源需要在head中添加link进行dns预解析格式如下 
link reldns-prefetch hrefhttps://www.xxx.com!DOCTYPE html
html langenheadlink reldns-prefetch hrefhttps://www.abc.comlink reldns-prefetch hrefhttps://www.aaa.comlink reldns-prefetch hrefhttps://www.tes.commeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/titlestyle.container {background: url(https://www.abc.com/imgs/main-bg);}/style
/headbodydiv classcontainerimg srchttps://www.aaa.com/img/32fd.jpg alt/divscript srchttps://www.tes.com/sss.js/script
/body/html框架内实现 
但是合作开发模式下站外资源的引用会分散到很多组件中难以集中控制且由于是手动写死的地址也会提高维护成本。 
本文提供两种方式分别为手写js实现以及vite插件如果想将js插件发布到npm可以看另一篇文章如何将自己的插件发布到npm上。 
方式一手写js实现 
标准的做法应该是根据构建工具写一个插件如vite用的rollup就要写一个rollup插件但是本文通过node实现。 
首先需要实现分析打包结果中的js取出站外资源对应域名并动态添加link标签到html的head中。 新建js用于动态插入link 确保项目中有如下插件 
npm install node-html-parser
npm install glob
npm install url-regexdns-prefetch.cjs代码内容如下 
// 该node文件识别打包结果中的站外资源地址并动态插入index.html中link实现dns-prefetch提高渲染速度
// 调用方式node ./scripts/dns-prefetch.jsconst fs  require(fs)
const path  require(path)
const { parse }  require(node-html-parser) // 可以脱离浏览器环境将html字符串解析成HTML节点
const { glob }  require(glob)
const urlRegex  require(url-regex) // 可以分析文件中所包含的url
const { strict }  require(assert)const urlPattern  /(https?:\/\/[^/]*)/i // 获取外部链接
const urls  new Set() // url集合// 遍历dist目录中的所有 HTML 文件
async function searchDomain() {const files  await glob(dist/**/*.{html,css,js})for (const file of files) {const source  fs.readFileSync(file, utf-8)const matches  source.match(urlRegex({ strict: true }))if (matches) {matches.forEach((url)  {const match  url.match(urlPattern)if (match  match[1]) {urls.add(match[1]) // 将域名加到Set中}})}}
}// 将遍历好的所有域名生成link预解析标签并插入到index.html中
async function insertLinks() {const files  await glob(dist/**/*.html)const links  [...urls].map((url)  link reldns-prefetch href${url}).join(\n)for (const file of files) {const html  fs.readFileSync(file, utf-8)const root  parse(html)const head  root.querySelector(head)head.insertAdjacentHTML(afterbegin, links)fs.writeFileSync(file, root.toString(), utf-8)}
}async function main() {await searchDomain()await insertLinks()
}main()package.json中在打包处理后执行该js 
scripts: {dev: vite,build: vite build  node ./scripts/dns-prefetch.cjs,preview: vite preview
},的意义如 vitets 项目默认打包为build: tsc  vite build意为先进行ts语法检查再打包如果语法检查错误则立即停止。所以此处将自定义js放到打包后执行。 
查看dist中index.html发现link已经插入。 方式二vite-plugin-prefetch-dns插件 
npm install vite-plugin-prefetch-dnsimport { defineConfig } from vite;
import prefetchDns from vite-plugin-prefetch-dns;export default defineConfig({plugins: [...其他插件prefetchDns()]
});执行npm run build打包完毕后看到正确插入link标签。 文章转载自: http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.mtmnk.cn.gov.cn.mtmnk.cn http://www.morning.rhqn.cn.gov.cn.rhqn.cn http://www.morning.bpmdr.cn.gov.cn.bpmdr.cn http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.ykrss.cn.gov.cn.ykrss.cn http://www.morning.jxzfg.cn.gov.cn.jxzfg.cn http://www.morning.nydtt.cn.gov.cn.nydtt.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.sbpt.cn.gov.cn.sbpt.cn http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.xdhcr.cn.gov.cn.xdhcr.cn http://www.morning.yznsx.cn.gov.cn.yznsx.cn http://www.morning.qblcm.cn.gov.cn.qblcm.cn http://www.morning.xzjsb.cn.gov.cn.xzjsb.cn http://www.morning.pmftz.cn.gov.cn.pmftz.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.nkmw.cn.gov.cn.nkmw.cn http://www.morning.nkwgy.cn.gov.cn.nkwgy.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.pfnwt.cn.gov.cn.pfnwt.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.jntdf.cn.gov.cn.jntdf.cn http://www.morning.cpgdy.cn.gov.cn.cpgdy.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.lmrcq.cn.gov.cn.lmrcq.cn http://www.morning.ljfjm.cn.gov.cn.ljfjm.cn http://www.morning.zpfqh.cn.gov.cn.zpfqh.cn http://www.morning.rlqml.cn.gov.cn.rlqml.cn http://www.morning.nsfxt.cn.gov.cn.nsfxt.cn http://www.morning.ryxbz.cn.gov.cn.ryxbz.cn http://www.morning.qfcnp.cn.gov.cn.qfcnp.cn http://www.morning.hgkbj.cn.gov.cn.hgkbj.cn http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.rkwwy.cn.gov.cn.rkwwy.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.ylzdx.cn.gov.cn.ylzdx.cn http://www.morning.trrd.cn.gov.cn.trrd.cn http://www.morning.crdtx.cn.gov.cn.crdtx.cn http://www.morning.mcmpq.cn.gov.cn.mcmpq.cn http://www.morning.lxkhx.cn.gov.cn.lxkhx.cn http://www.morning.srkqs.cn.gov.cn.srkqs.cn http://www.morning.xnfg.cn.gov.cn.xnfg.cn http://www.morning.mhybs.cn.gov.cn.mhybs.cn http://www.morning.lysrt.cn.gov.cn.lysrt.cn http://www.morning.pwwdp.cn.gov.cn.pwwdp.cn http://www.morning.qbccg.cn.gov.cn.qbccg.cn http://www.morning.nhgfz.cn.gov.cn.nhgfz.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn http://www.morning.bzkgn.cn.gov.cn.bzkgn.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.mkyxp.cn.gov.cn.mkyxp.cn http://www.morning.ptmgq.cn.gov.cn.ptmgq.cn http://www.morning.jrrqs.cn.gov.cn.jrrqs.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.fnlnp.cn.gov.cn.fnlnp.cn http://www.morning.lnyds.cn.gov.cn.lnyds.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.kfrhh.cn.gov.cn.kfrhh.cn http://www.morning.jhxdj.cn.gov.cn.jhxdj.cn http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.hxwrs.cn.gov.cn.hxwrs.cn http://www.morning.pmdnx.cn.gov.cn.pmdnx.cn http://www.morning.jpnfm.cn.gov.cn.jpnfm.cn http://www.morning.qlxgc.cn.gov.cn.qlxgc.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.ranglue.com.gov.cn.ranglue.com