PWA化を行いたく、serviceworkerを導入したのですが、ローカルでは特に問題なく動いているのですが、herokuでのproduction環境ですとなにやらapplication.jsが以下のような理由により働いていないではないか思われます。
・ログインした時などの出ていた、toastrというgemで作成された「ログインしました」をはじめとするフラッシュメッセージが出なくなってしまった。
・ドロップダウンバーをクリックしても各々のリンクボタンが出てこない。
・micropostの削除ボタンを押すとローカルではdeleteできるが、production環境では、heroku_logsにて以下エラーとなっている。(なぜか、GETにrouteされている)
ActionController::RoutingError (No route matches [GET] "/microposts/3052"):
micropost削除ボタンのコード
_micropost.html.erb
<% if current_user?(micropost.user) %> <%= link_to "削除する", micropost_path(micropost), method: :delete, data: { confirm: "削除してもいいですか?"} %> <% end %>
原因ではないかと自分で思うコードを掲載させて頂きますので、もしお分かりになる方がいらっしゃいましたら、ご指摘頂ければ幸いです。
application.js
// This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. // // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require jquery //= require jquery_ujs //= require turbolinks //= require_tree . //= require jquery //= require bootstrap-sprockets //= require toastr //= require serviceworker-companion
javascripts/serviceworker-companion.js
if (navigator.serviceWorker) { navigator.serviceWorker.register('/serviceworker.js', { scope: './' }) .then(function(reg) { console.log('[Companion]', 'Service worker registered!'); }); }
javascripts/serviceworker.js.erb
// fetchvar CACHE_VERSION = 'v1'; 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 "www_domain/application.js" %>', '<%= asset_path "www_domain/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);
config/environments/production.rb
config.assets.js_compressor = Uglifier.new(harmony: true) config.assets.css_compressor = :sass # Do not fallback to assets pipeline if a precompiled asset is missed. config.assets.compile = true
javascripts/application.js
// This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. // // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require jquery //= require rails-ujs //= require turbolinks //= require bootstrap-sprockets //= require toastr //= require serviceworker-companion //= require_tree .
長々と大変恐縮ですが、ご確認いただければと思います。
あなたの回答
tips
プレビュー