Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering Service Worker in a different folder

I am trying to figure how to register properly service worker, in development all works fine, I call service worker:

if (navigator.serviceWorker) {
    navigator.serviceWorker.register('./sw.js').then(function(reg) {
        if (reg.waiting) {
            reg.waiting.postMessage({ action: 'skipWaiting' });
            return;
        }

        reg.addEventListener('updatefound', function() {
            trackInstalling(reg.installing);
        });

        var refreshing;
        navigator.serviceWorker.addEventListener('controllerchange', function() {
            if (refreshing) return;
            window.location.reload();
            refreshing = true;
        });

    });

}

Cache is listed like this:

var urlsToCache = [
    '/',
    '/index.html',
    '/css/app.min.css',
    '/js/app.min.js',
    '/js/library.min.js',
    '/views/line.html',
    '/views/start.html',
    '/views/station.html',
    '/views/timetable.html'
];

This is just a project I am doing for Udacity so the page won't have it's own domain. However I still want to keep the final version on-line in a folder /train/ but service worker is registered under the main domain so is caching wrong files.

If I add 'train/index.html'the service worker is looking for 'train/train/index.html'. Same happens if try to register service worker in a folder "train"...

How should I do it so the on-line version of the project works properly?

like image 373
TheFullResolution Avatar asked Jul 04 '16 18:07

TheFullResolution


People also ask

Do I need to register a service worker for each sub-domain?

Each sub-domain is considered a different origin, So, you will need to register a service worker for each one. Each of these workers will have its own cache and scope. Show activity on this post.

Where do I put a service worker file?

In the above code, a Service Worker is registered using the serviceworker.min.js file located at the root of the site. Make sure to place the Service Worker file in the highest-level directory that you want it to manage. Such a directory is called the scope of the Service Worker.

Why is my service worker not registering?

Why is my service worker failing to register? This could be for the following reasons: You are not running your application through HTTPS. The path to your service worker file is not written correctly — it needs to be written relative to the origin, not your app’s root directory.

Do I need Two service worker registrations for multiple pages?

If you have pages that live on both abc.123.com and xyz.123.com and you want both sets of pages to be controlled by a service worker, then you need to have two separate service worker registrations.


1 Answers

Since your service worker will now live in a subfolder of the domain, its cache paths should be relative:

var urlsToCache = [
    './',  // I'm not 100% sure on the syntax for this one. In your case
           // '/train/' would definitely work, but it would require updating
           // if you ever move the app to a different folder.

    'index.html', // All the other URLs just lose their preceding slash.
    'css/app.min.css',
    'js/app.min.js',
    'js/library.min.js',
    'views/line.html',
    'views/start.html',
    'views/station.html',
    'views/timetable.html'
];
like image 65
Noah Freitas Avatar answered Oct 24 '22 19:10

Noah Freitas