0

Sorry for the long post, but I have a spring boot app and need to set it up as a simple PWA.

It works perfect locally, but every time I commit my changes to AWS (GitHub -> AWS Code Pipeline -> Elastic Beanstalk) I run into this frustrating error.

Note, my port is set to 5000 (not spring boot's default 8080 because 5000 is what AWS expects)

File tree overview

Resources
|                                 
├───static                         
│   │   service-worker.js          
│   │   site.webmanifest
│   │
│   ├───css
│   │       site.css
│   │
│   ├───images
│   │       android-chrome-192x192.png
│   │       ...
│   │
│   └───js
│           site.js
│
└───templates
    │   index.html
    │   ...
    │
    └───fragments
            elements.html

service-worker.js


self.addEventListener('install', function(event) {
  event.waitUntil(
    caches.open('ads-cache').then(function(cache) {
      try{
      return cache.addAll([
              '/',
              '/css/site.css',
              '/js/site.js'
            ]);
      } catch(error){
        console.log(error);
      }
    })
  )
});

self.addEventListener('activate', function(event) {
    // ...
});

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request).then(function(cachedResponse) {
      return cachedResponse || fetch(event.request);
    })
  );
});

site.webmanifest

{
    "name": "The app name",
    "short_name": "The short name",
    "description": "Details about the app",
    "icons": [
        { "src": "images/android-chrome-192x192.png", "sizes": "192x192", "type": "image/png" },
        { "src": "images/logo.png", "sizes": "200x105", "type": "image/png" }
    ],
    "theme_color": "#ffffff",
    "background_color": "#ffffff",
    "display": "standalone",
    "start_url": "/?source=pwa",
    "id": "/?source=pwa",
    "dir": "auto",
    "lang": "english",
    "orientation": "any",
    "scope": "/"
}

site.js

if ('serviceWorker' in navigator) {
 navigator.serviceWorker.register('/service-worker.js', {
     scope: '.'
   })
 .then(function(registration) {
   console.log('Service worker registration successful, scope is:', registration.scope);
 })
 .catch(function(error) {
   console.log('Service worker registration failed, error:', error);
 });
}

Localhost

Everything works fine

Service worker registering ok

But then once the site is live on AWS I keep getting this annoying (and very unhelpful) message

enter image description here

And I also noticed this which makes me think it's got something to do with the file structure maybe?

service-worker.js through DevTools sources tab

Showing error with filepath?

Am I doing something wrong "file structure wise" (I'm using Thymeleaf)? Is there something I need to change in AWS? Could this be a cloudflare thing? (my site is routed through cloudflare to AWS)

AussieDev81
  • 454
  • 5
  • 11

1 Answers1

0

I'm not sure if elastic beanstalk needed extra time, or is if was related to my browser cache, but today I noticed a warning in the browser console saying insecure content was loaded over HTTPS, but requested an insecure resource which gave me some new clues.

Following Alessio's answer in another post, I found that I had 2 links pointing to insecure http:// sources rather than https://.

    <!-- I changed -->
    <link th:href="@{http://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.css}" rel="stylesheet" />
    <link th:href="@{http://fonts.googleapis.com/css?family=Grand+Hotel}" rel="stylesheet" type="text/css" />

    <!-- To -->
    <link th:href="@{https://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.css}" rel="stylesheet" />
    <link th:href="@{https://fonts.googleapis.com/css?family=Grand+Hotel}" rel="stylesheet" type="text/css" />

This of course wasn't a problem running locally but then became an issue running live with SSL

AussieDev81
  • 454
  • 5
  • 11