Skip to content

Compatibility with hotwired/turbo-rails #336

@hcurotta

Description

@hcurotta

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.

image

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

  1. Install Turbo
  2. Load page
  3. 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions