-
Notifications
You must be signed in to change notification settings - Fork 109
Description
I started migrating a project from Turbolinks to the Turbo project and hit some issues. I have a workaround which might be useful to include in the documentation, or at least have here for others who encounter the same problem. Let me know if it would be helpful to PR somewhere.
Turbo feels like a drop-in replacement but it doesn't profess to being backwards compatible as DHH outlines: hotwired/turbo-rails#67
Although Turbo follows the same patterns as Turbolinks, and has the same events, i.e. before-cache, visit, load etc. they are now namespaced under turbo instead of turbolinks, so turbolinks:load is now turbo:load.
The IntercomJS client that gets loaded from Intercom has a bunch of Turbolinks event listeners, as you can see if you view the file and ctrl-f for Turbolinks. Due to the new names these event listeners are ignored and Intercom throws the following error when navigating via Turbo.
I've messaged Intercom support and asked them to consider including the new event names alongside the current ones. In the meantime I've found the following workaround:
document.addEventListener('turbo:before-cache', () => {
document.dispatchEvent(new Event('turbolinks:before-cache'))
});
document.addEventListener('turbo:load', () => {
document.dispatchEvent(new Event('turbolinks:load'))
});
document.addEventListener('turbo:visit', () => {
document.dispatchEvent(new Event('turbolinks:visit'))
});Version info
- intercom-rails version: 0.4.2
- Rails version: 6.0.3.3
Expected behavior
Intercom works when using Turbolinks to navigate
Actual behavior
Intercom JS throws an error
Steps to reproduce
- Install Turbo
- Load page
- Click link to another page
Logs
VM6567 frame-modern.412e5163.js:1 Uncaught TypeError: Cannot read property 'document' of null
at o (VM6567 frame-modern.412e5163.js:1)
at Object.read (VM6567 frame-modern.412e5163.js:1)
at VM6567 frame-modern.412e5163.js:1
at session_Session.createOrUpdateUser (VM6567 frame-modern.412e5163.js:1)
at app_App.createOrUpdateUser (VM6567 frame-modern.412e5163.js:1)
at Object.update (VM6567 frame-modern.412e5163.js:1)
at VM6567 frame-modern.412e5163.js:1
at <anonymous>:3:473
at <anonymous>:3:910
at replaceElementWithElement (turbo.es2017-esm.js:3925)
