-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-remove-pwa): add remove pwa plugin (#54)
- Loading branch information
1 parent
44fba50
commit 58b7e32
Showing
14 changed files
with
227 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# remove-pwa | ||
|
||
<NpmBadge package="@vuepress/plugin-remove-pwa" /> | ||
|
||
This plugin removes any related service worker from your VuePress site, so that users can still get updates if you removed any PWA plugin after enabling it. | ||
|
||
::: tip Why this plugin is needed if you used PWA plugin once? | ||
|
||
PWA plugins like [`@vuepress/plugin-pwa`](./pwa.md) register service worker to your site, which will cache your site and make it available offline. | ||
|
||
However, if you remove pwa plugin, the old service worker will still be there, but they can never get an update because they can never found a new service worker to update to. So users will stay with the old version of your site. | ||
|
||
To solve this problem: | ||
|
||
1. A new service worker with empty contents shall be generated in the original place. | ||
1. The new service worker shall attempt to remove contents that old service worker cached, then it should unregister itself. | ||
|
||
::: | ||
|
||
## Usage | ||
|
||
```bash | ||
npm i -D @vuepress/plugin-remove-pwa@next | ||
``` | ||
|
||
```ts | ||
import { removePwaPlugin } from '@vuepress/plugin-remove-pwa' | ||
|
||
export default { | ||
plugins: [ | ||
removePwaPlugin({ | ||
// options | ||
}), | ||
], | ||
} | ||
``` | ||
|
||
## Config | ||
|
||
### cachePrefix | ||
|
||
- Type: `string` | ||
- Default: `'workbox'` | ||
- Details: The cache prefix for the service worker. | ||
|
||
### swLocation | ||
|
||
- Type: `string` | ||
- Default: `'service-worker.js'` | ||
- Details: The location of the old service worker. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# remove-pwa | ||
|
||
<NpmBadge package="@vuepress/plugin-remove-pwa" /> | ||
|
||
此插件从你的 VuePress 站点中删除任何相关的 Service Worker,因此如果你在启用后任何 PWA 插件后移除它们,用户仍然可以获得更新。 | ||
|
||
::: tip 如果你启用过 PWA,为什么需要这个插件? | ||
|
||
PWA 插件,如 [`@vuepress/plugin-pwa`](./pwa.md) 将 Service Worker 注册到你的站点,这将缓存你的站点并使其离线可用。 | ||
|
||
但是,如果你删除 PWA 插件,先前的 Service Worker 仍将在那里,但它们永远无法获得更新,因为他们永远无法找到要更新的新 Service Worker。 因此,用户将继续使用你网站的旧版本。 | ||
|
||
要解决这个问题: | ||
|
||
1. 一个新的内容为空的 Service Worker 需要生成在原位置。 | ||
|
||
1. 新的 Service Worker 应该尝试删除旧 Service Worker 缓存的内容,然后它应该注销自己。 | ||
|
||
::: | ||
|
||
## Usage | ||
|
||
```bash | ||
npm i -D @vuepress/plugin-remove-pwa@next | ||
``` | ||
|
||
```ts | ||
import { removePwaPlugin } from '@vuepress/plugin-remove-pwa' | ||
|
||
export default { | ||
plugins: [ | ||
removePwaPlugin({ | ||
// options | ||
}), | ||
], | ||
} | ||
``` | ||
|
||
## Config | ||
|
||
### cachePrefix | ||
|
||
- 类型:`string` | ||
- 默认值:`'workbox'` | ||
- 详情:Service worker 的缓存前缀。 | ||
|
||
### swLocation | ||
|
||
- 类型: `string` | ||
- 默认值:`'service-worker.js'` | ||
- 详情:旧 Service Worker 的位置。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "@vuepress/plugin-remove-pwa", | ||
"version": "2.0.0-rc.7", | ||
"description": "VuePress plugin - remove-pwa", | ||
"keywords": [ | ||
"vuepress-plugin", | ||
"vuepress", | ||
"plugin", | ||
"remove-pwa" | ||
], | ||
"homepage": "https://ecosystem.vuejs.press/plugins/rtl.html", | ||
"bugs": { | ||
"url": "https://github.com/vuepress/ecosystem/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/vuepress/ecosystem.git", | ||
"directory": "plugins/plugin-remove-pwa" | ||
}, | ||
"license": "MIT", | ||
"author": { | ||
"name": "Mr.Hope", | ||
"email": "[email protected]", | ||
"url": "https://mister-hope.com" | ||
}, | ||
"type": "module", | ||
"exports": { | ||
".": "./lib/node/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"main": "./lib/node/index.js", | ||
"types": "./lib/node/index.d.ts", | ||
"files": [ | ||
"lib" | ||
], | ||
"scripts": { | ||
"build": "tsc -b tsconfig.build.json", | ||
"clean": "rimraf --glob ./lib ./*.tsbuildinfo" | ||
}, | ||
"peerDependencies": { | ||
"vuepress": "2.0.0-rc.2" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './removePWAPlugin.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { PluginObject } from 'vuepress/core' | ||
import { removeLeadingSlash } from 'vuepress/shared' | ||
import { fs } from 'vuepress/utils' | ||
|
||
export interface RemovePWAPluginOptions { | ||
/** | ||
* Original service worker cache prefix | ||
* | ||
* 原始 service worker 缓存前缀 | ||
* | ||
* @default "workbox" | ||
*/ | ||
cachePrefix?: string | ||
|
||
/** | ||
* Original service worker location relative to dest folder | ||
* | ||
* 相对于 dest 文件夹的原始 service worker 位置 | ||
* | ||
* @default "service-worker.js" | ||
*/ | ||
swLocation?: string | ||
} | ||
|
||
export const removePWAPlugin = ({ | ||
cachePrefix = 'workbox', | ||
swLocation = 'service-worker.js', | ||
}: RemovePWAPluginOptions): PluginObject => ({ | ||
name: 'vuepress-plugin-remove-pwa', | ||
|
||
onGenerated: async (app): Promise<void> => { | ||
const SERVICE_WORKER_CONTENT = `self.addEventListener("install",(()=>self.skipWaiting())),self.addEventListener("activate",(()=>{const e=[${JSON.stringify(cachePrefix)},"precache-v2","undefined"!=typeof registration?registration.scope:""].filter((e=>e&&e.length>0)).join("-");self.caches.open(e).then((e=>e.keys())).then((e=>e.forEach((e=>cache.delete(e))))).then((()=>{self.clients.claim().then((()=>{})).then((()=>self.registration.unregister())).then((()=>self.clients.matchAll())).then((e=>e.forEach((e=>e.navigate(e.url)))))}))}));` | ||
|
||
await fs.writeFile( | ||
app.dir.dest(removeLeadingSlash(swLocation)), | ||
SERVICE_WORKER_CONTENT, | ||
'utf-8', | ||
) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.build.json", | ||
"compilerOptions": { | ||
"rootDir": "./src", | ||
"outDir": "./lib" | ||
}, | ||
"include": ["./src"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters