Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): return pwa info when required #378

Merged
merged 3 commits into from Sep 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/api.ts
Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -106,6 +110,7 @@ export function createAPI(ctx: PWAPluginContext): VitePluginPWAAPI {
}

return {
// hint when required
shouldRegisterSW,
inline: options.injectRegister === 'inline',
scope: options.scope,
Expand Down