diff --git a/docs/examples/svelte.md b/docs/examples/svelte.md index be0d885c..187e30f2 100644 --- a/docs/examples/svelte.md +++ b/docs/examples/svelte.md @@ -8,26 +8,6 @@ The `Svelte` example project can be found on `examples/svelte-routify` package/d The router used on this example project is [@roxi/routify](https://routify.dev/). -The `Svelte` example has been created using `create-vite` template with `pnpx`: - -::: details pnpx create-vite -```shell -pnpx create-vite -+ create-vite 2.5.4 -√ Project name: ... svelte-routify -√ Select a framework: » svelte -√ Select a variant: » svelte-ts - -Scaffolding project in examples\svelte-routify... - -Done. Now run: - - cd svelte-routify - npm install - npm run dev -``` -::: - To test `new content available`, you should rerun the corresponding script, and then refresh the page. If you are running an example with `Periodic SW updates`, you will need to wait 1 minute: diff --git a/docs/examples/sveltekit.md b/docs/examples/sveltekit.md index f6c40c73..a3ae17de 100644 --- a/docs/examples/sveltekit.md +++ b/docs/examples/sveltekit.md @@ -6,45 +6,6 @@ title: SvelteKit | Examples The `SvelteKit` example project can be found on `examples/sveltekit-pwa` package/directory and it is configured with `@sveltejs/adapter-static` adapter. -The `SvelteKit` example has been created using `svelte@next` template with `pnpm`: - -::: details pnpm create svelte@next sveltekit-pwa -```shell -pnpm create svelte@next sveltekit-pwa -+ create-svelte 2.0.0-next.89 - -Progress: resolved 5, reused 5, downloaded 0, added 5, done - -create-svelte version 2.0.0-next.89 - -Welcome to SvelteKit! - -This is beta software; expect bugs and missing features. - -Problems? Open an issue on https://github.com/sveltejs/kit/issues if none exists already. - -√ Which Svelte app template? » Skeleton project -√ Use TypeScript? ... No / Yes -√ Add ESLint for code linting? ... No / Yes -√ Add Prettier for code formatting? ... No / Yes - -Your project is ready! -✔ Typescript - Inside Svelte components, use {#if (!dev && browser)} - + {/if}
- -{#if ReloadPrompt} - -{/if} ``` ::: -## SvelteKit Adapters - -The main problem with the current implementation of the service worker module of `SvelteKit` is that you don't have access to the result applied by any adapter you have configured on your application. The service worker module of `SvelteKit` will be called before the adapter logic is applied, and so, inside the service worker module, you don't have access to those resources. Your application will not work when the user is offline, since the pages will not be included on the service worker precache manifest. - -When using `Vite PWA Plugin` with any `SvelteKit Adapter` you need to provide an additional script to rebuild your `pwa` once `SvelteKit` finish building your application, that is, when the adapter configured finish its job. - -The biggest difference between this plugin and the SvelteKit service worker module is that this plugin does not require integration into the application logic - just configuration. You can take a look at [SvelteKit example](https://github.com/antfu/vite-plugin-pwa/tree/main/examples/sveltekit-pwa) to configure the additional scripts on your application, it is quite complex since we use it for multiple behaviors with the same codebase. +### Prompt for update -### Workbox manifestTransforms +Since SvelteKit uses SSR/SSG, we need to add the `ReloadPrompt` component using a dynamic `import`. `vite-plugin-pwa` plugin will only register the service worker on build (check the [Development section](/guide/development)), it is aligned with the current behavior of [SvelteKit service worker module](https://kit.svelte.dev/docs#modules-$service-worker). -We must provide a list of URLs for the service worker to load and precache. We provide these to workbox using the [the `manifestTransforms` option](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#.ManifestTransform) under `workbox` or [`injectManifest`](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#.injectManifest). The [manifest entries](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#.ManifestEntry) provided via this option will contain all the assets specified in the `srcDir` option. +The best place to include the `ReloadPrompt` component will be in main layout of the application (you should register it in any layout): -Since `SvelteKit` outputs an `.html` page for each pre-rendered page, you can use `manifestTransforms` to generate the URL from the prerendered HTML file path. For an example, see the `pwa-configuration.js` module in the next example using `@sveltejs/adapter-static`. - -Pages which are not prerendered or are generated with a unique adapter will need to be handled separately and the `manifestTransforms` logic will need to be modified accordingly. - -### Static Adapter example - -As an example, when using [@sveltejs/adapter-static](https://github.com/sveltejs/kit/tree/master/packages/adapter-static) with `generateSW` strategy and `Prompt for update` behavior, you will need: - -::: details 1) add pwa.js script -```js -import { copyFileSync } from 'fs' -import { resolveConfig } from 'vite' -import { VitePWA } from 'vite-plugin-pwa' -import { pwaConfiguration } from './pwa-configuration.js' +::: details src/routes/__layout.svelte +```html + -const swDestinations = [ - './build/', -] + + {#if (!dev && browser)} + + {/if} + -const buildPwa = async () => { - const config = await resolveConfig({ plugins: [VitePWA({ ...pwaConfiguration })] }, 'build', 'production') - // when `vite-plugin-pwa` is present, use it to regenerate SW after rendering - const pwaPlugin = config.plugins.find(i => i.name === 'vite-plugin-pwa')?.api - if (pwaPlugin?.generateSW) { - console.log('Generating PWA...') - await pwaPlugin.generateSW() - webmanifestDestinations.forEach((d) => { - copyFileSync('./.svelte-kit/output/client/_app/manifest.webmanifest', `${d}/manifest.webmanifest`) - }) - // don't copy workbox, SvelteKit will copy it - swDestinations.forEach((d) => { - copyFileSync('./.svelte-kit/output/client/sw.js', `${d}/sw.js`) - }) - console.log('Generation of PWA complete') - } -} +
+ +
-buildPwa() +{#if ReloadPrompt} + +{/if} ``` ::: -::: details 2) add pwa-configuration.js script - -```js -const pwaConfiguration = { - srcDir: './build', - outDir: './.svelte-kit/output/client', - includeManifestIcons: false, - base: '/', - scope: '/', - manifest: { - short_name: '', - name: '', - scope: '/', - start_url: '/', - display: 'standalone', - theme_color: '#ffffff', - background_color: '#ffffff', - icons: [ - { - src: '/pwa-192x192.png', - sizes: '192x192', - type: 'image/png' - }, - { - src: '/pwa-512x512.png', - sizes: '512x512', - type: 'image/png' - }, - { - src: '/pwa-512x512.png', - sizes: '512x512', - type: 'image/png', - purpose: 'any maskable' - } - ] - }, - workbox: { - // mode: 'development', - navigateFallback: '/', - // vite and SvelteKit are not aligned: pwa plugin will use /\.[a-f0-9]{8}\./ by default: #164 optimize workbox work - dontCacheBustURLsMatching: /-[a-f0-9]{8}\./, - globDirectory: './build/', - globPatterns: ['robots.txt', '**/*.{js,css,html,ico,png,svg,webmanifest}'], - globIgnores: ['**/sw*', '**/workbox-*'], - manifestTransforms: [async (entries) => { - // manifest.webmanifest is added always by pwa plugin, so we remove it. - // EXCLUDE from the sw precache sw and workbox-* - const manifest = entries.filter(({ url }) => - url !== 'manifest.webmanifest' && url !== 'sw.js' && !url.startsWith('workbox-') - ).map((e) => { - let url = e.url - if (url && url.endsWith('.html')) { - if (url.startsWith('/')) - url = url.slice(1) - - if (url === 'index.html') - e.url = '/' - else if (url.endsWith('index.html')) - e.url = `/${url.substring(0, url.lastIndexOf('/'))}` - else if (url.endsWith('.html')) - e.url = `/${url.substring(0, url.length - '.html'.length)}` - - } - - return e - }) - - return { manifest } - }] - } -} +::: details $lib/ReloadPrompt.svelte +```html + -export { pwaConfiguration } -``` -::: +{#if toast} + +{/if} -::: details 3) modify your build script -```json -{ - "scripts": { - "build": "svelte-kit build && node ./pwa.js" - } -} + ``` ::: -::: details 4) add Vite Plugin PWA to svelte.config.js -```js -import adapter from '@sveltejs/adapter-static' -import preprocess from 'svelte-preprocess' -import { VitePWA } from 'vite-plugin-pwa' -import { pwaConfiguration } from './pwa-configuration.js' - -/** @type {import('@sveltejs/kit').Config} */ -const config = { - // Consult https://github.com/sveltejs/svelte-preprocess - // for more information about preprocessors - preprocess: preprocess(), - - kit: { - adapter: adapter(), - - // hydrate the
element in src/app.html - target: '#svelte', - vite: { - plugins: [VitePWA(pwaConfiguration)] - } - } -} - -export default config -``` -::: diff --git a/docs/guide/index.md b/docs/guide/index.md index 872e64a2..6be33c43 100644 --- a/docs/guide/index.md +++ b/docs/guide/index.md @@ -91,7 +91,7 @@ If you want to check it in `dev`, add the `devOptions` option to the plugin conf import { VitePWA } from 'vite-plugin-pwa' export default defineConfig({ plugins: [ - VitePWA({ + VitePWA({ registerType: 'autoUpdate', devOptions: { enabled: true diff --git a/examples/svelte-routify/package.json b/examples/svelte-routify/package.json index 1527aa54..acb380d5 100644 --- a/examples/svelte-routify/package.json +++ b/examples/svelte-routify/package.json @@ -46,18 +46,18 @@ "devDependencies": { "@rollup/plugin-replace": "^4.0.0", "@roxi/routify": "^2.18.8", - "@sveltejs/vite-plugin-svelte": "^1.0.0-next.49", + "@sveltejs/vite-plugin-svelte": "^1.0.1", "@tsconfig/svelte": "^3.0.0", "cross-env": "^7.0.3", "eslint": "^8.19.0", "eslint-plugin-svelte3": "^4.0.0", "https-localhost": "^4.7.1", "rimraf": "^3.0.2", - "svelte": "^3.48.0", + "svelte": "^3.49.0", "svelte-check": "^2.8.0", "svelte-preprocess": "^4.10.7", "typescript": "^4.7.4", - "vite": "^2.9.13", + "vite": "^3.0.2", "vite-plugin-pwa": "workspace:*", "workbox-core": "^6.5.3", "workbox-precaching": "^6.5.3", diff --git a/examples/sveltekit-pwa/.eslintrc.cjs b/examples/sveltekit-pwa/.eslintrc.cjs index 6ad4bdd7..13b64140 100644 --- a/examples/sveltekit-pwa/.eslintrc.cjs +++ b/examples/sveltekit-pwa/.eslintrc.cjs @@ -10,7 +10,7 @@ module.exports = { }, parserOptions: { sourceType: 'module', - ecmaVersion: 2019 + ecmaVersion: 2020 }, env: { browser: true, diff --git a/examples/sveltekit-pwa/package.json b/examples/sveltekit-pwa/package.json index 5001d153..2cb82619 100644 --- a/examples/sveltekit-pwa/package.json +++ b/examples/sveltekit-pwa/package.json @@ -3,13 +3,14 @@ "version": "0.0.0", "private": true, "scripts": { - "dev": "rimraf dev-dist && cross-env DEBUG=vite-plugin-pwa:* SW_DEV=true svelte-kit dev", - "dev-claims": "rimraf dev-dist && cross-env DEBUG=vite-plugin-pwa:* SW_DEV=true CLAIMS=true SW=true svelte-kit dev", - "dev-destroy": "rimraf dev-dist && cross-env DEBUG=vite-plugin-pwa:* SW_DESTROY=true SW_DEV=true svelte-kit dev", - "dev-claims-destroy": "rimraf dev-dist && cross-env DEBUG=vite-plugin-pwa:* SW_DESTROY=true SW_DEV=true CLAIMS=true SW=true svelte-kit dev", - "build": "svelte-kit build", + "dev": "rimraf dev-dist && cross-env DEBUG=vite-plugin-pwa:* SW_DEV=true vite dev", + "dev-claims": "rimraf dev-dist && cross-env DEBUG=vite-plugin-pwa:* SW_DEV=true CLAIMS=true SW=true vite dev", + "dev-destroy": "rimraf dev-dist && cross-env DEBUG=vite-plugin-pwa:* SW_DESTROY=true SW_DEV=true vite dev", + "dev-claims-destroy": "rimraf dev-dist && cross-env DEBUG=vite-plugin-pwa:* SW_DESTROY=true SW_DEV=true CLAIMS=true SW=true vite dev", + "build": "vite build", "package": "svelte-kit package", - "preview": "svelte-kit preview", + "preview": "vite preview", + "https": "serve build", "check": "svelte-check --tsconfig ./tsconfig.json", "check:watch": "svelte-check --tsconfig ./tsconfig.json --watch", "lint": "eslint --ignore-path .gitignore .", @@ -21,14 +22,14 @@ "start-sw-reloadsw": "npm run run-build-sw-reloadsw && npm run preview", "start-sw-claims": "npm run run-build-sw-claims && npm run preview", "start-sw-claims-reloadsw": "npm run run-build-sw-claims-reloadsw && npm run preview", - "run-build": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true", - "run-build-reloadsw": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true RELOAD_SW=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --RELOAD_SW=true", - "run-build-claims": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true CLAIMS=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --CLAIMS=true", - "run-build-reloadsw-claims": "routify -b && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true CLAIMS=true RELOAD_SW=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --CLAIMS=true --RELOAD_SW=true", - "run-build-sw": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --SW=true", - "run-build-sw-reloadsw": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true RELOAD_SW=true SW=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --RELOAD_SW=true --SW=true", - "run-build-sw-claims": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true CLAIMS=true SW=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --CLAIMS=true --SW=true", - "run-build-sw-claims-reloadsw": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true CLAIMS=true RELOAD_SW=true SW=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --CLAIMS=true --RELOAD_SW=true --SW=true", + "run-build": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true vite build", + "run-build-reloadsw": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true RELOAD_SW=true vite build", + "run-build-claims": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true CLAIMS=true vite build", + "run-build-reloadsw-claims": "routify -b && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true CLAIMS=true RELOAD_SW=true vite build", + "run-build-sw": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW=true vite build", + "run-build-sw-reloadsw": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true RELOAD_SW=true SW=true vite build", + "run-build-sw-claims": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true CLAIMS=true SW=true vite build", + "run-build-sw-claims-reloadsw": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true CLAIMS=true RELOAD_SW=true SW=true vite build", "start-destroy": "npm run run-build-destroy && npm run preview", "start-reloadsw-destroy": "npm run run-build-reloadsw-destroy && npm run preview", "start-claims-destroy": "npm run run-build-claims-destroy && npm run preview", @@ -37,19 +38,19 @@ "start-sw-reloadsw-destroy": "npm run run-build-sw-reloadsw-destroy && npm run preview", "start-sw-claims-destroy": "npm run run-build-sw-claims-destroy && npm run preview", "start-sw-claims-reloadsw-destroy": "npm run run-build-sw-reloadsw-claims-destroy && npm run preview", - "run-build-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --SW_DESTROY=true", - "run-build-reloadsw-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true RELOAD_SW=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --SW_DESTROY=true --RELOAD_SW=true", - "run-build-claims-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true CLAIMS=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --SW_DESTROY=true --CLAIMS=true", - "run-build-reloadsw-claims-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true CLAIMS=true RELOAD_SW=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --SW_DESTROY=true --CLAIMS=true --RELOAD_SW=true", - "run-build-sw-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true SW=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --SW_DESTROY=true --SW=true", - "run-build-sw-reloadsw-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true RELOAD_SW=true SW=true svelte-kit build && node ./pwa.jss --BASE_URL=/ --SOURCE_MAP=true --SW_DESTROY=true --RELOAD_SW=true --SW=true", - "run-build-sw-claims-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true CLAIMS=true SW=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --SW_DESTROY=true --CLAIMS=true --SW=true", - "run-build-sw-reloadsw-claims-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true CLAIMS=true RELOAD_SW=true SW=true svelte-kit build && node ./pwa.js --BASE_URL=/ --SOURCE_MAP=true --SW_DESTROY=true --CLAIMS=true --RELOAD_SW=true --SW=true" + "run-build-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true vite build", + "run-build-reloadsw-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true RELOAD_SW=true vite build", + "run-build-claims-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true CLAIMS=true vite build", + "run-build-reloadsw-claims-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true CLAIMS=true RELOAD_SW=true vite build", + "run-build-sw-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true SW=true vite build", + "run-build-sw-reloadsw-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true RELOAD_SW=true SW=true vite build", + "run-build-sw-claims-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true CLAIMS=true SW=true vite build", + "run-build-sw-reloadsw-claims-destroy": "rimraf .svelte-kit && rimraf build && cross-env DEBUG=vite-plugin-pwa:* BASE_URL=/ SOURCE_MAP=true SW_DESTROY=true CLAIMS=true RELOAD_SW=true SW=true vite build" }, "devDependencies": { "@rollup/plugin-replace": "^4.0.0", "@sveltejs/adapter-static": "next", - "@sveltejs/kit": "next", + "@sveltejs/kit": "1.0.0-next.388", "@typescript-eslint/eslint-plugin": "^5.30.3", "@typescript-eslint/parser": "^5.30.3", "cross-env": "^7.0.3", @@ -58,11 +59,12 @@ "https-localhost": "^4.7.1", "minimist": "^1.2.6", "rimraf": "^3.0.2", - "svelte": "^3.48.0", + "svelte": "^3.49.0", "svelte-check": "^2.8.0", "svelte-preprocess": "^4.10.7", "tslib": "^2.4.0", "typescript": "^4.7.4", + "vite": "^3.0.2", "vite-plugin-pwa": "workspace:*", "workbox-core": "^6.5.3", "workbox-precaching": "^6.5.3", diff --git a/examples/sveltekit-pwa/pwa-configuration.js b/examples/sveltekit-pwa/pwa-configuration.js index 82d5f2d8..590524f6 100644 --- a/examples/sveltekit-pwa/pwa-configuration.js +++ b/examples/sveltekit-pwa/pwa-configuration.js @@ -1,18 +1,19 @@ +const mode = process.env.SOURCE_MAP === 'true' ? 'development': undefined + +/** @type {import('vite-plugin-pwa').VitePWAOptions} */ const pwaConfiguration = { - srcDir: './build', - outDir: './.svelte-kit/output/client', - mode: 'development', - includeManifestIcons: false, + mode, scope: '/', base: '/', + selfDestroying: process.env.SW_DESTROY === 'true', manifest: { - short_name: "PWA Router", - name: "PWA Router", - start_url: "/", - scope: "/", - display: "standalone", - theme_color: "#ffffff", - background_color: "#ffffff", + short_name: 'PWA Router', + name: 'PWA Router', + start_url: '/', + scope: '/', + display: 'standalone', + theme_color: '#ffffff', + background_color:' "#ffffff"', icons: [ { src: '/pwa-192x192.png', @@ -37,65 +38,27 @@ const pwaConfiguration = { /* when using generateSW the PWA plugin will switch to classic */ type: 'module', navigateFallback: '/', - webManifestUrl: '/_app/manifest.webmanifest' }, } const claims = process.env.CLAIMS === 'true' const reload = process.env.RELOAD_SW === 'true' const sw = process.env.SW === 'true' -const selfDestroying = process.env.SW_DESTROY === 'true' const replaceOptions = { __DATE__: new Date().toISOString(), __RELOAD_SW__: reload ? 'true' : 'false', __SW_DEV__: process.env.SW_DEV === 'true' ? 'true' : 'false', } -const workboxOrInjectManifestEntry = { - // vite and SvelteKit are not aligned: pwa plugin will use /\.[a-f0-9]{8}\./ by default: #164 optimize workbox work - dontCacheBustURLsMatching: /-[a-f0-9]{8}\./, - globDirectory: './build/', - globPatterns: ['**/*.{js,css,html,ico,png,svg,webmanifest}'], - globIgnores: sw ? (claims ? ['**/claims-sw*'] : ['**/prompt-sw*']) : ['**/sw*', '**/workbox-*'], - // Before generating the service worker, manifestTransforms entry will allow us to transform the resulting precache manifest. See the manifestTransforms docs for mode details. - manifestTransforms: [async(entries) => { - // manifest.webmanifest is added always by pwa plugin, so we remove it. - // EXCLUDE from the sw precache sw and workbox-* - const manifest = entries.filter(({ url }) => - url !== 'manifest.webmanifest' && !url.endsWith('sw.js') && !url.startsWith('workbox-') - ).map((e) => { - let url = e.url - if (url && url.endsWith('.html')) { - if (url.startsWith('/')) - url = url.slice(1) - - e.url = url === 'index.html' ? '/' : `/${url.substring(0, url.lastIndexOf('.'))}` - console.log(`${url} => ${e.url}`) - } - - return e - }) - return { manifest } - }] -} - if (sw) { pwaConfiguration.srcDir = 'src' pwaConfiguration.filename = claims ? 'claims-sw.ts' : 'prompt-sw.ts' pwaConfiguration.strategies = 'injectManifest' pwaConfiguration.manifest.name = 'PWA Inject Manifest' pwaConfiguration.manifest.short_name = 'PWA Inject' - pwaConfiguration.injectManifest = workboxOrInjectManifestEntry -} else { - workboxOrInjectManifestEntry.mode = 'development' - workboxOrInjectManifestEntry.navigateFallback = '/' - pwaConfiguration.workbox = workboxOrInjectManifestEntry } if (claims) pwaConfiguration.registerType = 'autoUpdate' -if (selfDestroying) - pwaConfiguration.selfDestroying = selfDestroying - export { pwaConfiguration, replaceOptions } diff --git a/examples/sveltekit-pwa/pwa.js b/examples/sveltekit-pwa/pwa.js deleted file mode 100644 index 7540ea32..00000000 --- a/examples/sveltekit-pwa/pwa.js +++ /dev/null @@ -1,57 +0,0 @@ -import { resolveConfig } from 'vite' -import replace from '@rollup/plugin-replace' -import { VitePWA } from 'vite-plugin-pwa' -import { copyFileSync } from 'fs' -import minimist from 'minimist' - -const args = minimist(process.argv.slice(2)) - -process.env.CLAIMS = `${args['CLAIMS'] === 'true'}` -process.env.RELOAD_SW = `${args['RELOAD_SW'] === 'true'}` -process.env.SW = `${args['SW'] === 'true'}` - -const webmanifestDestinations = [ - './.svelte-kit/output/client/', - './build/', -] - -const swDestinations = [ - './build/', -] - -const buildPwa = async() => { - const { pwaConfiguration, replaceOptions } = await import('./pwa-configuration.js') - const config = await resolveConfig({ - plugins: [ - VitePWA(pwaConfiguration), - replace(replaceOptions), - ] - }, - 'build', - 'production' - ) - // when `vite-plugin-pwa` is presented, use it to regenerate SW after rendering - const pwaPlugin = config.plugins.find(i => i.name === 'vite-plugin-pwa')?.api - if (pwaPlugin?.generateSW) { - console.log('Generating PWA...') - await pwaPlugin.generateSW() - webmanifestDestinations.forEach(d => { - copyFileSync('./.svelte-kit/output/client/_app/immutable/manifest.webmanifest', `${d}/manifest.webmanifest`) - }) - // don't copy workbox, svelte kit will copy it - if (pwaConfiguration.strategies === 'injectManifest') { - const destName = pwaConfiguration.registerType === 'autoUpdate' ? 'claims-sw.js' : 'prompt-sw.js' - const name = `./.svelte-kit/output/client/${destName}` - swDestinations.forEach(d => { - copyFileSync(name, `${d}/${destName}`) - }) - } else { - swDestinations.forEach(d => { - copyFileSync('./.svelte-kit/output/client/sw.js', `${d}/sw.js`) - }) - } - console.log('Generation of PWA complete') - } -} - -buildPwa() diff --git a/examples/sveltekit-pwa/src/app.html b/examples/sveltekit-pwa/src/app.html index 638c3f1e..8094c780 100644 --- a/examples/sveltekit-pwa/src/app.html +++ b/examples/sveltekit-pwa/src/app.html @@ -3,14 +3,14 @@ - - - + + + %sveltekit.head% -
%sveltekit.body%
+
%sveltekit.body%
diff --git a/examples/sveltekit-pwa/src/claims-sw.ts b/examples/sveltekit-pwa/src/claims-sw.ts index 6d70e782..a3bba35a 100644 --- a/examples/sveltekit-pwa/src/claims-sw.ts +++ b/examples/sveltekit-pwa/src/claims-sw.ts @@ -11,14 +11,14 @@ precacheAndRoute(self.__WB_MANIFEST) cleanupOutdatedCaches() -let denylist: undefined | RegExp[] +let allowlist: undefined | RegExp[] if (import.meta.env.DEV) - denylist = [/^\/_app\/manifest.webmanifest$/] + allowlist = [/^\/$/] // to allow work offline registerRoute(new NavigationRoute( createHandlerBoundToURL('/'), - { denylist }, + { allowlist }, )) self.skipWaiting() diff --git a/examples/sveltekit-pwa/src/prompt-sw.ts b/examples/sveltekit-pwa/src/prompt-sw.ts index 0a2e5daf..cfa3f25c 100644 --- a/examples/sveltekit-pwa/src/prompt-sw.ts +++ b/examples/sveltekit-pwa/src/prompt-sw.ts @@ -14,5 +14,12 @@ precacheAndRoute(self.__WB_MANIFEST) // clean old assets cleanupOutdatedCaches() +let allowlist: undefined | RegExp[] +if (import.meta.env.DEV) + allowlist = [/^\/$/] + // to allow work offline -registerRoute(new NavigationRoute(createHandlerBoundToURL('/'))) +registerRoute(new NavigationRoute( + createHandlerBoundToURL('/'), + { allowlist }, +)) diff --git a/examples/sveltekit-pwa/src/routes/__layout.svelte b/examples/sveltekit-pwa/src/routes/__layout.svelte index 4b341af9..404945ac 100644 --- a/examples/sveltekit-pwa/src/routes/__layout.svelte +++ b/examples/sveltekit-pwa/src/routes/__layout.svelte @@ -1,6 +1,7 @@