The post introduce a way to make your wordpress website has the ability to check network connection.
Install the plugin Insert Headers and Footers for wordpress.
Add the following code in header part.
<script>
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/serviceworker.js")
.then(function(registration) {
console.log("Service Worker registered with scope:", registration.scope);
}).catch(function(err) {
console.log("Service worker registration failed:", err);
});
}
</script>
Create the file serviceworker.js in the root path of website, add the following code for network connection check.
self.addEventListener("fetch", function(event) {
event.respondWith(
fetch(event.request).catch(function() {
return new Response(
"Welcome to the weiy.city.\n"+
"There seems to be a problem with your connection.\n"+
"We look forward to provide satisfactory services as soon as you go online."
);
})
);
});
Then our website can remind you to connect to the Internet if you get disconnected.