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!

1
  • 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! Commented Dec 29, 2016 at 15:19

1 Answer 1

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
      })
    })
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.