Skip to content

404 on /serviceworker.js in deployed environment #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
illinixc opened this issue Nov 7, 2018 · 8 comments
Open

404 on /serviceworker.js in deployed environment #63

illinixc opened this issue Nov 7, 2018 · 8 comments

Comments

@illinixc
Copy link

illinixc commented Nov 7, 2018

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
Copy link
Author

illinixc commented Nov 27, 2018

@rossta can you provide any assistance here?

@rossta
Copy link
Owner

rossta commented Nov 27, 2018

@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
Copy link
Author

@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
Copy link
Owner

rossta commented Nov 28, 2018

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
Copy link

nikriek commented Feb 25, 2019

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

@nikriek
Copy link

nikriek commented Feb 27, 2019

I think this it happens due to this line here:

path = Webpacker.manifest.lookup(path_info)
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
Copy link

nikriek commented Feb 27, 2019

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
Copy link

sunwooz commented Oct 4, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants