{TametheBots}

serviceWorker API

The Result:

Service Worker?

JavaScript Not Functioning

Testing if the serviceWorker API exisits, and if it can register a service worker.

There is a script at the bottom of the page, it attempts to register a service worker and updates the result p tag with the outcome.

            <script>
                window.addEventListener('load', function(loadEvent) {
                    if (window.navigator.serviceWorker) {
                        addRes('1 - window.navigator.serviceWorker TRUE');
                        navigator.serviceWorker.register('/sw.js').then(function(registration) {
                            addRes('2 - Service Worker Registered');
                            navigator.serviceWorker.controller.postMessage({
                                msg: 'getFact'
                            })
        
                        }, function(err) {
                            addRes('2 - Service Worker Register Fail');
                        })
                    } else {
                        addRes('1 - window.navigator.serviceWorker FALSE');
                    }
                })
        
                navigator.serviceWorker.addEventListener('message', event => {
                    console.log(event.data.fact);
                    addRes(`<i>Random Cat Fact fetched in Service Worker:</i><br />${event.data.fact}`);
                });
        
        
        
                function addRes(mess) {
                    let cur = document.getElementById("result").innerHTML;
                    if (cur === 'JavaScript Not Functioning') {
                        cur = '';
                    }
                    document.getElementById("result").innerHTML = cur + '<br />' + mess;
                }
            </script>

< Back