ACCESS YOUR SITE EVEN YOU ARE OFFLINE
Demo
Usage
Build your service worker
- Clone this repository
- Change base url in the vite.config.ts to your base url
- Run
npm run build
- Copy service-worker.js file at the dist/assets/scripts
Include script file to your app
copy service-worker.js file to the root of the app
offline.html is a required file
- another files
- service-worker.js
- index.html
- offline.html
Create a file js that declare the service worker above
register-sw.js
const registerServiceWorker = async () => {
if('serviceWorker' in navigator) {
try {
console.log("Service worker registering");
const registration = await navigator.serviceWorker.register(`service-worker.js`);
console.log("Service worker registered");
} catch(error) {
console.log(error);
}
} else {
console.log('SW is not supported');
}
}
registerServiceWorker();
Now, you can access your page offline. Have fun!