Skip to content

Commit

Permalink
fix: expose injectRegister in pwa info (#611)
Browse files Browse the repository at this point in the history
* fix: expose `injectRegister` in pwa info

* docs: update api mode jsdocs
  • Loading branch information
userquin committed Nov 25, 2023
1 parent 3ceab95 commit ceab94a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/vanilla-ts-dev-options/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"build-auto": "rimraf dev-dist && DEBUG=vite-plugin-pwa SW_DEV=true SW_INLINE=auto vite build --force",
"build-inline": "rimraf dev-dist && DEBUG=vite-plugin-pwa SW_DEV=true SW_INLINE=inline vite build --force",
"build-script": "rimraf dev-dist && DEBUG=vite-plugin-pwa SW_DEV=true SW_INLINE=script vite build --force",
"build-script-defer": "rimraf dev-dist && DEBUG=vite-plugin-pwa SW_DEV=true SW_INLINE=script-defer vite build --force",
"serve": "serve dist"
},
"devDependencies": {
Expand Down
8 changes: 7 additions & 1 deletion info.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ declare module 'virtual:pwa-info' {
*/
registerSW?: {
/**
* When this flag is `true` the service worker must be registered via inline script otherwise registered via script with src attribute `registerSW.js` .
* When this flag is `true` the service worker must be registered via inline script otherwise registered via script with src attribute `registerSW.js`.
*
* @deprecated From `v0.17.2` this flag is deprecated, use `mode` instead.
*/
inline: boolean
/**
* When this flag is `inline` the service worker must be registered via inline script otherwise registered via script with src attribute `registerSW.js`.
*/
mode: 'inline' | 'script' | 'script-defer'
/**
* The path for the inline script: will contain the service worker url.
*/
Expand Down
1 change: 1 addition & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export function createAPI(ctx: PWAPluginContext): VitePluginPWAAPI {
// hint when required
shouldRegisterSW,
inline: options.injectRegister === 'inline',
mode: mode === 'auto' ? 'script' : mode,
scope: options.scope,
inlinePath: `${base}${ctx.devEnvironment ? DEV_SW_NAME : options.filename}`,
registerPath: `${base}${FILE_SW_REGISTER}`,
Expand Down
5 changes: 3 additions & 2 deletions src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ export function generateWebManifest(options: ResolvedVitePWAOptions, dev: boolea
}

export function generateRegisterSW(options: ResolvedVitePWAOptions, dev: boolean) {
if (options.injectRegister === 'inline')
if (options.injectRegister === 'inline') {
return `<script id="vite-plugin-pwa:inline-sw">${generateSimpleSWRegister(options, dev)}</script>`
}
else if (options.injectRegister === 'script' || options.injectRegister === 'script-defer') {
const hasDefer = options.injectRegister === 'script-defer'
return `<script id="vite-plugin-pwa:register-sw" src="${dev ? options.base : options.buildBase}${FILE_SW_REGISTER}" ${hasDefer ? 'defer' : ''}></script>`
return `<script id="vite-plugin-pwa:register-sw" src="${dev ? options.base : options.buildBase}${FILE_SW_REGISTER}"${hasDefer ? ' defer' : ''}></script>`
}

return undefined
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ interface VirtualPwaInfo {
linkTag: string
}
registerSW?: {
/**
* @deprecated use `mode` instead
*/
inline: boolean
mode: 'inline' | 'script' | 'script-defer'
inlinePath: string
registerPath: string
scope: string
Expand Down Expand Up @@ -61,9 +65,10 @@ function generatePwaInfo(ctx: PWAPluginContext, api: VitePluginPWAAPI) {
if (registerSWData) {
const scriptTag = registerSWData.toScriptTag()
if (scriptTag) {
const { inline, inlinePath, registerPath, type, scope } = registerSWData
const { inline, mode, inlinePath, registerPath, type, scope } = registerSWData
entry.registerSW = {
inline,
mode,
inlinePath,
registerPath,
type,
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,15 @@ export interface WebManifestData {
export interface RegisterSWData {
shouldRegisterSW: boolean
/**
* When this flag is `true` the service worker must be registered via inline script otherwise registered via script with src attribute `registerSW.js` .
* When this flag is `true` the service worker must be registered via inline script otherwise registered via script with src attribute `registerSW.js`.
*
* @deprecated From `v0.17.2` this flag is deprecated, use `mode` instead.
*/
inline: boolean
/**
* When this flag is `inline` the service worker must be registered via inline script otherwise registered via script with src attribute `registerSW.js`.
*/
mode: 'inline' | 'script' | 'script-defer'
/**
* The path for the inline script: will contain the service worker url.
*/
Expand Down

0 comments on commit ceab94a

Please sign in to comment.