A service worker framework made specifically for SvelteKit
SvelteKit-Adapter-Versioned-Worker is a SvelteKit adapter that enables the generation of service workers for SvelteKit sites, allowing them to work offline. This adapter simplifies the process of dealing with caching headers, provides a reload opportunity and resumable state system for seamless updates, and supports various update priorities to avoid unnecessary disruptions for users. It also offers an easy-to-use hooks system for virtual routes and more, and supports different file modes, including stale-while-revalidate and “semi-lazy”.
Make sure you have the following peer dependencies installed:
Update the following dependencies (if you already have them installed):
Install the peer dependencies and the SvelteKit adapter with the following command:
npm install --save-dev @sveltejs/adapter-static@2.x.x typescript@5.x.x tslib@2.x.x
npm install --save-dev svelte@4.x.x vite@4.x.x
npm install --save @addaleax/versioned-worker-adapter
In your src/routes/+layout.ts (or .js) file, ensure that SvelteKit is configured to prerender all routes.
In your svelte.config.js file, import and use the versionedWorkerAdapter package as your SvelteKit adapter:
import { versionedWorkerAdapter } from '@addaleax/versioned-worker-adapter';
export default {
kit: {
adapter: versionedWorkerAdapter(),
// other SvelteKit config options...
},
};
Specify the location of the versionedWorker.json file for Versioned Worker to use.
import { GetLast } from '@addaleax/versioned-worker-adapter';
export function getLast(InfoProvider: GetLast): LastInfoProvider {
return new InfoProvider({
filename: 'path_to_build_directory/versionedWorker.json',
});
}
package.json file:{
"scripts": {
"getLast": "<command_to_download_versionedWorker_json>"
},
"sveltekit": {
"adapter": {
"getLast": "npm run getLast"
},
"preview": "npm run getLast && svelte-kit preview"
}
}
Make sure to replace <command_to_download_versionedWorker_json> with the actual command to download the versionedWorker.json file.
Install the required packages for production with the following command:
npm install --save-dev <command_to_install_required_packages>
Make sure to replace <command_to_install_required_packages> with the command to install the required packages.
Add the ServiceWorker component to your src/routes/+layout.svelte file so that it is used:
<script>
import { ServiceWorker } from '@addaleax/versioned-worker-adapter';
// other code...
</script>
<!-- rest of the layout code -->
<ServiceWorker />
SvelteKit-Adapter-Versioned-Worker is a useful adapter for SvelteKit sites that enables the generation of service workers, allowing for offline functionality. It simplifies the handling of caching headers, provides seamless updates with a reload opportunity and resumable state system, and supports various update priorities. With an easy-to-use hooks system, support for different file modes, and dynamic data prefetching, this adapter enhances the performance and offline experience of SvelteKit sites. However, it currently requires compatibility with @sveltejs/adapter-static.