The JavaScript service worker placed on my website doesn't work in the MIT App Inventor WebViewer

I turned my website into an app using WebViewer. I wanted it to be able to work offline, for this I wrote a service worker script for my site, here is its code:

// service-worker.js

// Визначаємо назву кешу
var cacheName = 'offline-cache';

// Вказуємо ресурси, які ми хочемо зберегти в кеші
var resourcesToCache = [
  '/',
  '/index.php',
  'images/goodes/18650-holder.png',
  'images/goodes/display.png',
  'images/goodes/esp32.png',
  'images/goodes/led-strip.png',
  'images/goodes/mt3608.png',
  'images/goodes/resistor.png',
  'images/goodes/switch.png',
  'images/goodes/tp4056.png',
  'images/goodes/ttp223.png',
  'images/maps/0.png',
  'images/maps/1.png',
  'images/maps/2.jpg',
  'images/maps/3.png',
  'images/maps/4.png',
  'images/maps/5.1.png',
  'images/maps/5.2.png',
  'images/maps/6.png',
  'images/maps/7.png',
  'images/maps/8.jpg',
  'images/led-scheme.jpg',
  'images/scheme.png',
  'scripts/resize-video.js',
  'scripts/slides.js',
  'styles/base-style.css',
  'styles/changelog-style.css',
  'styles/components-style.css',
  'styles/make-style.css',
  'styles/map-body-style.css',
  'styles/start-style.css'
];

// Встановлюємо Service Worker
self.addEventListener('install', function(event) {
  event.waitUntil(
    caches.open(cacheName).then(function(cache) {
      return cache.addAll(resourcesToCache);
    })
  );
});

// Обробляємо запити
self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request).then(function(response) {
      // Перевіряємо, чи є відповідь в кеші
      if (response) {
        return response;
      }
      
      // Якщо користувач офлайн, повертаємо головну сторінку з кешу
      if (!navigator.onLine) {
        return caches.match('/index.php');
      }
      
      // Інакше виконуємо запит до мережі
      return fetch(event.request);
    })
  );
});

In a normal browser, the site is successfully cached and works offline, but in MIT App Inventor WebViewer it does not work offline and I see an error "no internet".

Here are my WebViewer settings:

I will be grateful for your help :heart:

The native webviewer does not have any local or session storage enabled Try with the webviewextra extension.

1 Like

How do I use webviewextra to achieve my goals?

Load the extension, then set the webviewer to it. Local and session storage will then be available .

Is this code enough?


or could it be better?

Sorry, I'm still new so I don't understand much

Put it in screen initialise

I did the following code but when I run it I just see a white screen even though I'm online
image

You still need to set the URL to your website in the webviewer blocks.

Do you mean that I have to specify the address in the code? Just not enough block settings?

How will the webviewer know what to display? I presume you were doing the same previously?

Yes, i do the same previously

The following code helped to get rid of the white screen:


But I always get a message that there is no Internet and the site does not work offline

Here is the webviewer setting just in case
image

Please help

My guess is, webviewer cannot run the PHP?

Why do you think I use php? I really use it, but I think that it does not affect anything. Because I use JavaScript to cache the page for it to work properly offline. I really need to finish this project, maybe you can recommend me something else that I can use to turn the site into an Android application quickly and without special knowledge of code.

Thanks again for helping! :heart:

I have now been able to do some quick research on the subject of caching with the android webview. It is not setup by default in the AI2 webviewer, nor enabled with webviewextra - a few more lines of code needed to do this. This would probably make your service worker redundant! If I get some time in the next couple of days, I will update a version for you to test.

There may still be an issue of how index.php is run....we will have to see.

Note this feature is already available if you use the CustomWebView extension

How can I use CustomWebView to achieve my goals? I am ready to install any extension that solves my problem.

You now know how to install and use an extension. Read the topic linked to, just about everything your need to know is there.

1 Like

Sorry, but I could not find an answer to my question in this topic. Could you explain what exactly I need to do to make my site work offline?

Why it has to be offline?

Did you view the CustomWebview topic? Did you install the CustomWebview extension, set it up correctly, as per the guidance in the topic, did you enable cache mode? Did you then test your app? Once you have done this, come back with questions if things are not working as expected.