rossta / serviceworker-rails

Use Service Worker with the Rails asset pipeline
MIT License
587 stars 44 forks source link

404 on /serviceworker.js in deployed environment #63

Open illinixc opened 5 years ago

illinixc commented 5 years ago

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

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 commented 5 years ago

@rossta can you provide any assistance here?

rossta commented 5 years ago

@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.

illinixc commented 5 years ago

@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/.

rossta commented 5 years ago

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.

nikriek commented 5 years ago

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

nikriek commented 5 years ago

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 commented 5 years ago

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.

sunwooz commented 2 years ago

@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?