@vuepress/plugin-pwa 2.0.0-beta.50-pre → 2.0.0-beta.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ export * from './usePwaEvent.js';
2
+ export * from './useSkipWaiting.js';
@@ -0,0 +1,2 @@
1
+ export * from './usePwaEvent.js';
2
+ export * from './useSkipWaiting.js';
@@ -0,0 +1,21 @@
1
+ import type { Emitter } from 'mitt';
2
+ import type { InjectionKey } from 'vue';
3
+ export declare type PwaEvent = Emitter<{
4
+ ready: ServiceWorkerRegistration;
5
+ registered: ServiceWorkerRegistration;
6
+ cached: ServiceWorkerRegistration;
7
+ updatefound: ServiceWorkerRegistration;
8
+ updated: ServiceWorkerRegistration;
9
+ offline: void;
10
+ error: Error;
11
+ }>;
12
+ export declare const pwaEventSymbol: InjectionKey<PwaEvent>;
13
+ export declare const usePwaEvent: () => Emitter<{
14
+ ready: ServiceWorkerRegistration;
15
+ registered: ServiceWorkerRegistration;
16
+ cached: ServiceWorkerRegistration;
17
+ updatefound: ServiceWorkerRegistration;
18
+ updated: ServiceWorkerRegistration;
19
+ offline: void;
20
+ error: Error;
21
+ }>;
@@ -0,0 +1,9 @@
1
+ import { inject } from 'vue';
2
+ export const pwaEventSymbol = Symbol('pwaEvent');
3
+ export const usePwaEvent = () => {
4
+ const pwaEvent = inject(pwaEventSymbol);
5
+ if (!pwaEvent) {
6
+ throw new Error('usePwaEvent() is called without provider.');
7
+ }
8
+ return pwaEvent;
9
+ };
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Call `skipWaiting()` inside current waiting worker
3
+ */
4
+ export declare const useSkipWaiting: (registration: ServiceWorkerRegistration) => void;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Call `skipWaiting()` inside current waiting worker
3
+ */
4
+ export const useSkipWaiting = (registration) => {
5
+ // get the waiting worker
6
+ const worker = registration.waiting;
7
+ // if there is no waiting worker, return directly
8
+ if (!worker)
9
+ return;
10
+ // post SKIP_WAITING message to the waiting worker
11
+ const channel = new MessageChannel();
12
+ worker.postMessage({ type: 'SKIP_WAITING' }, [channel.port2]);
13
+ };
@@ -0,0 +1,2 @@
1
+ declare const _default: import("@vuepress/client").ClientConfig;
2
+ export default _default;
@@ -0,0 +1,50 @@
1
+ import { defineClientConfig, withBase } from '@vuepress/client';
2
+ import mitt from 'mitt';
3
+ import { onMounted, provide } from 'vue';
4
+ import { pwaEventSymbol } from './composables/index.js';
5
+ const swFilename = __PWA_SW_FILENAME__;
6
+ export default defineClientConfig({
7
+ setup() {
8
+ if (__VUEPRESS_SSR__ || !swFilename)
9
+ return;
10
+ const log = (...args) => console.log('[@vuepress/plugin-pwa]', ...args);
11
+ // create event emitter and provide it
12
+ const event = mitt();
13
+ provide(pwaEventSymbol, event);
14
+ onMounted(async () => {
15
+ // lazy load register-service-worker
16
+ const { register } = await import('register-service-worker');
17
+ // Register service worker
18
+ register(withBase(swFilename), {
19
+ ready(registration) {
20
+ log('Service worker is active.');
21
+ event.emit('ready', registration);
22
+ },
23
+ registered(registration) {
24
+ log('Service worker has been registered.');
25
+ event.emit('registered', registration);
26
+ },
27
+ cached(registration) {
28
+ log('Content has been cached for offline use.');
29
+ event.emit('cached', registration);
30
+ },
31
+ updatefound(registration) {
32
+ log('New content is downloading.');
33
+ event.emit('updatefound', registration);
34
+ },
35
+ updated(registration) {
36
+ log('New content is available, please refresh.');
37
+ event.emit('updated', registration);
38
+ },
39
+ offline() {
40
+ log('No internet connection found. App is running in offline mode.');
41
+ event.emit('offline');
42
+ },
43
+ error(err) {
44
+ log('Error during service worker registration:', err);
45
+ event.emit('error', err);
46
+ },
47
+ });
48
+ });
49
+ },
50
+ });
@@ -0,0 +1 @@
1
+ export * from './composables/index.js';
@@ -0,0 +1 @@
1
+ export * from './composables/index.js';
@@ -0,0 +1,4 @@
1
+ import type { App } from '@vuepress/core';
2
+ import type { GenerateSWOptions } from 'workbox-build';
3
+ export declare type GenerateSWConfig = Omit<GenerateSWOptions, 'swDest' | 'globDirectory'>;
4
+ export declare const generateServiceWorker: (app: App, serviceWorkerFilename: string, generateSWConfig: GenerateSWConfig) => Promise<void>;
@@ -0,0 +1,36 @@
1
+ import { logger } from '@vuepress/utils';
2
+ const assetsExtensions = [
3
+ // basic
4
+ 'html',
5
+ 'js',
6
+ 'css',
7
+ // images
8
+ 'png',
9
+ 'jpg',
10
+ 'jpeg',
11
+ 'gif',
12
+ 'svg',
13
+ // fonts
14
+ 'woff',
15
+ 'woff2',
16
+ 'eot',
17
+ 'tff',
18
+ 'otf',
19
+ ];
20
+ export const generateServiceWorker = async (app, serviceWorkerFilename, generateSWConfig) => {
21
+ // lazy-load workbox-build
22
+ const { generateSW } = await import('workbox-build');
23
+ const globDirectory = app.dir.dest();
24
+ const swDest = app.dir.dest(serviceWorkerFilename);
25
+ const { warnings } = await generateSW({
26
+ dontCacheBustURLsMatching: new RegExp(`\\.[0-9a-f]{8}\\.(${assetsExtensions.join('|')})$`),
27
+ globPatterns: [`**/*.{${assetsExtensions.join(',')}}`],
28
+ mode: app.env.isDebug ? 'development' : 'production',
29
+ sourcemap: app.env.isDebug,
30
+ ...generateSWConfig,
31
+ // should not be override by user config
32
+ globDirectory,
33
+ swDest,
34
+ });
35
+ warnings.forEach((warning) => logger.warn('[@vuepress/plugin-pwa]', warning));
36
+ };
@@ -0,0 +1,4 @@
1
+ import { pwaPlugin } from './pwaPlugin.js';
2
+ export * from './generateServiceWorker.js';
3
+ export * from './pwaPlugin.js';
4
+ export default pwaPlugin;
@@ -0,0 +1,4 @@
1
+ import { pwaPlugin } from './pwaPlugin.js';
2
+ export * from './generateServiceWorker.js';
3
+ export * from './pwaPlugin.js';
4
+ export default pwaPlugin;
@@ -0,0 +1,17 @@
1
+ import type { Plugin } from '@vuepress/core';
2
+ import type { GenerateSWConfig } from './generateServiceWorker.js';
3
+ /**
4
+ * Options for @vuepress/plugin-pwa
5
+ */
6
+ export interface PwaPluginOptions extends GenerateSWConfig {
7
+ /**
8
+ * Filename of the generated service worker file
9
+ *
10
+ * If you put it into a sub directory, the `scope` of service worker
11
+ * might be affected
12
+ *
13
+ * @default 'service-worker.js'
14
+ */
15
+ serviceWorkerFilename?: string;
16
+ }
17
+ export declare const pwaPlugin: ({ serviceWorkerFilename, ...generateSWConfig }?: PwaPluginOptions) => Plugin;
@@ -0,0 +1,23 @@
1
+ import { getDirname, path, withSpinner } from '@vuepress/utils';
2
+ import { generateServiceWorker } from './generateServiceWorker.js';
3
+ const __dirname = getDirname(import.meta.url);
4
+ export const pwaPlugin = ({ serviceWorkerFilename = 'service-worker.js', ...generateSWConfig } = {}) => (app) => {
5
+ const plugin = {
6
+ name: '@vuepress/plugin-pwa',
7
+ };
8
+ if (app.env.isDev) {
9
+ return plugin;
10
+ }
11
+ return {
12
+ ...plugin,
13
+ clientConfigFile: path.resolve(__dirname, '../client/config.js'),
14
+ alias: {
15
+ // workaround for https://github.com/vitejs/vite/issues/7621
16
+ '@vuepress/plugin-pwa/client': path.resolve(__dirname, '../client/index.js'),
17
+ },
18
+ define: {
19
+ __PWA_SW_FILENAME__: serviceWorkerFilename,
20
+ },
21
+ onGenerated: (app) => withSpinner('Generating service worker')(() => generateServiceWorker(app, serviceWorkerFilename, generateSWConfig)),
22
+ };
23
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vuepress/plugin-pwa",
3
- "version": "2.0.0-beta.50-pre",
3
+ "version": "2.0.0-beta.51",
4
4
  "description": "VuePress plugin - progressive web application",
5
5
  "keywords": [
6
6
  "vuepress-plugin",
@@ -27,12 +27,12 @@
27
27
  "main": "./lib/node/index.js",
28
28
  "types": "./lib/node/index.d.ts",
29
29
  "files": [
30
- "./lib"
30
+ "lib"
31
31
  ],
32
32
  "dependencies": {
33
- "@vuepress/client": "2.0.0-beta.50-pre",
34
- "@vuepress/core": "2.0.0-beta.50-pre",
35
- "@vuepress/utils": "2.0.0-beta.50-pre",
33
+ "@vuepress/client": "2.0.0-beta.51",
34
+ "@vuepress/core": "2.0.0-beta.51",
35
+ "@vuepress/utils": "2.0.0-beta.51",
36
36
  "mitt": "^3.0.0",
37
37
  "register-service-worker": "^1.7.2",
38
38
  "vue": "^3.2.37",