Skip to content

Commit

Permalink
fix!: unable to use custom service worker without precaching (#83)
Browse files Browse the repository at this point in the history
* fix!: unable to use custom service worker without precaching

* chore: use rename also inside injection point logic
  • Loading branch information
userquin committed Jul 4, 2024
1 parent 30c177e commit 0e52330
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@vite-pwa/sveltekit",
"type": "module",
"version": "0.5.0",
"packageManager": "pnpm@9.0.6",
"packageManager": "pnpm@9.4.0",
"description": "Zero-config PWA for SvelteKit",
"author": "antfu <anthonyfu117@hotmail.com>",
"license": "MIT",
Expand Down
63 changes: 38 additions & 25 deletions src/plugins/SvelteKitPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { lstat, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
import { lstat, mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import type { Plugin, ResolvedConfig } from 'vite'
import type { VitePWAOptions, VitePluginPWAAPI } from 'vite-plugin-pwa'
Expand Down Expand Up @@ -117,31 +117,44 @@ export function SvelteKitPlugin(
if (swName.endsWith('.ts'))
swName = swName.replace(/\.ts$/, '.js')

// kit fixes sw name to 'service-worker.js'
const injectManifestOptions: import('workbox-build').InjectManifestOptions = {
globDirectory: outDir.replace(/\\/g, '/'),
...options.injectManifest ?? {},
swSrc: join(clientOutputDir, 'service-worker.js').replace(/\\/g, '/'),
swDest: join(clientOutputDir, 'service-worker.js').replace(/\\/g, '/'),
}
const injectionPoint = !options.injectManifest || !('injectionPoint' in options.injectManifest) || !!options.injectManifest.injectionPoint

const [injectManifest, logWorkboxResult] = await Promise.all([
import('workbox-build').then(m => m.injectManifest),
import('./log').then(m => m.logWorkboxResult),
])

// inject the manifest
const buildResult = await injectManifest(injectManifestOptions)
// log workbox result
logWorkboxResult('injectManifest', buildResult, viteConfig)
// rename the sw
if (swName !== 'service-worker.js') {
await writeFile(
join(clientOutputDir, swName).replace('\\/g', '/'),
await readFile(injectManifestOptions.swSrc, 'utf-8'),
'utf-8',
)
await rm(injectManifestOptions.swDest)
if (injectionPoint) {
// kit fixes sw name to 'service-worker.js'
const injectManifestOptions: import('workbox-build').InjectManifestOptions = {
globDirectory: outDir.replace(/\\/g, '/'),
...options.injectManifest ?? {},
swSrc: join(clientOutputDir, 'service-worker.js').replace(/\\/g, '/'),
swDest: join(clientOutputDir, 'service-worker.js').replace(/\\/g, '/'),
}

const [injectManifest, logWorkboxResult] = await Promise.all([
import('workbox-build').then(m => m.injectManifest),
import('./log').then(m => m.logWorkboxResult),
])

// inject the manifest
const buildResult = await injectManifest(injectManifestOptions)
// log workbox result
logWorkboxResult('injectManifest', viteConfig, buildResult)
// rename the sw
if (swName !== 'service-worker.js') {
await rename(
join(clientOutputDir, 'service-worker.js').replace('\\/g', '/'),
join(clientOutputDir, swName).replace('\\/g', '/'),
)
}
}
else {
const { logWorkboxResult } = await import('./log')
// log workbox result
logWorkboxResult('injectManifest', viteConfig)
if (swName !== 'service-worker.js') {
await rename(
join(clientOutputDir, 'service-worker.js').replace('\\/g', '/'),
join(clientOutputDir, swName).replace('\\/g', '/'),
)
}
}
}
},
Expand Down
11 changes: 10 additions & 1 deletion src/plugins/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import type { ResolvedConfig } from 'vite'
import { cyan, dim, green, magenta, yellow } from 'kolorist'
import { version } from '../../package.json'

export function logWorkboxResult(strategy: string, buildResult: BuildResult, viteOptions: ResolvedConfig) {
export function logWorkboxResult(strategy: string, viteOptions: ResolvedConfig, buildResult?: BuildResult) {
const { root, logLevel = 'info' } = viteOptions

if (logLevel === 'silent')
return

if (!buildResult) {
console.info([
'',
`${cyan(`SvelteKit VitePWA v${version}`)}`,
`mode ${magenta(strategy)}`,
].join('\n'))
return
}

const { count, size, filePaths, warnings } = buildResult

if (logLevel === 'info') {
Expand Down

0 comments on commit 0e52330

Please sign in to comment.