{"id":18024011,"url":"https://github.com/lete114/sw-racing","last_synced_at":"2025-03-27T00:30:48.433Z","repository":{"id":40643064,"uuid":"496488779","full_name":"Lete114/SW-Racing","owner":"Lete114","description":"使用Service Workers拯救你网站上的jsdelivr CDN","archived":false,"fork":false,"pushed_at":"2022-08-08T13:01:49.000Z","size":274,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-22T18:11:21.504Z","etag":null,"topics":["cdn","jsdelivr","jsdelivr-cdn"],"latest_commit_sha":null,"homepage":"https://lete114.github.io/SW-Racing/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lete114.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-26T05:10:43.000Z","updated_at":"2024-08-14T15:00:15.000Z","dependencies_parsed_at":"2022-08-25T18:02:46.758Z","dependency_job_id":null,"html_url":"https://github.com/Lete114/SW-Racing","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/Lete114%2FSW-Racing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lete114%2FSW-Racing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lete114%2FSW-Racing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lete114%2FSW-Racing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lete114","download_url":"https://codeload.github.com/Lete114/SW-Racing/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245760564,"owners_count":20667886,"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":["cdn","jsdelivr","jsdelivr-cdn"],"created_at":"2024-10-30T07:11:39.897Z","updated_at":"2025-03-27T00:30:48.109Z","avatar_url":"https://github.com/Lete114.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## 简介\n\n今年 [jsdelivr](https://www.jsdelivr.com/) 频繁出现问题，导致众多使用 jsdelivr 的网站无法正常访问\n\n对于一些内容较少的网站，只需少量修改即可完成，但对于内容较多的网站，入侵式调整网站的 **jsdelivr** 链接，使得迁移工作相当麻烦\n\n甚至对于一些很少接触开发的站长来说，迁移工作可能会耗费很多时间\n\n## 使用 Service Workers\n\n\u003e Hexo 插件: [hexo-sw-racing](https://github.com/Lete114/SW-Racing/tree/hexo)\n\n\u003e Service Workers 在本文统一叫 sw\n\n关于 **Service Workers** 是什么？自行查看 [https://developer.mozilla.org/zh-CN/docs/Web/API/Service_Worker_API/Using_Service_Workers](https://developer.mozilla.org/zh-CN/docs/Web/API/Service_Worker_API/Using_Service_Workers)\n\n关于 sw 的浏览器兼容性，市面上 **94%** 的浏览器都已经支持，具体以 [https://caniuse.com/?search=ServiceWorkers](https://caniuse.com/?search=ServiceWorkers) 为准\n\nsw 可以拦截网站的所有请求，捕获并篡改请求，使用也是有要求的，必须是**HTTPS**协议，并且 sw 必须与当前**网站同源**\n\n## 注册 sw\n\n在你的网站插入如下 js，并将仓库中`sw.js`文件放到你网站的根目录\n\n将安装代码放置在`\u003chead\u003e`之后\n\n```html\n\u003cscript\u003e\n  ;(function () {\n    if (navigator.serviceWorker) {\n      navigator.serviceWorker\n        .register('/sw.js')\n        .then((result) =\u003e {\n          // 判断是否安装了sw\n          if (localStorage.getItem('installSW')) return\n          localStorage.setItem('installSW', true)\n          // 这里就不用清理setInterval了，因为页面刷新后就没有了\n          const timer = setInterval(() =\u003e {\n            // 判断sw安装后，是否处于激活状态，激活后刷新页面\n            if (result.active.state === 'activated') {\n              clearInterval(timer)\n              fetch(window.location.href)\n                .then((res) =\u003e res.text())\n                .then((text) =\u003e {\n                  document.open()\n                  document.write(text)\n                  document.close()\n                })\n            }\n          }, 100)\n        })\n        .catch(console.log)\n    }\n  })()\n\u003c/script\u003e\n```\n\n## 注销 sw\n\n直接删除`sw.js`文件并删除安装代码，新用户不会安装上 sw，但已经安装过的老用户 sw 则会继续正常运作，这将导致老用户可能访问不正常的 bug\n\n所以正确的删除姿势是将注册的 sw 代码改为如下代码：\n\n\u003e 请保留注销 sw 代码在你的网站至少 3 个月或者更久的时间(代码体积小，且是异步代码，所以不会对你网站的渲染照成影响)\n\n```js\nif ('serviceWorker' in navigator) {\n  navigator.serviceWorker\n    .getRegistrations()\n    .then((r) =\u003e {\n      for (let i of r) i.unregister()\n      console.log('注销成功')\n    })\n    .catch(() =\u003e console.log('注销失败'))\n}\n```\n\n## 效果图\n\n![](img/1.png)\n![](img/2.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flete114%2Fsw-racing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flete114%2Fsw-racing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flete114%2Fsw-racing/lists"}