Serving local files if user is offline

216 Views Asked by At

I was wondering is there a better way to serve a user with locally hosted assets if they are detected to be offline.

They wouldn't actually be offline, but due to corporate proxies sometimes the links to sites are blocked or just denied.

I have about 10 assets delivered via CDN, but it seems bad practice to write it as so:

<script>
var isOnline = window.navigator.onLine;

if( isOnline ) {
    document.write('<script src="http://cdn.site.ltd/file1.js"></script>');
    document.write('<script src="http://cdn.site.ltd/file2.js"></script>');
    document.write('<script src="http://cdn.site.ltd/file3.js"></script>');
   document.write('<link href="http://cdn.site.ltd/file.css"></link>');
} else {
   document.write('<script src="../file1.js"></script>');
   document.write('<script src="../file2.js"></script>');
   document.write('<script src="../file3.js"></script>');
   document.write('<link href="../file.css"></link>');
}
</script>

I mean apart from the user having Javascript disabled (not the default) is this the wrong way about doing this?

I also found this but I dont know if its overkill for the situation or not?

1

There are 1 best solutions below

1
On

This would be the ideal use case for a ServiceWorker, wouldn't it?