金医疗医疗转板申请已获批准,即将在联交所主板上市
06-17
ele[key] =" />
方案一代码语言:javascript copy const code = `!(function () { "use strict" const assert = $$assert function createElement(tag) { const ele = document.createElement(tag.tagName) if (tag.attributes) { Object .keys(tag.attributes) .forEach(key => ele[key] = tag.attributes[key]) } if (tag. innerHTML) { ele.innerHTML = tag.innerHTML } ele.async = false ele.defer = false return ele } 函数appendTag(tag) { document.body.appendChild(createElement(tag)) } 函数inlineTag(tag, innerHTML) { let path if (tag.tagName.toUpperCase() === 'LINK') { tag.tagName = 'style' path = tag.attributes.href 删除 tag.attributes } else { path = tag.attributes.src 删除标签。 attribute.src tag.attributes.async = false } tag.innerHTML = \`// 来自 CacheStorage: \${path} \\n\${innerHTML}\` return tag } const cacheName= 'assert' try { window.caches .open(cacheName) .then((cache) => { return assert.map((tag) => { if (tag.innerHTML) { return tag } if (!tag.attributes .src && !tag.attributes.href) { throw new Error(\`tag.innerHTML 与 tag.attributes.src、tag.attributes.href 必传一个, 当前为 \${JSON.stringify(tag)}\` ) } const assertURL = tag.tagName.toUpperCase() === 'SCRIPT' ? tag.attributes.src : tag.attributes.href return cache.match(assertURL) .then((response) => { if (!response ) { return 缓存 .add(assertURL) .then( () => 缓存 .match(assertURL) .then(response => response.text()) .then(innerHTML => inlineTag(tag, innerHTML)) ) } else {console.info(\`来自 CacheStorage: \${assertURL}\`) return response.text().then(innerHTML => inlineTag(tag, innerHTML)) } }) }) }) .then((scriptList) = > { // 保证执行顺序 scriptList .reduce( (a, b) => a .then(appendTag) .then(() => b) ) .then(appendTag) }) } catch (e) { console .error(e) setTimeout(() => { if (window.fundebug) {fundebug.notifyError(e) } }, ) constfragment = document.createDocumentFragment()assert.map(tag => { const ele = createElement( tag)fragment.appendChild(ele) }) document.body.appendChild(fragment) }})();`const compiler = source => require('@babel/core').transformSync( source, {预设: [ ' @babel/preset-env' ] } ) .codeclass CacheStaticPlugin{ 构造函数(htmlWebpackPlugin, test) { this.htmlWebpackPlugin = htmlWebpackPlugin this.test = test } isTargetTag(tag) { let link const result = (tag.tagName === 'script' || tag.tagName === 'link' ) && !tag.innerHTML && tag.attributes && (link = (tag.attributes.src || tag.attributes.href)) && (this.test instanceof RegExp ? this.test.test(link) : this.test(链接)) if (!result) { } return !!result }jectCacheStaticCode(scriptList) { return compiler(code.replace('$$assert', JSON.stringify(scriptList))) } apply(compiler) { compiler.hooks .compilation.tap('StaticCachePlugin', 编译 => { const hooks = this.htmlWebpackPlugin.getHooks(compilation) hooks.alterAssetTagGroups.tap('StaticCachePlugin', 资产 => { asset.bodyTags = [ ...assets.bodyTags.过滤器(标签=>!this.isTargetTag(tag)), { tagName: 'script', innerHTML: this.injectCacheStaticCode(assets.bodyTags.filter(this.isTargetTag.bind(this))), closeTag: true } ] }) }) }}module.exports = CacheStaticPlugin当这是一个Webpack插件时,通过传入特定的正则表达式,过滤掉需要缓存的静态JS和CSS文件,并将一段代码注入到HTML页面中。
当浏览器运行这段代码时,会通过cache.add() API下载带有特定标识符的js和css文件,并存储在CacheStorage中。然后将下载的代码通过script标签注入到HTML中。
(注入时需要考虑顺序问题)优化思路方案第二代码语言:javascript copy //cacheconst OFFLINE_SUFFIX = '?offline';let CURRENT_CACHES = {offline: 'offline'};const cacheFiles = [ '/?offline'] ;self .addEventListener('install', function(event) { event.waitUntil(self.skipWaiting()); event.waitUntil( caches.open(CURRENT_CACHES.offline).then(cache => { return cache.addAll(cacheFiles) ); }) );});const matchCache = 异步函数(事件, 缓存) { const res = wait Promise.all([ caches.match(event.request.url + OFFLINE_SUFFIX), caches.match(event.request.网址)]); if (res.some(e => e)) { return res.filter(e => { if (e) return e; })[0]; } else { 返回新的 Response(); }}; const matchOfflineUrl = function(origin, event) { if (event.request.method !== 'GET') { return false; } } } 返回 (cacheFiles.some(url => origin + url === event.request.url) || cacheFiles.some(url => origin + url === event.request.url + OFFLINE_SUFFIX) );};self.addEventListener( 'fetch', function(event) { const origin = location.origin; if (matchOfflineUrl(origin) , event)) { event.respondWith( fetch(event.request.url) .then(response => { if (!response.ok) { return caches.open(CURRENT_CACHES.offline).then(cache => { event. request.url === origin + '/' && cache.add(cacheFiles[0]); , 缓存).then(r => r); } }) .catch(err => { console.error(err); 返回 matchCache(event, 缓存).then(r => r); }) } });该解决方案使用Service Worker技术来监控fetch方法。当fetch请求失败时,自动使用Ca返回cheStorage中的缓存。
当用户再次连接到互联网时,缓存中存储的信息就会更新。这个解决方案类似于Google Workbox中的某个策略解决方案。
版权声明:本文内容由互联网用户自发贡献,本站不拥有所有权,不承担相关法律责任。如果发现本站有涉嫌抄袭的内容,欢迎发送邮件 举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。
标签:
相关文章
06-17
06-18
06-21
06-21
最新文章
【玩转GPU】ControlNet初学者生存指南
【实战】获取小程序中用户的城市信息(附源码)
包雪雪简单介绍Vue.js:开学
Go进阶:使用Gin框架简单实现服务端渲染
线程池介绍及实际案例分享
JMeter 注释 18 - JMeter 常用配置组件介绍
基于Sentry的大数据权限解决方案
【云+社区年度征文集】GPE监控介绍及使用