Search code examples
javascripthtmlnginxsinatraunicorn

Javascript Error on Sinatra with NGINX with Unicorn


I have made a site that runs on NGINX with Unicorn Web Server, and Sinatra. I created the site locally, and everything worked great. However, after uploading, the countdown on the home page is not working. When I inspect it with chrome (the web version, located at http://udidreg.com), I get the following errors:

Uncaught TypeError: Object [object Object] has no method 'placeholder' customtime.js:41

and

Uncaught TypeError: Object [object Object] has no method 'countdown'

However, when I inspect locally, there are no errors, and it counts as it should.

Any help would be greatly appreciated!


Solution

  • TwoThree things come to mind.

    • You've not deployed the updated the customtime.js file, even though you think you have.
    • The customtime.js has been cached by the browser and it hasn't collected the new one.
    • The project's requirements (such as gems, scripts etc) aren't fully described by the Gemfile etc and due to the way your development environment is set up you don't get these errors on your dev machine.

    Be careful to sandbox your environment as much as possible, and to make sure that all things the project requires are within the project.

    A classic example is developing on a machine with an account that has the Administrator role, and then deploying to a machine that runs the app under an account with limited permissions.

    The classic with Ruby development is to have installed gems system wide using Rubygems, and then to install gems via Bundler for a project. Even though a few of the required gems are missed off the Gemfile, because the required gems are installed system wide no errors occur. On deployment these gems are found to be missing and the app fails.

    To stop this happening, sandbox your project's gems using Bundler (if you are) with the command bundle install --binstubs --path vendor, and then run everything via bundle exec… or from the bin dir e.g. bin/rackup config.ru. Then your project will only be running from the gems installed via Bundler. If anything is missing it will fail both on the development machine and the production machine. This is obviously better.

    To handle out of date assets like javascripts and stylesheets, I favour the method used in this helpful blog post. I like it so much that I've forked the Sinatra::StaticAssets gem and rewritten it to incorporate it, but I'm not entirely finished (though it still has the power to destroy planets, I'm worried about attacks from one manned fighters), so if you've a github account and it sounds like it may be of use to you you can follow it. If you want to try it just add the development branch to your Gemfile and be sure to let me know if it works well or badly for you.

    Edit: I finished the fork and made it into its own gem