Home

App Component 1 Coding Tips 4 CSS Tips 4 Data Server 2 Layout 1 Project Setup 6
Vanilla WebDev
Coding Tips 4 Project Setup 6 Starter Code 6
February
17
2025

Offline-First Experience

@lvc Project Setup
Edit this post

Initial setup of offline-first experience for vanilla JS web app.

Registering A Service Worker

Service worker is reponsible for handling web requests when the user is offline. Service worker can only be registered within the same domain.

Files:

.
├── index.html
└── sw.js

sw.js

self.addEventListener('message', function (e) {
	if (e.data.action == 'skipWaiting') {
		self.skipWaiting();
	}
});

self.addEventListener('fetch', function (e) {
	e.respondWith(
		caches.match(e.request).then(function (resp) {
			if (resp) return resp;

			return fetch(e.request)
				.then(function (r) {
					return r;
				})
				.catch(function () {
					console.error('Check connection.');
				});
		})
	);
});

index.html


    <script>
        // regiter service worker
        if (typeof (navigator) !== 'undefined' && 'serviceWorker' in navigator) {
            navigator.serviceWorker.register('/sw.js').catch(function (e) {
            console.error(e);
            });
        }
    </script>

</body>
</html>

Files Caching Component

Next, you'll need a file caching component. Visit the following post for an example on how to build one:

  • File Caching Component


***


Part of the usual setup:

  • Make Your Web App Installable



Open comments page
Newer posts
Older posts
Home
Coding Tips 4 Project Setup 6 Starter Code 6

Blogs

  • XML Expr / Blogger Development
  • tmpcafe / Guitar Tab

Pages

  • YouTube
  • About
  • Contact

Reading List

https://jackwhiting.co.uk/posts/lazy-loading-vanilla-js-with-webpack-laravel-mix https://medium.com/free-code-camp/reducing-css-bundle-size-70-by-cutting-the-class-names-and-using-scope-isolation-625440de600b

Libraries

https://github.com/verlok/vanilla-lazyload

Developer Resources

https://webpack.js.org/configuration/output/ https://web.dev/articles/preload-critical-assets

Reading List

  • How to name a webpack chunk (including split and common chunks)

Featured Post

  • Home

Blogs

  • XML Expr / Blogger Development
  • tmpcafe / Guitar Tab

Pages

  • YouTube
  • About
  • Contact

External Stylesheets

JS: Mobile m=1 Link Fix