ExecJS::RuntimeError: SyntaxError: Unexpected token: name (catch) (line: 41, col: 8, pos: 1392)

ExecJS::RuntimeError: SyntaxError: Unexpected token: name (catch) (line: 41, col: 8, pos: 1392)

我正在尝试使用 serviceworker gem 将 service worker 添加到我的 Rails 应用程序。

我一直在遵循本指南 https://rossta.net/blog/offline-page-for-your-rails-application.html 并开始收到标题中列出的错误。

我已经查看了其他类似的问题,但大多数是由于语法错误。因为这个 post

我的错误是由于通过 serviceworker gem 文件提供的令牌名称 (catch)。

这是我发生错误的代码...

 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
          })
        })
    );

我知道错误出在 .catch ,我只是不确定如何解决这个问题。

非常感谢任何帮助,谢谢!

有两个点的语法错误。换行符有时让人很难注意到。

 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
      })
    })
);

应该是

 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
      })
    })
);