1

I'm trying to add a service worker to my Rails app with the serviceworker gem.

I've been following this guide https://rossta.net/blog/offline-page-for-your-rails-application.html and started receiving the error listed in the title.

I've reviewed other questions similar, but most are due to syntax errors. As this post Rails 5 Heroku deploy error: ExecJS::ProgramError: SyntaxError: Unexpected token: name (autoRegisterNamespace)

My error is due to the token name (catch) which was supplied via the serviceworker gem file.

Here is my code where the error is occurring ...

 function onFetch(event) {
   // Fetch from network, fallback to cached content, then offline.html for same-origin GET requests
   var request = event.request;

   if (!request.url.match(/^https?:\/\/example.com/) ) { return; }
   if (request.method !== 'GET') { return; }

   event.respondWith(
     fetch(request).                                       // first, the network
        .catch(function fallback() {
          caches.match(request).then(function(response) {  // then, the cache
            response || caches.match("/offline.html.erb");     // then, /offline cache
          })
        })
    );

I understand the error is at .catch , I'm just not sure how to solve the issue.

Any help is much appreciated, thanks!

Community
  • 1
  • 1
Jonathan Corrin
  • 1,219
  • 4
  • 17
  • 42
  • That was it, the (.) at the end of fetch was causing the error. I overlooked it because it nothing was raised about being a syntax issue. Thank you! – Jonathan Corrin Dec 29 '16 at 15:19

1 Answers1

1

Syntax error with two dots. The line break sometimes makes it hard to notice.

 event.respondWith(
 fetch(request).    // Dot here and then...
    .catch(function fallback() { // ... the one at beginning of line as well causes the issue...
      caches.match(request).then(function(response) {  // then, the cache
        response || caches.match("/offline.html.erb");     // then, /offline cache
      })
    })
);

Should be

 event.respondWith(
 fetch(request)                                     // remove dot 
    .catch(function fallback() {
      caches.match(request).then(function(response) {  // then, the cache
        response || caches.match("/offline.html.erb");     // then, /offline cache
      })
    })
);
Rockwell Rice
  • 3,376
  • 5
  • 33
  • 61