Skip to content

Commit

Permalink
feat: add new option to include kit version.json file to sw precache (#…
Browse files Browse the repository at this point in the history
…75)

* feat: add new option to include kit version.json file to sw precache

* chore: update jsdocs
  • Loading branch information
userquin authored Dec 7, 2023
1 parent 4ca54e0 commit de782fd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/sveltekit-ts/client-test/sw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ test('The service worker is registered and cache storage is present', async ({ p
// dontCacheBustURLsMatching: any asset in _app/immutable folder shouldn't have a revision (?__WB_REVISION__=)
expect(urls.some(url => url.startsWith('_app/immutable/') && url.endsWith('.css'))).toEqual(true)
expect(urls.some(url => url.startsWith('_app/immutable/') && url.endsWith('.js'))).toEqual(true)
// expect(urls.some(url => url.includes('_app/version.json?__WB_REVISION__='))).toEqual(true)
expect(urls.some(url => url.includes('_app/version.json?__WB_REVISION__='))).toEqual(true)
});
2 changes: 2 additions & 0 deletions examples/sveltekit-ts/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { defineConfig, devices } from '@playwright/test'

const url = 'http://localhost:4173'

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { nodeAdapter } from './adapter.mjs'

/**
Expand Down
4 changes: 3 additions & 1 deletion examples/sveltekit-ts/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ const config: UserConfig = {
navigateFallback: '/',
},
// if you have shared info in svelte config file put in a separate module and use it also here
kit: {}
kit: {
includeVersionFile: true,
}
}
)
]
Expand Down
26 changes: 12 additions & 14 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ export function configureSvelteKitOptions(
if (!config.globDirectory)
config.globDirectory = `${outDir}/output`

if (!config.modifyURLPrefix)
let buildAssetsDir = kit.appDir ?? '_app/'
if (buildAssetsDir[0] === '/')
buildAssetsDir = buildAssetsDir.slice(1)
if (buildAssetsDir[buildAssetsDir.length - 1] !== '/')
buildAssetsDir += '/'

if (!config.modifyURLPrefix) {
config.globPatterns = buildGlobPatterns(config.globPatterns)
if (kit.includeVersionFile)
config.globPatterns.push(`client/${buildAssetsDir}version.json`)
}

// exclude server assets: sw is built on SSR build
config.globIgnores = buildGlobIgnores(config.globIgnores)

// Vite 5 support: allow override dontCacheBustURLsMatching
if (!('dontCacheBustURLsMatching' in config)) {
let buildAssetsDir = kit.appDir ?? '_app/'
if (buildAssetsDir[0] === '/')
buildAssetsDir = buildAssetsDir.slice(1)
if (buildAssetsDir[buildAssetsDir.length - 1] !== '/')
buildAssetsDir += '/'

buildAssetsDir += 'immutable/'

config.dontCacheBustURLsMatching = new RegExp(buildAssetsDir)
}
if (!('dontCacheBustURLsMatching' in config))
config.dontCacheBustURLsMatching = new RegExp(`${buildAssetsDir}immutable/`)

if (!config.manifestTransforms) {
config.manifestTransforms = [createManifestTransform(
Expand Down Expand Up @@ -105,10 +105,8 @@ function createManifestTransform(base: string, webManifestName?: string, options
// fallback page in `.svelte-kit/output/prerendered` folder (fallback.html is the default).
if (url.startsWith('client/'))
url = url.slice(7)

else if (url.startsWith('prerendered/pages/'))
url = url.slice(18)

else if (url === defaultAdapterFallback)
url = adapterFallback!

Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export interface KitOptions {
* @see https://kit.svelte.dev/docs/configuration#appdir
*/
appDir?: string

/**
* Include `${appDir}/version.json` in the service worker precache manifest?
*
* @default false
*/
includeVersionFile?: boolean
}

export interface SvelteKitPWAOptions extends Partial<VitePWAOptions> {
Expand Down

0 comments on commit de782fd

Please sign in to comment.