serviceworker-rails gem与Solidus eCommerce gem不整合

时间:2018-09-17 13:09:40

标签: ruby-on-rails rubygems service-worker web-worker solidus

我正在使用serviceworker-rails gem使我的电子商务应用程序成为渐进式Web应用程序。这是该项目的gem文件片段;

# progressive web application gem
gem 'serviceworker-rails'
# eCommerce gems
gem 'solidus'
gem 'solidus_auth_devise'
#base gems
gem 'rails', '~> 5.2.0'
ruby '2.5.1'

serviceworker-rails gem依次生成使该应用程序成为渐进式Web应用程序所需的清单文件;

app / assets / javascripts / serviceworker.js.erb

var CACHE_VERSION = 'v1';
var CACHE_NAME = CACHE_VERSION + ':sw-cache-';

function onInstall(event) {
  console.log('[Serviceworker]', "Installing!", event);
  event.waitUntil(
    caches.open(CACHE_NAME).then(function prefill(cache) {
      return cache.addAll([

        // make sure serviceworker.js is not required by application.js
        // if you want to reference application.js from here
        '<%#= asset_path "application.js" %>',

        '<%= asset_path "application.css" %>',

        '/offline.html',

      ]);
    })
  );
}

function onActivate(event) {
  console.log('[Serviceworker]', "Activating!", event);
  event.waitUntil(
    caches.keys().then(function(cacheNames) {
      return Promise.all(
        cacheNames.filter(function(cacheName) {
          // Return true if you want to remove this cache,
          // but remember that caches are shared across
          // the whole origin
          return cacheName.indexOf(CACHE_VERSION) !== 0;
        }).map(function(cacheName) {
          return caches.delete(cacheName);
        })
      );
    })
  );
}

// Borrowed from https://github.com/TalAter/UpUp
function onFetch(event) {
  event.respondWith(
    // try to return untouched request from network first
    fetch(event.request).catch(function() {
      // if it fails, try to return request from the cache
      return caches.match(event.request).then(function(response) {
        if (response) {
          return response;
        }
        // if not found in cache, return default offline content for navigate requests
        if (event.request.mode === 'navigate' ||
          (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) {
          console.log('[Serviceworker]', "Fetching offline content", event);
          return caches.match('/offline.html');
        }
      })
    })
  );
}

self.addEventListener('install', onInstall);
self.addEventListener('activate', onActivate);
self.addEventListener('fetch', onFetch);

app / assets / javascripts / serviceworker-companion.js

if (navigator.serviceWorker) {
  navigator.serviceWorker.register('/serviceworker.js', { scope: './' })
    .then(function(reg) {
      console.log('[Companion]', 'Service worker registered!');
    });
}

该应用程序不会引发任何错误;只是我碰巧注意到服务人员没有注册。这是使用中的清单;

app / assets / javascripts / manifest.json.erb

{
  "name": "My Progressive Rails App",
  "short_name": "Progressive",
  "start_url": "/"
}

...最后;

app / views / layouts / application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Istore</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

<link rel="manifest" href="/manifest.json" />
<meta name="apple-mobile-web-app-capable" content="yes">
</head>

  <body>
    <%= yield %>
  </body>
</html>

公平地说;我以前使用过此设置,效果很好。当我将其与solidus_gem一起使用时,出现了一些中断。至今;我调试问题的努力只是使我无所适从。最强者缺乏根指令。

(对于默认的Rails初始页面,gem也不起作用)

但是在设置自定义根页面时;宝石作品。因此,我做了一些猴子补丁,然后将solidus的根页面添加到我的项目中,然后追加了应用程序路由以包含solidus主页。

config / routes.rb

Rails.application.routes.draw do
  root to: 'spree/home#index'

  mount Spree::Core::Engine, at: '/'    

end

这被证明是不成功的。我正在使用灯塔来审核应用程序,并且该错误表示未注册服务工作人员。

我想念什么?是否超出范围?

1 个答案:

答案 0 :(得分:0)

我终于使它起作用了:)。提供应用程序布局的gem是固相线,但问题是清单是在本地应用程序中,solidus是宝石。

使其对宝石可见;我从中删除了标签

app/views/layouts/application.html.erb

,然后将它们放入gems布局文件中。您必须在本地下载它才能进行更改。撰写本文时的布局文件位于

app/views/spree/layouts/

但是head部分包含_head部分的渲染图

app/views/spree/shared/_head.html.erb

在其中更有效地放置这些标签以解决问题。

<%= javascript_include_tag "application" %>
<link rel="manifest" href="/manifest.json" />
<meta name="apple-mobile-web-app-capable" content="yes">