From f351ade434ca0fc05fad4c54e90a404c51bd8c4a Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 3 Dec 2019 15:13:20 +0000 Subject: [PATCH] javascript: expose Turbolinks globally MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When calling `redirect_to` in a Rails controller that emits Javascript, Rails will emit `Turbolinks.replace(…)` commands. And for this, Turbolinks needs to be globally available. --- app/javascript/packs/application.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index fbe2ee237..a0e320c3a 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -65,19 +65,14 @@ Rails.start(); Turbolinks.start(); ActiveStorage.start(); -// If Turbolinks is imported via Webpacker (and thus not available globally), -// ReactRailsUJS will be unable to locate it. -// https://github.com/reactjs/react-rails#event-handling - -// eslint-disable-next-line no-undef -ReactRailsUJS.useContext(require.context('components', true)); -// Add Turbolinks to the global namespace: -window.Turbolinks = Turbolinks; -// Remove previous event handlers and add new ones: -ReactRailsUJS.detectEvents(); -// (Optional) Clean up global namespace: -delete window.Turbolinks; - // Expose globals window.DS = window.DS || DS; window.Chartkick = Chartkick; +// (Both Rails redirects and ReactRailsUJS expect Turbolinks to be globally available) +window.Turbolinks = Turbolinks; + +// Now that Turbolinks is globally exposed,configure ReactRailsUJS +// eslint-disable-next-line no-undef +ReactRailsUJS.useContext(require.context('components', true)); +// Remove previous event handlers and add new ones: +ReactRailsUJS.detectEvents();