From 4927b1921e3db122d455069916201da7fac7bcc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Thu, 22 Sep 2022 11:07:43 +0200 Subject: [PATCH] fix(api): return pwa info when required (#378) * fix(api): webmanifest should be generated always * chore: check also manifest is present * chore: update to logic to only return when required --- src/api.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/api.ts b/src/api.ts index 4092f935..307b3a1e 100644 --- a/src/api.ts +++ b/src/api.ts @@ -63,7 +63,7 @@ export function createAPI(ctx: PWAPluginContext): VitePluginPWAAPI { }, webManifestData() { const options = ctx?.options - if (!options || options.disable || ctx.viteConfig.build.ssr || (ctx.devEnvironment && !ctx.options.devOptions.enabled)) + if (!options || options.disable || !options.manifest || (ctx.devEnvironment && !ctx.options.devOptions.enabled)) return undefined let url = options.manifestFilename @@ -85,14 +85,18 @@ export function createAPI(ctx: PWAPluginContext): VitePluginPWAAPI { } }, registerSWData() { + // we'll return the info only when it is required + // 1: exclude if not enabled const options = ctx?.options - if (!options || options.disable || ctx.viteConfig.build.ssr || (ctx.devEnvironment && !ctx.options.devOptions.enabled)) + if (!options || options.disable || (ctx.devEnvironment && !ctx.options.devOptions.enabled)) return undefined + // 2: if manual registration or using virtual const mode = options.injectRegister if (!mode || ctx.useImportRegister) return undefined + // 3: otherwise we always return the info let type: WorkerType = 'classic' let script: string | undefined let shouldRegisterSW = options.injectRegister === 'inline' || options.injectRegister === 'script' @@ -106,6 +110,7 @@ export function createAPI(ctx: PWAPluginContext): VitePluginPWAAPI { } return { + // hint when required shouldRegisterSW, inline: options.injectRegister === 'inline', scope: options.scope,