Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I register a service worker in Ruby on Rails?

I'm trying register a service worker in Ruby On Rails to implement a push notification GCM. But nothing happens. Please, see my code below:

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/assets/service-worker.js')
    .then(initialiseState);
  } else {
    window.Demo.debug.log('Service workers aren\'t supported in this browser.');

This part works fine. But, when I call navigator.serviceWorker.ready.then(function(serviceWorkerRegistration), nothing happens.

I was searching on search engine and there are few things to help me.

Can anybody help me ?

like image 320
Tercio Avatar asked Oct 01 '15 03:10

Tercio


Video Answer


1 Answers

Check out the serviceworker-rails gem.

As Jeff mentioned, the path on which the service worker must be "in scope". This gem allows you to proxy service worker requests at the root (or any other path) to a resource in the Rails asset pipeline.

For example, if your serviceworker script entry point is in app/assets/javascripts/nested/directory/serviceworker.js in your Rails project, you can configure your application to render the script from /serviceworker.js with some simple configuration setup:

# config/application.rb
config.serviceworker.routes.draw do
  match "/serviceworker.js" => "nested/directory/serviceworker.js"
end
like image 178
rossta Avatar answered Sep 30 '22 15:09

rossta