I am making a web application with offline capabilities using a service worker generated by a Nodejs plugin called sw-precache. Everything works fine and I do have access to the html files or images offline.
But then, since you have no server-side language possible, is there a way to rewrite url client-side like an .htaccess
file would do? Like showing a "404 Page not found" page when no file matches the url? I know that redirections are possible using Javascript or meta tags, but rewriting the url?
By default,
sw-precache
will only respond tofetch
events when the URL being requested is a URL for a resource that it has cached. If someone navigations to a URL for a non-existent web page, thensw-precache
won't respond to thefetch
event.That does mean that you have a chance to run your own code in an additional
fetch
event handler that could implement custom behavior, like returning a404.html
page when a user navigates to a non-existent page while offline. You need to jump through a couple of hoops, but here's how to do it:This shouldn't interfere with anything that
sw-precache
is doing, as it will just be used as fallback.