From 1dfb30598e537f056b19f7b74aa837deb88f953c Mon Sep 17 00:00:00 2001 From: DuCanhGH <75556609+DuCanhGH@users.noreply.github.com> Date: Mon, 5 Dec 2022 15:48:16 +0700 Subject: [PATCH] fix(types): InjectManifest's options and GenerateSW's shouldn't be interchangeable anymore --- README.md | 2 +- src/index.ts | 6 +++++ src/private_types.ts | 55 ++++++++++++++++++++++++++++++++++++++++---- src/types.ts | 2 +- 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 82f6612b..12ab9419 100644 --- a/README.md +++ b/README.md @@ -308,7 +308,7 @@ export default withPWA({ ### Available options -See [PluginOptions](https://github.com/DuCanhGH/next-pwa/blob/master/src/types.ts?plain=1#L3) +See [PluginOptions](https://github.com/DuCanhGH/next-pwa/blob/master/src/types.ts?plain=1#L5) ### Other options diff --git a/src/index.ts b/src/index.ts index f088d551..61e4ead2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -74,6 +74,12 @@ const withPWAInit = ( ...workbox } = workboxOptions; + Object.keys(workbox).forEach( + (key) => + workbox[key as keyof typeof workbox] === undefined && + delete workbox[key as keyof typeof workbox] + ); + let importScripts: string[] = []; let runtimeCaching: RuntimeCaching[] = defaultCache; diff --git a/src/private_types.ts b/src/private_types.ts index 85755119..b9f0adea 100644 --- a/src/private_types.ts +++ b/src/private_types.ts @@ -1,9 +1,54 @@ -import type { WebpackInjectManifestOptions } from "workbox-build"; +import type { + WebpackGenerateSWOptions, + WebpackInjectManifestOptions, +} from "workbox-build"; import type { GenerateSWConfig } from "workbox-webpack-plugin"; +type Impossible = { [P in K]?: never }; + +type GenerateSWOverrideJSDoc = { + /** + * Note: This plugin changes the default to `true`. + * + * @default true ("next-pwa") + */ + skipWaiting?: GenerateSWConfig["skipWaiting"]; + /** + * Note: This plugin changes the default to `true`. + * + * @default true ("next-pwa") + */ + clientsClaim?: GenerateSWConfig["clientsClaim"]; + /** + * Note: This plugin changes the default to `true`. + * + * @default true ("next-pwa") + */ + cleanUpOutdatedCaches?: GenerateSWConfig["cleanupOutdatedCaches"]; + /** Note: This plugin changes the default to `[]`. */ + ignoreURLParametersMatching?: GenerateSWConfig["ignoreURLParametersMatching"]; +}; + export type WorkboxTypes = { - GenerateSW: GenerateSWConfig & { - swSrc?: undefined; - }; - InjectManifest: WebpackInjectManifestOptions; + GenerateSW: Impossible< + Exclude< + keyof WebpackInjectManifestOptions, + Extract< + keyof WebpackGenerateSWOptions, + keyof WebpackInjectManifestOptions + > + > + > & + GenerateSWConfig & + GenerateSWOverrideJSDoc; + InjectManifest: Impossible< + Exclude< + keyof WebpackGenerateSWOptions, + Extract< + keyof WebpackInjectManifestOptions, + keyof WebpackGenerateSWOptions + > + > + > & + WebpackInjectManifestOptions; }; diff --git a/src/types.ts b/src/types.ts index 3d49d0c1..4de9f583 100644 --- a/src/types.ts +++ b/src/types.ts @@ -134,7 +134,7 @@ export interface PluginOptions { * @deprecated Use `basePath` in `next.config.js` instead. */ subdomainPrefix?: string; - /** Pass options to workbox-webpack-plugin */ + /** Pass options to `workbox-webpack-plugin` */ workboxOptions?: WorkboxTypes[keyof WorkboxTypes]; }