2016-03-03 79 views
4

我正在使用服务工作人员进行推送通知,请看this文章。一切正在与Chrome,但Firefox(v.44.0.2)我有一个奇怪的问题。 成功登录到我的应用程序后,我注册了服务人员,该人员除了等待推送事件外什么也不做;我看到这是正确的登记(从一些日志记录和从关于:serviceworkers)。现在,如果我刷新页面(CTRL + R),则由于此服务工作人员的原因,我的所有POST都有CORS问题(缺少访问控制 - 允许来源标题),并且用户被重定向到登录页面;从这里开始,所有POST不起作用的原因是相同的。 相反,如果我登录,取消注册服务人员和然后刷新,根本没有问题。对发生什么事有任何想法?我的服务人员再一次处理推送事件,不会缓存任何其他处理,并且它可以在Chrome上正常工作。在Firefox上引发CORS问题的服务人员

这里是我的服务人员的代码(SOME_API_URL点,使服务人员登记后,这个问题会发生哪些不需要测试用一个真正的API,无需推事件)

self.addEventListener('push', function(event) { 
    // Since there is no payload data with the first version 
    // of push messages, we'll grab some data from 
    // an API and use it to populate a notification 
    event.waitUntil(
    fetch(SOME_API_URL).then(function(response) { 
     if (response.status !== 200) { 
     // Either show a message to the user explaining the error 
     // or enter a generic message and handle the 
     // onnotificationclick event to direct the user to a web page 
     console.log('Looks like there was a problem. Status Code: ' + response.status); 
     throw new Error(); 
     } 

     // Examine the text in the response 
     return response.json().then(function(data) { 
     if (data.error || !data.notification) { 
      console.error('The API returned an error.', data.error); 
      throw new Error(); 
     } 

     var title = data.notification.title; 
     var message = data.notification.message; 
     var icon = data.notification.icon; 
     var notificationTag = data.notification.tag; 

     return self.registration.showNotification(title, { 
      body: message, 
      icon: icon, 
      tag: notificationTag 
     }); 
     }); 
    }).catch(function(err) { 
     console.error('Unable to retrieve data', err); 

     var title = 'An error occurred'; 
     var message = 'We were unable to get the information for this push message'; 

     var notificationTag = 'notification-error'; 
     return self.registration.showNotification(title, { 
     body: message, 
     tag: notificationTag 
     }); 
    }) 
); 
}); 

self.addEventListener('notificationclick', function(event) { 
    console.log('On notification click: ', event.notification.tag); 
    // Android doesn't close the notification when you click on it 
    // See: http://crbug.com/463146 
    event.notification.close(); 

    // This looks to see if the current is already open and 
    // focuses if it is 
    event.waitUntil(
    clients.matchAll({ 
     type: 'window' 
     }) 
     .then(function(clientList) { 
     for (var i = 0; i < clientList.length; i++) { 
      var client = clientList[i]; 
      if (client.url == '/' && 'focus' in client) 
      return client.focus(); 
     } 
     if (clients.openWindow) { 
      return clients.openWindow('/'); 
     } 
     }) 
); 
}); 
+0

文章?你能否告诉我们你的服务人员的源代码,以防你不在? – Marco

+0

@Marco我更新了我的问题 – Dario

+1

我没有答案,但我可以补充说'Access-Control-Allow-Origin'头可能会丢失,因为Firefox没有发送带有请求的Origin头。您可以在网络标签中验证。我们现在正在处理这个确切的问题。 – mwcz

回答

3

火狐44 bug 1243453,如果服务工作人员不侦听提取事件,则会导致跨源请求的Origin头部被丢弃。

该问题已在Firefox 45中解决,该问题将于2016年3月8日(下周截至撰写本文时)发布。在此期间,和谁不立即升级到最新的Firefox版本的用户,则可以解决该问题通过将此代码添加到服务人员:您使用完全相同来自同一服务人员

addEventListener('fetch', function(evt) { 
    evt.respondWith(fetch(evt.request)); 
}); 
+1

这实际上工作,但仍然看起来“推”事件没有触发FF(45)(虽然订阅和注册成功) – Dario

+0

@Dario:我建议问一个单独的问题。你会得到更多的人来看看它! –