Skip to content
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

Add cache-busting path for webshims #22

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,34 @@ Since webshims does not support fingerprinting, this will result in 404s (missin
(Note that this should be run directly, not in a dom-ready block.)

```javascript
$.webshims.setOptions('basePath', '/assets/webshims/shims/')
$.webshims.setOptions('basePath', '/webshims/shims/1.15.6/')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this change.. I don't think it's a good idea to hardcode the webshims version into the application. That's pretty much what we are trying to avoid in all this.

Maybe we should use the webshims_path helper here as an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, which is why I created the webshims_path helper. However, in our case (and this seemed likely for most implementations), this line is in a JS file that's loaded and compressed with the rest of the JS into application.js and therefore doesn't run through ERB (or HAML or whatever).

The problem I've found, running this branch, is that the webshims are no longer offloaded to nginx and so they use the rails server which they really shouldn't need to.

I think I like your approach of just writing webshims to public and letting the assets manager (nginx, CDN, etc.) take care of it from there.

$.webshims.polyfill()
```

The version number above should match the version of Webshims. When you update the gem change this so browser-cached files get reloaded. If you are interpolating your JavaScript, you can use the helper `webshims_path` to automatically populate the path with version number.

4. For Turbolinks users only: you'll need to update the polyfill on page load:

```coffeescript
$(document).on "page:load", ->
$(this).updatePolyfill()
```

5. For Spork users, if you have a line to reload your routes for each spec run in your `spec_helper`:

```ruby
Spork.each_run do
# This code will be run each time you run your specs.
load "#{Rails.root}/config/routes.rb"
end
```

Then you need to add the `mount` to your `config/routes.rb` otherwise it gets removed by Spork's reload.

```ruby
mount Webshims::Rails::Rewrite.new, at: '/webshims/shims'
```

## Updating this gem

This is only in the case this repository is not up-to-date; I try to stay current with webshims but sometimes I miss the webshims releases.
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rails.application.routes.draw do
mount Webshims::Rails::Rewrite.new, :at => "/webshims/shims"
end
6 changes: 6 additions & 0 deletions lib/webshims-rails.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require "webshims-rails/version"
require "webshims-rails/view-helpers"
require "webshims-rails/rewrite"

module Webshims
module Rails
Expand All @@ -12,6 +14,10 @@ class Engine < ::Rails::Engine
app.config.assets.precompile << /webshims/
end
end

initializer "Webshims::Rails::ViewHelpers" do
ActiveSupport.on_load(:action_view) { include Webshims::Rails::ViewHelpers }
end
end
end
end
21 changes: 21 additions & 0 deletions lib/webshims-rails/rewrite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Webshims
module Rails
class Rewrite < Rack::File

def initialize
super(root)
end

def call(env)
env["PATH_INFO"] = env["PATH_INFO"].gsub(%r(\A/[0-9.]+/), '')
super(env)
end

private

def root
File.join(Gem.loaded_specs["webshims-rails"].full_gem_path, "vendor", "assets", "javascripts", "webshims", "shims")
end
end
end
end
9 changes: 9 additions & 0 deletions lib/webshims-rails/view-helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Webshims
module Rails
module ViewHelpers
def webshims_path
"/webshims/shims/#{Webshims::Rails::WEBSHIMS_VERSION}/"
end
end
end
end