From 7ce0968a52a5bc013e49865205a8998881884c02 Mon Sep 17 00:00:00 2001 From: Connor Shea Date: Thu, 23 Jun 2016 10:33:38 -0600 Subject: [PATCH] Re-implement page-specific JS in a better way. This makes larger libraries more cacheable and will allow us to use SRI with the dynamically included libraries. --- app/assets/javascripts/application.js.coffee | 2 +- app/assets/javascripts/graphs/application.js.coffee | 1 - app/assets/javascripts/lib/chart/application.js.coffee | 1 + app/assets/javascripts/lib/d3/application.js.coffee | 1 + app/assets/javascripts/lib/raphael/application.js.coffee | 3 +++ app/assets/javascripts/users/application.js.coffee | 6 ------ app/views/layouts/_head.html.haml | 7 ++----- app/views/projects/graphs/_head.html.haml | 4 +++- app/views/projects/network/show.html.haml | 4 +++- app/views/users/show.html.haml | 4 +++- 10 files changed, 17 insertions(+), 16 deletions(-) create mode 100644 app/assets/javascripts/lib/chart/application.js.coffee create mode 100644 app/assets/javascripts/lib/d3/application.js.coffee create mode 100644 app/assets/javascripts/lib/raphael/application.js.coffee diff --git a/app/assets/javascripts/application.js.coffee b/app/assets/javascripts/application.js.coffee index 0206db461da..07b45eae9bb 100644 --- a/app/assets/javascripts/application.js.coffee +++ b/app/assets/javascripts/application.js.coffee @@ -50,7 +50,7 @@ #= require_directory ./ci #= require_directory ./commit #= require_directory ./extensions -#= require_directory ./lib +#= require_directory ./lib/. #= require_directory ./u2f #= require_directory . #= require fuzzaldrin-plus diff --git a/app/assets/javascripts/graphs/application.js.coffee b/app/assets/javascripts/graphs/application.js.coffee index 91f81a5d249..e0f681acf0b 100644 --- a/app/assets/javascripts/graphs/application.js.coffee +++ b/app/assets/javascripts/graphs/application.js.coffee @@ -4,5 +4,4 @@ # It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the # the compiled file. # -#= require Chart #= require_tree . diff --git a/app/assets/javascripts/lib/chart/application.js.coffee b/app/assets/javascripts/lib/chart/application.js.coffee new file mode 100644 index 00000000000..82217fc5107 --- /dev/null +++ b/app/assets/javascripts/lib/chart/application.js.coffee @@ -0,0 +1 @@ +#= require Chart diff --git a/app/assets/javascripts/lib/d3/application.js.coffee b/app/assets/javascripts/lib/d3/application.js.coffee new file mode 100644 index 00000000000..74f0a0bb06a --- /dev/null +++ b/app/assets/javascripts/lib/d3/application.js.coffee @@ -0,0 +1 @@ +#= require d3 diff --git a/app/assets/javascripts/lib/raphael/application.js.coffee b/app/assets/javascripts/lib/raphael/application.js.coffee new file mode 100644 index 00000000000..ab8e5979b87 --- /dev/null +++ b/app/assets/javascripts/lib/raphael/application.js.coffee @@ -0,0 +1,3 @@ +#= require raphael +#= require g.raphael +#= require g.bar diff --git a/app/assets/javascripts/users/application.js.coffee b/app/assets/javascripts/users/application.js.coffee index 647ffbf5f45..91cacfece46 100644 --- a/app/assets/javascripts/users/application.js.coffee +++ b/app/assets/javascripts/users/application.js.coffee @@ -1,8 +1,2 @@ -# This is a manifest file that'll be compiled into including all the files listed below. -# Add new JavaScript/Coffee code in separate files in this directory and they'll automatically -# be included in the compiled file accessible from http://example.com/assets/application.js -# It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the -# the compiled file. # -#= require d3 #= require_tree . diff --git a/app/views/layouts/_head.html.haml b/app/views/layouts/_head.html.haml index d5965a6ec99..bc3118f70aa 100644 --- a/app/views/layouts/_head.html.haml +++ b/app/views/layouts/_head.html.haml @@ -30,11 +30,8 @@ = javascript_include_tag "application", integrity: true - -# FIXME: SRI doesn't apply to the dynamically-generated per-page - -# JavaScript due to a bug in sprockets-rails. - -# See https://github.com/rails/sprockets-rails/issues/359 - - if page_specific_javascripts - = javascript_include_tag page_specific_javascripts, {"data-turbolinks-track" => true} + - if :page_specific_javascripts + = yield :page_specific_javascripts = csrf_meta_tags diff --git a/app/views/projects/graphs/_head.html.haml b/app/views/projects/graphs/_head.html.haml index a388d9a0a61..5ea4adcd0fa 100644 --- a/app/views/projects/graphs/_head.html.haml +++ b/app/views/projects/graphs/_head.html.haml @@ -1,7 +1,9 @@ .nav-links.sub-nav %ul{ class: (container_class) } - - page_specific_javascripts asset_path("graphs/application.js") + - content_for :page_specific_javascripts do + = javascript_include_tag 'lib/chart/application.js', {integrity: true, "data-turbolinks-track" => true} + = javascript_include_tag 'graphs/application.js', {integrity: true, "data-turbolinks-track" => true} = nav_link(action: :show) do = link_to 'Contributors', namespace_project_graph_path = nav_link(action: :commits) do diff --git a/app/views/projects/network/show.html.haml b/app/views/projects/network/show.html.haml index 593af319a47..749afe6b6ab 100644 --- a/app/views/projects/network/show.html.haml +++ b/app/views/projects/network/show.html.haml @@ -1,5 +1,7 @@ - page_title "Network", @ref -- page_specific_javascripts asset_path("network/application.js") +- content_for :page_specific_javascripts do + = javascript_include_tag 'lib/raphael/application.js', {integrity: true, "data-turbolinks-track" => true} + = javascript_include_tag 'network/application.js', {integrity: true, "data-turbolinks-track" => true} = render "projects/commits/head" = render "head" %div{ class: (container_class) } diff --git a/app/views/users/show.html.haml b/app/views/users/show.html.haml index 92305594a81..9ca351e5147 100644 --- a/app/views/users/show.html.haml +++ b/app/views/users/show.html.haml @@ -1,6 +1,8 @@ - page_title @user.name - page_description @user.bio -- page_specific_javascripts asset_path("users/application.js") +- content_for :page_specific_javascripts do + = javascript_include_tag 'lib/d3/application.js', {integrity: true, "data-turbolinks-track" => true} + = javascript_include_tag 'users/application.js', {integrity: true, "data-turbolinks-track" => true} - header_title @user.name, user_path(@user) - @no_container = true