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

404 on /serviceworker.js in deployed environment

Open illinixc opened this issue 6 years ago • 8 comments

similar to #35, after deployment, i get a 404 on the serviceworker.js route. I am already on 0.5.5 though.

  • followed all instructions in README
  • https
  • CDN (i have also changed this to not use a Proc and just set to ENV['CDN_HOST'] directly to no effect)
    config.asset_host = Proc.new do |source|
      if source.starts_with?('/assets/') # per CloudFront path to origin mapping
        ENV['CDN_HOST']
      else
        nil # default behavior
      end
    end
  end
  • I have match "/serviceworker.js" in serviceworker.rb
  • config.assets.compile = false
  • config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? (false)
  • precompiling serviceworker.js, and verified presence of fingerprinted serviceworker.js file in public/assets/. https://<CDN_HOST>/assets/<fingerprinted_file> returns the correct javascript file. (note, needs the assets keyword in path)

localhost development is also working fine. from #35 (and a couple others) i will post the output of the same commands and info you've asked about. The conversation sort of stopped there so i dont know what results you were looking for.

Rails.application.config.assets.compile         
=> false
 asset_path = ::ActionController::Base.helpers.asset_path('/serviceworker.js')
=> "/serviceworker.js"
code - 405
headers - {"Content-Type"=>"text/plain", "Content-Length"=>"19", "X-Cascade"=>"pass", "Allow"=>"GET, HEAD, OPTIONS"}
body - ["Method Not Allowed\n"]

ngjnx logs just show a 404 to /serviceworker.js rails logs show a 404 to a path that looks to be more like a physical path?:

"method": "GET",
    "path": "/srv/www/<removed>/current/public/assets/serviceworker-2ed7e64d46b390cd57183ad0bb4105a2a64f6a84923c2c0b47cab0f6f9747b0c.js",
    "format": "js",
    "controller": "ErrorsController",
    "action": "routing_error",
    "status": 404,

RAILS_ENV=production rake middleware includes ServiceWorker middleware

From #44, I could grab nginx config if requested. There is a location that matches with "assets" but since thats not in the path and it does seem to be hitting the rails logs, it doesn't feel necessary.

Let me know if there's anything more i can provide, and thanks for making this gem!

illinixc avatar Nov 07 '18 16:11 illinixc

@rossta can you provide any assistance here?

illinixc avatar Nov 27 '18 18:11 illinixc

@illinixc The compiled serviceworker asset does not appear to be on your server. If you expect the file to be there, then make sure you've added the serviceworker asset tot the sprocket precompile config as described here: https://github.com/rossta/serviceworker-rails#precompile-the-assets and that your deployment process (capistrano? heroku?) is working as expected. If you are explicitly not putting compiled assets on your server—say, you are pushing the assets to your CDN directly—you could patch the project to handle such a case or have nginx perform the proxy.

rossta avatar Nov 27 '18 22:11 rossta

@rossta thanks for looking into it. Just curious, what makes you come to that conclusion? i'm not very proficient in rails and particularly the asset pipeline. I do have serviceworker.js in the precompile config according to the readme. from above:

precompiling serviceworker.js, and verified presence of fingerprinted serviceworker.js file in public/assets/.

illinixc avatar Nov 28 '18 13:11 illinixc

The Rails log 404ing on "/srv/www/<removed>/current/public/assets/serviceworker-2ed7e64d46b390cd57183ad0bb4105a2a64f6a84923c2c0b47cab0f6f9747b0c.js" means Rails can't find the file at that path. If you can access the production file system, you could try looking for that file or one with a similar fingerprint. You could also try precompiling the assets locally and run RAIL_ENV=production rails assets:precompile. If the file is there, it could be that your Rails process hasn't restarted so the computed asset path is pointing to the wrong file.

rossta avatar Nov 28 '18 15:11 rossta

Have you found any solution to this or what to look at? I am encountering a similar problem with Webpacker.

nikriek avatar Feb 25 '19 11:02 nikriek

I think this it happens due to this line here: https://github.com/rossta/serviceworker-rails/blob/4ebe4fff21f96775dc3ff62129ec10de4f2cc991/lib/serviceworker/handlers/webpacker_handler.rb#L12 In production, the manifest.json (I suppose this is used for the lookup) contains urls prefixed with the asset_host. The fileserver might not be able to find these locally.

nikriek avatar Feb 27 '19 12:02 nikriek

I wrote my custom service worker based on your ideas. The above issue can be solved by replacing the line above with:

path = URI(Webpacker.manifest.lookup(pack_file)).path

I can create a PR, if you want me to.

nikriek avatar Feb 27 '19 19:02 nikriek

@nikriek I tried forking the gem with the changes you mentioned, but I'm still getting an error on Heroku production. Sprockets::Rails::Helper::AssetNotFound (The asset "firebase-messaging-sw.js" is not present in the asset pipeline. Did you have to do anything else to fix it?

Hey @rossta , how would we resolve this on production?

sunwooz avatar Oct 04 '21 00:10 sunwooz