diff --git a/.eslintignore b/.eslintignore index 849ddff3..8a34d5d2 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ dist/ +*.svelte diff --git a/.eslintrc.json b/.eslintrc.json index d3ad37f4..adff7ddb 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,6 +1,16 @@ { - "extends": "@antfu/eslint-config", - "rules": { - "@typescript-eslint/no-unused-vars": "off" - } + "extends": "@antfu", + "overrides": [ + { + "files": [ + "**/*.md/*.*" + ], + "rules": { + "@typescript-eslint/no-this-alias": "off", + "n/handle-callback-err": "off", + "no-restricted-syntax": "off", + "no-labels": "off" + } + } + ] } diff --git a/docs/.vitepress/theme/components/GenerateSWCleanupOutdatedCaches.md b/docs/.vitepress/theme/components/GenerateSWCleanupOutdatedCaches.md index a911f0dd..8c97b10c 100644 --- a/docs/.vitepress/theme/components/GenerateSWCleanupOutdatedCaches.md +++ b/docs/.vitepress/theme/components/GenerateSWCleanupOutdatedCaches.md @@ -13,9 +13,9 @@ export default defineConfig({ plugins: [ VitePWA({ workbox: { - cleanupOutdatedCaches: false - } + cleanupOutdatedCaches: false + } }) - ] + ] }) ``` diff --git a/docs/.vitepress/theme/components/GenerateSWSourceMap.md b/docs/.vitepress/theme/components/GenerateSWSourceMap.md index c3bd2c3b..40331787 100644 --- a/docs/.vitepress/theme/components/GenerateSWSourceMap.md +++ b/docs/.vitepress/theme/components/GenerateSWSourceMap.md @@ -13,9 +13,9 @@ export default defineConfig({ plugins: [ VitePWA({ workbox: { - sourcemap: true - } + sourcemap: true + } }) - ] + ] }) ``` diff --git a/docs/.vitepress/theme/components/InjectManifestCleanupOutdatedCaches.md b/docs/.vitepress/theme/components/InjectManifestCleanupOutdatedCaches.md index 10becc95..01297e16 100644 --- a/docs/.vitepress/theme/components/InjectManifestCleanupOutdatedCaches.md +++ b/docs/.vitepress/theme/components/InjectManifestCleanupOutdatedCaches.md @@ -3,7 +3,7 @@ also the old ones. To delete old assets (from previous versions that are no long building your own service worker, you will need to add the following code to your custom service worker: ```js -import { precacheAndRoute, cleanupOutdatedCaches } from 'workbox-precaching' +import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching' cleanupOutdatedCaches() diff --git a/docs/.vitepress/theme/components/SsrSsg.md b/docs/.vitepress/theme/components/SsrSsg.md index e6cc7eca..de4b2939 100644 --- a/docs/.vitepress/theme/components/SsrSsg.md +++ b/docs/.vitepress/theme/components/SsrSsg.md @@ -6,15 +6,14 @@ You can register the service worker on `src/pwa.ts` module: ```ts import { registerSW } from 'virtual:pwa-register' -registerSW({ ... }) +registerSW({ /* ... */ }) ``` and then import it from your `main.ts`: ```ts -if (typeof window !== 'undefined') { +if (typeof window !== 'undefined') import('./pwa') -} ``` You can see the [FAQ](/guide/faq.md#navigator-window-is-undefined) entry for more info. diff --git a/docs/.vitepress/theme/config.ts b/docs/.vitepress/theme/config.ts index 677c20f9..7b5ec663 100644 --- a/docs/.vitepress/theme/config.ts +++ b/docs/.vitepress/theme/config.ts @@ -1,6 +1,3 @@ -/* eslint-disable no-use-before-define */ -/* eslint-disable @typescript-eslint/no-namespace */ - export namespace DefaultTheme { export interface Config { logo?: string diff --git a/docs/frameworks/preact.md b/docs/frameworks/preact.md index edfc2821..12206751 100644 --- a/docs/frameworks/preact.md +++ b/docs/frameworks/preact.md @@ -13,7 +13,7 @@ You can use the built-in `Vite` virtual module `virtual:pwa-register/preact` for ```ts declare module 'virtual:pwa-register/preact' { - // @ts-ignore ignore when preact/hooks is not installed + // @ts-expect-error ignore when preact/hooks is not installed import type { StateUpdater } from 'preact/hooks' export interface RegisterSWOptions { @@ -40,7 +40,6 @@ You can use this `ReloadPrompt.tsx` component: ReloadPrompt.tsx code ```tsx -// eslint-disable-next-line no-use-before-define import './ReloadPrompt.css' import { useRegisterSW } from 'virtual:pwa-register/preact' @@ -52,11 +51,11 @@ function ReloadPrompt() { updateServiceWorker, } = useRegisterSW({ onRegistered(r) { - // eslint-disable-next-line prefer-template - console.log('SW Registered: ' + r) + // eslint-disable-next-line prefer-template + console.log('SW Registered: ' + r) }, onRegisterError(error) { - console.log('SW registration error', error) + console.log('SW registration error', error) }, }) @@ -71,8 +70,8 @@ function ReloadPrompt() { &&
{ offlineReady - ? App ready to work offline - : New content available, click on reload button to update. + ? App ready to work offline + : New content available, click on reload button to update. }
{ needRefresh && } @@ -131,7 +130,7 @@ As explained in [Periodic Service Worker Updates](/guide/periodic-sw-updates.htm behavior on your application with the virtual module `virtual:pwa-register/preact`: ```ts -import { useRegisterSW } from 'virtual:pwa-register/preact'; +import { useRegisterSW } from 'virtual:pwa-register/preact' const intervalMS = 60 * 60 * 1000 diff --git a/docs/frameworks/react.md b/docs/frameworks/react.md index 12104f33..ffb78b2d 100644 --- a/docs/frameworks/react.md +++ b/docs/frameworks/react.md @@ -13,7 +13,7 @@ You can use the built-in `Vite` virtual module `virtual:pwa-register/react` for ```ts declare module 'virtual:pwa-register/react' { - // @ts-ignore ignore when react is not installed + // @ts-expect-error ignore when react is not installed import type { Dispatch, SetStateAction } from 'react' export interface RegisterSWOptions { @@ -40,7 +40,6 @@ You can use this `ReloadPrompt.tsx` component: ReloadPrompt.tsx code ```tsx -// eslint-disable-next-line no-use-before-define import React from 'react' import './ReloadPrompt.css' @@ -53,11 +52,11 @@ function ReloadPrompt() { updateServiceWorker, } = useRegisterSW({ onRegistered(r) { - // eslint-disable-next-line prefer-template - console.log('SW Registered: ' + r) + // eslint-disable-next-line prefer-template + console.log('SW Registered: ' + r) }, onRegisterError(error) { - console.log('SW registration error', error) + console.log('SW registration error', error) }, }) @@ -72,8 +71,8 @@ function ReloadPrompt() { &&
{ offlineReady - ? App ready to work offline - : New content available, click on reload button to update. + ? App ready to work offline + : New content available, click on reload button to update. }
{ needRefresh && } @@ -132,7 +131,7 @@ As explained in [Periodic Service Worker Updates](/guide/periodic-sw-updates.htm behavior on your application with the virtual module `virtual:pwa-register/react`: ```ts -import { useRegisterSW } from 'virtual:pwa-register/react'; +import { useRegisterSW } from 'virtual:pwa-register/react' const intervalMS = 60 * 60 * 1000 diff --git a/docs/frameworks/solidjs.md b/docs/frameworks/solidjs.md index caf51277..0880c3e7 100644 --- a/docs/frameworks/solidjs.md +++ b/docs/frameworks/solidjs.md @@ -13,7 +13,7 @@ You can use the built-in `Vite` virtual module `virtual:pwa-register/solid` for ```ts declare module 'virtual:pwa-register/solid' { - // @ts-ignore ignore when solid-js is not installed + // @ts-expect-error ignore when solid-js is not installed import type { Accessor, Setter } from 'solid-js' export interface RegisterSWOptions { @@ -40,24 +40,23 @@ You can use this `ReloadPrompt.tsx` component: ReloadPrompt.tsx code ```tsx -// eslint-disable-next-line no-use-before-define -import { Component, Show } from "solid-js"; -import styles from './ReloadPrompt.module.css' - +import type { Component } from 'solid-js' +import { Show } from 'solid-js' import { useRegisterSW } from 'virtual:pwa-register/solid' +import styles from './ReloadPrompt.module.css' -const ReloadPrompt: Component = () => { +const ReloadPrompt: Component = () => { const { offlineReady: [offlineReady, setOfflineReady], needRefresh: [needRefresh, setNeedRefresh], updateServiceWorker, } = useRegisterSW({ onRegistered(r) { - // eslint-disable-next-line prefer-template - console.log('SW Registered: ' + r) + // eslint-disable-next-line prefer-template + console.log('SW Registered: ' + r) }, onRegisterError(error) { - console.log('SW registration error', error) + console.log('SW registration error', error) }, }) @@ -67,10 +66,10 @@ const ReloadPrompt: Component = () => { } return ( -
+
-
-
+
+
New content available, click on reload button to update.} when={offlineReady()} @@ -79,9 +78,9 @@ const ReloadPrompt: Component = () => {
- + - +
@@ -136,7 +135,7 @@ As explained in [Periodic Service Worker Updates](/guide/periodic-sw-updates.htm behavior on your application with the virtual module `virtual:pwa-register/solid`: ```ts -import { useRegisterSW } from 'virtual:pwa-register/solid'; +import { useRegisterSW } from 'virtual:pwa-register/solid' const intervalMS = 60 * 60 * 1000 diff --git a/docs/frameworks/svelte.md b/docs/frameworks/svelte.md index 2b3a452e..2ce206a6 100644 --- a/docs/frameworks/svelte.md +++ b/docs/frameworks/svelte.md @@ -13,7 +13,7 @@ You can use the built-in `Vite` virtual module `virtual:pwa-register/svelte` for ```ts declare module 'virtual:pwa-register/svelte' { - // @ts-ignore ignore when svelte is not installed + // @ts-expect-error ignore when svelte is not installed import type { Writable } from 'svelte/store' export interface RegisterSWOptions { @@ -121,7 +121,7 @@ As explained in [Periodic Service Worker Updates](/guide/periodic-sw-updates.htm behavior on your application with the virtual module `virtual:pwa-register/svelte`: ```ts -import { useRegisterSW } from 'virtual:pwa-register/svelte'; +import { useRegisterSW } from 'virtual:pwa-register/svelte' const intervalMS = 60 * 60 * 1000 diff --git a/docs/frameworks/sveltekit.md b/docs/frameworks/sveltekit.md index 097c8315..3fe3868e 100644 --- a/docs/frameworks/sveltekit.md +++ b/docs/frameworks/sveltekit.md @@ -5,7 +5,6 @@ title: SvelteKit | Frameworks # SvelteKit > For `Type declarations`, `Prompt for update` and `Periodic SW Updates` go to [Svelte](/frameworks/svelte.html) entry. -> You should remove all references to [SvelteKit service worker module](https://kit.svelte.dev/docs#modules-$service-worker) to disable it on your application. @@ -18,19 +17,19 @@ The best place to include the `ReloadPrompt` is on the main layout of the applic ```html - {#if (!dev && browser)} - - {/if} + {#if (!dev && browser)} + + {/if}
@@ -38,7 +37,7 @@ The best place to include the `ReloadPrompt` is on the main layout of the applic
{#if ReloadPrompt} - + {/if} ``` @@ -67,37 +66,37 @@ As an example, when using [@sveltejs/adapter-static](https://github.com/sveltejs 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'; -import { copyFileSync } from 'fs'; +import { VitePWA } from 'vite-plugin-pwa' +import { pwaConfiguration } from './pwa-configuration.js' const webmanifestDestinations = [ - './.svelte-kit/output/client/', - './build/', + './.svelte-kit/output/client/', + './build/', ] const swDestinations = [ - './build/', + './build/', ] -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') - } -} +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() ``` @@ -109,75 +108,75 @@ buildPwa() ```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 }; - }] - } -}; - -export { 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 } + }] + } +} + +export { pwaConfiguration } ``` @@ -185,8 +184,10 @@ export { pwaConfiguration }; 3) modify your build script ```json -"scripts": { - "build": "svelte-kit build && node ./pwa.js" +{ + "scripts": { + "build": "svelte-kit build && node ./pwa.js" + } } ``` @@ -196,28 +197,28 @@ export { pwaConfiguration }; 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 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; + // 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/frameworks/vue.md b/docs/frameworks/vue.md index 142ea13b..57fc07d9 100644 --- a/docs/frameworks/vue.md +++ b/docs/frameworks/vue.md @@ -13,7 +13,7 @@ You can use the built-in `Vite` virtual module `virtual:pwa-register/vue` for `V ```ts declare module 'virtual:pwa-register/vue' { - // @ts-ignore ignore when vue is not installed + // @ts-expect-error ignore when vue is not installed import type { Ref } from 'vue' export interface RegisterSWOptions { @@ -49,7 +49,7 @@ const { updateServiceWorker, } = useRegisterSW() -const close = async() => { +const close = async () => { offlineReady.value = false needRefresh.value = false } @@ -57,9 +57,9 @@ const close = async() => {