serviceworker-rails icon indicating copy to clipboard operation
serviceworker-rails copied to clipboard

undefined method `asset_pack_path' when referring to Webpacker entry packs

Open feliperaul opened this issue 3 years ago • 1 comments

I'm trying to refer to my Webpacker entry packs (JS and CSS) in my serviceworker.js.erb file, however it's throwing the following JS exception:

Uncaught Error: NoMethodError: undefined method `asset_pack_path' for #<#<Class:0x00007fa5b7856448>:0x00007fa5b89255d0>
Did you mean?  asset_path

Here's my serviceworker.js.erb file:

<% helpers = ActionController::Base.helpers %>

var CACHE_NAME = 'v1-cached-assets';

function onInstall(event) {
  event.waitUntil(
    caches.open(CACHE_NAME).then(function prefill(cache) {
      return cache.addAll([
        '<%= asset_path "application.js" %>', // This works
        '<%= asset_path "application.css" %>', // This works
        '<%= asset_pack_path 'some_entry_pack.js' %>', // This explodes
      ]);
    })
  );
}

feliperaul avatar Oct 27 '20 05:10 feliperaul

I ran into this problem also. I did try to use Webpacker.instance.manifest.lookup, but I think because the Webpacker assets are compiled after this is compiled it'll always return nil :/

MikeRogers0 avatar Jan 25 '21 16:01 MikeRogers0