From acba7d2a5dc1701ffd42eb2e92056a49769a0718 Mon Sep 17 00:00:00 2001 From: Kyle Zhao Date: Tue, 9 Oct 2018 13:50:45 +0800 Subject: [PATCH] Extract `discourse_javascript.html.erb` to a scrip include * extract omniauth auth complete inline JS * extract Ember error logging inline JS * transpile `authentication-complete` This is CSP related work --- .../authentication-complete.js.no-module.es6 | 17 +++++++++ .../preload-application-data.js.no-module.es6 | 22 +++++++++++ .../common/_discourse_javascript.html.erb | 38 ------------------- app/views/layouts/application.html.erb | 7 +++- config/application.rb | 1 + 5 files changed, 46 insertions(+), 39 deletions(-) create mode 100644 app/assets/javascripts/authentication-complete.js.no-module.es6 delete mode 100644 app/views/common/_discourse_javascript.html.erb diff --git a/app/assets/javascripts/authentication-complete.js.no-module.es6 b/app/assets/javascripts/authentication-complete.js.no-module.es6 new file mode 100644 index 00000000000..ad768b64261 --- /dev/null +++ b/app/assets/javascripts/authentication-complete.js.no-module.es6 @@ -0,0 +1,17 @@ +(function() { + const authenticationData = JSON.parse( + document.getElementById("data-authentication").dataset.authenticationData + ); + + Discourse.showingSignup = true; + require("discourse/routes/application").default.reopen({ + actions: { + didTransition: function() { + Em.run.next(function() { + Discourse.authenticationComplete(authenticationData); + }); + return this._super(); + } + } + }); +})(); diff --git a/app/assets/javascripts/preload-application-data.js.no-module.es6 b/app/assets/javascripts/preload-application-data.js.no-module.es6 index a544c1a9a16..7291d919502 100644 --- a/app/assets/javascripts/preload-application-data.js.no-module.es6 +++ b/app/assets/javascripts/preload-application-data.js.no-module.es6 @@ -39,4 +39,26 @@ Discourse.S3CDN = setupData.s3Cdn; Discourse.S3BaseUrl = setupData.s3BaseUrl; } + + Ember.RSVP.configure("onerror", function(e) { + // Ignore TransitionAborted exceptions that bubble up + if (e && e.message === "TransitionAborted") { + return; + } + + if (Discourse.Environment === "development") { + if (e) { + if (e.message || e.stack) { + console.log(e.message); + console.log(e.stack); + } else { + console.log("Uncaught promise: ", e); + } + } else { + console.log("A promise failed but was not caught."); + } + } + + window.onerror(e && e.message, null, null, null, e); + }); })(); diff --git a/app/views/common/_discourse_javascript.html.erb b/app/views/common/_discourse_javascript.html.erb deleted file mode 100644 index 98f63940d14..00000000000 --- a/app/views/common/_discourse_javascript.html.erb +++ /dev/null @@ -1,38 +0,0 @@ - - - - -<%= preload_script 'browser-update' %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 140c1b9ac97..5fbcc741379 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -55,6 +55,11 @@ <%= build_plugin_html 'server:before-head-close' %> <%= tag.meta id: 'data-discourse-setup', data: client_side_setup_data %> + + <%- if !current_user && flash[:authentication_data] %> + + <%= preload_script "authentication-complete" %> + <%- end %> @@ -106,7 +111,7 @@ <%= yield :data %> - <%= render :partial => "common/discourse_javascript" %> + <%= preload_script 'browser-update' %> <%- unless customization_disabled? %> <%= raw theme_lookup("body_tag") %> diff --git a/config/application.rb b/config/application.rb index 9c23554c14f..640f05f5b71 100644 --- a/config/application.rb +++ b/config/application.rb @@ -120,6 +120,7 @@ module Discourse google-tag-manager.js google-universal-analytics.js preload-application-data.js + authentication-complete.js } # Precompile all available locales