diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c61fafea9..e611a2cbd 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -35,4 +35,19 @@ module ApplicationHelper def diaspora_id_host User.diaspora_id_host end + + # Require jQuery from CDN if possible, falling back to vendored copy, and require + # vendored jquery_ujs + def jquery_include_tag + buf = [] + if AppConfig[:jquery_cdn] + version = Jquery::Rails::JQUERY_VERSION + buf << [ javascript_include_tag("//ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js") ] + buf << [ javascript_tag("!window.jQuery && document.write(unescape('#{j javascript_include_tag("jquery")}'));") ] + else + buf << [ javascript_include_tag('jquery') ] + end + buf << [ javascript_include_tag('jquery_ujs') ] + buf.join("\n").html_safe + end end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index a3a0c4c7a..01fa8f767 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -29,7 +29,7 @@ = javascript_include_tag :ie - = javascript_include_tag 'jquery', 'jquery_ujs' + = jquery_include_tag - unless @landing_page = javascript_include_tag :main, :templates diff --git a/config/application.yml.example b/config/application.yml.example index 1de63b89a..164768668 100644 --- a/config/application.yml.example +++ b/config/application.yml.example @@ -39,6 +39,9 @@ defaults: &defaults # let your reverse proxy/webserver do it. serve_static_assets: false + # Serve jQuery from Google's CDN + jquery_cdn: true + # # Settings # diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 271fd662b..7a34f9b89 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -48,4 +48,35 @@ describe ApplicationHelper do all_services_connected?.should be_false end end + + describe "#jquery_include_tag" do + describe "with google cdn" do + before do + AppConfig[:jquery_cdn] = true + end + + it 'inclues jquery.js from google cdn' do + jquery_include_tag.should match(/googleapis\.com/) + end + + it 'falls back to asset pipeline on cdn failure' do + jquery_include_tag.should match(/document\.write/) + end + end + + describe "without google cdn" do + before do + AppConfig[:jquery_cdn] = false + end + + it 'includes jquery.js from asset pipeline' do + jquery_include_tag.should match(/jquery\.js/) + jquery_include_tag.should_not match(/googleapis\.com/) + end + end + + it 'inclues jquery_ujs.js' do + jquery_include_tag.should match(/jquery_ujs\.js/) + end + end end