【问题标题】: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)
【发布时间】:2016-12-29 15:13:52
【问题描述】:

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

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

我查看过其他类似的问题,但大多数是由于语法错误。正如这篇文章Rails 5 Heroku deploy error: ExecJS::ProgramError: SyntaxError: Unexpected token: name (autoRegisterNamespace)

我的错误是由于通过 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 ,我只是不确定如何解决这个问题。

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

【问题讨论】:

  • 就是这样,fetch 末尾的 (.) 导致了错误。我忽略了它,因为它没有提出任何关于语法问题的问题。谢谢!

标签: ruby-on-rails ruby heroku service-worker


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 2017-01-06
    相关资源
    最近更新 更多