PWA Not Registering

PWA Not Registering
0

I based a PWA off of a codelab from Google:

https://g.co/codelabs/pwa

Image of Issue https://drive.google.com/file/d/1W5iZReB1zm2cBL5JhzrIVKcZyY8y9QIG/view?usp=sharing

service-worker.js:

`/*
 * @license
 * Your First PWA Codelab (https://g.co/codelabs/pwa)
 * Copyright 2019 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
'use strict';

// CODELAB: Update cache names any time any of the cached files change.const CACHE_NAME = 'static-cache-v2';
const CACHE_NAME = 'static-cache-v2';
const DATA_CACHE_NAME = 'data-cache-v1';

// CODELAB: Add list of files to cache here.
const FILES_TO_CACHE = [
  '/index.html',
  '/',
  '/images/install.svg',
  '/images/refresh.svg',
  'https://sis.palmbeachschools.org/focus/Modules.php?modname=misc/Portal.php',
  'https://sis.palmbeachschools.org/focus/Modules.php?force_package=SIS&modname=Students/Student.php',
  'https://sis.palmbeachschools.org/focus/Modules.php?modname=Scheduling/Schedule.php&LO_index=',
  'https://sis.palmbeachschools.org/focus/Modules.php?modname=Attendance/StudentSummary.php&LO_index=&include_top=',
  'https://sis.palmbeachschools.org/focus/Modules.php?force_package=SIS&modname=Billing/Billing.php',
  '/scripts/app.js',
  '/scripts/install.js',
  '/scripts/luxon-1.11.4.js',
  '/styles/inline.css',
  '/images/add.svg',
  'https://sis.palmbeachschools.org/focus/Modules.php?modname=Grades/StudentGBGrades.php?',
];

self.addEventListener('install', (evt) => {
  console.log('[ServiceWorker] Install');
  // CODELAB: Precache static resources here.
  evt.waitUntil(
    caches.open(CACHE_NAME).then((cache) => {
      console.log('[ServiceWorker] Pre-caching offline page');
      return cache.addAll(FILES_TO_CACHE);

  self.skipWaiting();
});

self.addEventListener('activate', (evt) => {
  console.log('[ServiceWorker] Activate');
  // CODELAB: Remove previous cached data from disk.
    evt.waitUntil(
    caches.keys().then((keyList) => {
      return Promise.all(keyList.map((key) => {
        if (key !== CACHE_NAME) {
          console.log('[ServiceWorker] Removing old cache', key);
          return caches.delete(key);
        }
      }));
    })
);

  self.clients.claim();
});

self.addEventListener('fetch', (evt) => {
  console.log('[ServiceWorker] Fetch', evt.request.url);
  
  // CODELAB: Add fetch event handler here.
  if (evt.request.url.includes('https://sis.palmbeachschools.org')) {
  console.log('[Service Worker] Fetch (data)', evt.request.url);
  evt.respondWith(
      caches.open(DATA_CACHE_NAME).then((cache) => {
        return fetch(evt.request)
            .then((response) => {
              // If the response was good, clone it and store it in the cache.
              if (response.status === 200) {
                cache.put(evt.request.url, response.clone());
              }
              return response;
            }).catch((err) => {
              // Network request failed, try to get it from the cache.
              return cache.match(evt.request);
            });
      }));
  return;
}
evt.respondWith(
    caches.open(CACHE_NAME).then((cache) => {
      return cache.match(evt.request)
          .then((response) => {
            return response || fetch(evt.request);
          });
    })
);
  

});`

Can someone help me out here?

What kind of help are you seeking? What are you expecting the code to do vs. what it is doing? Are you seeing any errors in the browser console?

The issue is that the PWA won’t install because the start_url is not there or something, however, I don’t know how to put it in. The image is the Chrome’s DevTools Lighthouse showing the problem.

Do you have your full project available to show on Glitch?

Yes.

there were mistakes in multiple places. but you want this result yes:

here is the your project remixed, i just changed around some stuff in index.html and in your service worker file, it passes those tests now… Hope this can help you:

Yes. I would like the result to be that.

Thank you. I appreciate it.

1 Like

Also, I have noticed with the service worker, the website I tried to cache in the first glitch version I posted refuses to go into cache. It is not a static page, I suspect that is why, but I don’t know for sure. I still can’t post links and I’m on mobile rn so…