{"id":25322726,"url":"https://github.com/qzruncode/cacheweb-webpack-plugin","last_synced_at":"2026-04-30T10:04:22.883Z","repository":{"id":57192581,"uuid":"391658771","full_name":"qzruncode/cacheweb-webpack-plugin","owner":"qzruncode","description":"Based on service worker, a unique buffer overflow algorithm is designed","archived":false,"fork":false,"pushed_at":"2022-02-27T13:47:17.000Z","size":105,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-13T23:47:44.309Z","etag":null,"topics":["cache","webpack","webpack-plugin"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qzruncode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-01T15:03:22.000Z","updated_at":"2022-02-10T08:03:21.000Z","dependencies_parsed_at":"2022-08-24T03:10:36.312Z","dependency_job_id":null,"html_url":"https://github.com/qzruncode/cacheweb-webpack-plugin","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qzruncode%2Fcacheweb-webpack-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qzruncode%2Fcacheweb-webpack-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qzruncode%2Fcacheweb-webpack-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qzruncode%2Fcacheweb-webpack-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qzruncode","download_url":"https://codeload.github.com/qzruncode/cacheweb-webpack-plugin/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247744129,"owners_count":20988779,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","webpack","webpack-plugin"],"created_at":"2025-02-13T23:47:58.614Z","updated_at":"2026-04-30T10:04:17.840Z","avatar_url":"https://github.com/qzruncode.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM version](https://img.shields.io/npm/v/cacheweb-webpack-plugin.svg)](https://www.npmjs.com/package/cacheweb-webpack-plugin)\n[![NPM package](https://img.shields.io/npm/dy/cacheweb-webpack-plugin.svg)](https://www.npmjs.com/package/cacheweb-webpack-plugin)\n\n## cacheweb-webpack-plugin\n\n1. 自动生成sw文件，对前端文件资源和请求做缓存，大幅优化前端页面响应速度\n2. 借鉴LRU和LFU算法，设计并实现LRU-FT算法，能够高效处理缓存溢出\n3. 在更新预缓存列表时，采用高效的diff对比并且利用indexedDB存储数据\n4. 最好和[sww-cli](https://www.npmjs.com/package/sww-cli)配套使用，此webpack构建工具是在开发测试cacheweb-webpack-plugin时配套开发的，能够将前端的文件打包成合适的大小更加便于缓存更新，并且能够用于大型的前端程序开发，省去频繁配置。\n5. 将前端的请求归类为需要永久缓存的请求、需要缓存优先的请求和不需要缓存的请求。\n6. 此插件为本人在编写硕士毕业论文时设计的一款缓存插件，欢迎大家指出缺点和建议。以下将sw的设计流程图分享出来。\n\n\u003cimg width=\"1200\" src=\"http://qzruncode.github.io/image/sw-code.png\" alt=\"keyboard\" \u003e\n\n### 安装\n```\nnpm install cacheweb-webpack-plugin\n```\n\n### 在webpack中使用\n```js\n// 在webpack的plugins配置项中添加\nnew cachewebWebpackPlugin({\n  // chacheName: 缓存的名称，当需要彻底刷新缓存的时候，将此字段修改成其他字段即可\n  chacheName: 'SW',\n  // expirationHour: 存入缓存的有效期，只对 cacheFirstList 中指定的url请求有效 \n  expirationHour: 72, \n  // maxNum: 动态缓存可容纳的最大数量\n  maxNum: 50, \n  // noCacheFileList: 默认情况下，经过webpack打包后的文件资源会全部进入缓存，这里可以指定部分不需要进入缓存的文件url\n  noCacheFileList: ['index.html', 'register.js'], \n  // cacheFirstList: 指定需要动态缓存的url，也就是说部分请求返回的数据更新不是特别频繁，需要短暂的缓存可以在这里指定\n  cacheFirstList: ['cacheFirstTest', 'acacheFirstTes', 'bcacheFirstTes'],\n  // permanentCacheList: 指定需要永久缓存的资源\n  permanentCacheList: ['test'],\n})\n```\n\n### 注册\n```js\n// 在前端项目中新建一个sw目录，创建register.js\nwindow.onload = () =\u003e {\n  if (\"serviceWorker\" in navigator) {\n    // register方法：页面刷新就会执行，但是 sw 里面的代码只有 sw文件发生改变才会执行\n    navigator.serviceWorker.register('sw.js', { \n      scope: './'\n    }).then(registration =\u003e {\n      let serviceWorker;\n      if (registration.installing) {\n        serviceWorker = registration.installing;\n      } else if (registration.waiting) {\n        serviceWorker = registration.waiting;\n      } else if (registration.active) {\n        serviceWorker = registration.active;\n      }\n      if (serviceWorker) {\n        serviceWorker.addEventListener('statechange', function (e) {\n          console.log('sw的状态改变：' + e.target.state);\n        });\n      }\n    });\n    navigator.serviceWorker.onmessage = (e) =\u003e {\n      // cachewebWebpackPlugin 插件在捕获到错误后，在这里可以做异常处理\n      const { data } = e;\n      if(data.type === 'FetchError') {\n        // 请求失败;\n      }else if(data.type === 'NetWorkError') {\n        // 断网\n      }else if(data.type === 'RefreshClient') {\n        // 刷新页面，有新版本的sw安装\n        location.reload();\n      }\n    };\n  } else {\n    console.log('sw不支持');\n  }\n};\n```\n\n### 开发\n\u003e 源码在 [cacheweb-webpack-plugin](https://github.com/qzruncode/cacheweb-webpack-plugin)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqzruncode%2Fcacheweb-webpack-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqzruncode%2Fcacheweb-webpack-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqzruncode%2Fcacheweb-webpack-plugin/lists"}