diff --git a/src/service-worker.js b/src/firebase-messaging-sw.js similarity index 100% rename from src/service-worker.js rename to src/firebase-messaging-sw.js diff --git a/src/routes/__layout.svelte b/src/routes/__layout.svelte index c14aa257..c01d21e8 100644 --- a/src/routes/__layout.svelte +++ b/src/routes/__layout.svelte @@ -46,23 +46,6 @@ }); window.localforage = localforage; await checkLocalSave(); - - if ('serviceWorker' in navigator && import.meta.env.PROD) { - navigator.serviceWorker - .register('/service-worker.js', { - scope: 'firebase-cloud-messaging-push-scope', - }) - .then( - function (registration) { - console.log('Service worker registration succeeded'); - }, - function (error) { - console.log('Service worker registration failed:', error); - }, - ); - } else { - console.log('Service workers are not supported.'); - } }); $: segment = $page.url.pathname.substring(1).split('/')[0]; diff --git a/src/stores/firebase.js b/src/stores/firebase.js index ace3249d..c9772abb 100644 --- a/src/stores/firebase.js +++ b/src/stores/firebase.js @@ -75,10 +75,8 @@ async function getToken() { console.log('request token'); try { - const swRegistration = await navigator.serviceWorker.getRegistration('/firebase-cloud-messaging-push-scope'); const token = await messaging.getToken({ vapidKey: 'BA6niiIWa_QP2SXMTjS8gBtM3M7m0q0n0_ZWjECw3Z_iEFujzPG2VdAAvNFJ5btbgpEiRe2B80M4QKxRSxtmvDw', - serviceWorkerRegistration: swRegistration, }); if (token) { diff --git a/vite.config.js b/vite.config.js index 8122778f..deccedf4 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,11 +1,40 @@ +import { resolve } from 'path'; import { sveltekit } from '@sveltejs/kit/vite'; +import { build } from 'vite'; -/** @type {import('vite').UserConfig} */ -const config = { - plugins: [sveltekit()], - build: { - sourcemap: true, +/** @type {import('vite').Plugin} */ +const firebaseSwPlugin = { + name: 'firebaseplugin', + apply: 'build', + enforce: 'post', + buildEnd: async () => { + const conf = { + build: { + lib: { + entry: resolve(__dirname, 'src/firebase-messaging-sw.js'), + formats: ['es'], + }, + rollupOptions: { + output: { + entryFileNames: 'firebase-messaging-sw.js', + }, + }, + outDir: resolve(__dirname, '.svelte-kit/output/client'), + emptyOutDir: false, + }, + configFile: false, + }; + + await build(conf); }, }; +/** @type {import('vite').UserConfig} */ +const config = { + build: { + sourcemap: true, + }, + plugins: [sveltekit(), firebaseSwPlugin], +}; + export default config;