Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbo intercepting back button even though Turbo.session.drive=false #1353

Open
saurabhnanda opened this issue Dec 13, 2024 · 0 comments
Open

Comments

@saurabhnanda
Copy link

I'm trying to introduce turbo in certain parts of my AngularJS application. Even though Turbo.session.drive = false; has been set a global level, and only few elements have data-turbo=true, I'm finding that turbojs intercepts the browser's back button, and ends-up making an additional HTTP GET request to the URL (over and above whatever Angular is doing), and ends-up rendering a blank page, thus completely breaking the client-side app.

How do I get to the bottom of what is happening and fix it?

Turbo is being loaded along with angular in this manner:

<head>
  <script src="/assets/angular/angular.min.js?body=1" type="text/javascript"></script>

  <script src="https://cdn.jsdelivr.net/npm/@hotwired/[email protected]/dist/turbo.es2017-umd.js" integrity="sha512-V9aQHDbe8UfvPmVTXpoqfNC+2Fvt9/eYew7xfWP25w318fXlN3OTvJxvvnvpTOVrhR8x+H5LuyChN913oB3fPg==" crossorigin="anonymous"></script>
  <script src="/assets/common/admin/turbojs_config.js?body=1" type="text/javascript"></script>
</head>

Further, here's what turbojs_config contains:

Turbo.session.drive = false;
Turbo.cache.exemptPageFromCache();
Turbo.cache.exemptPageFromPreview();

document.addEventListener('DOMContentLoaded', function() {
  try {
    const pb = angular.element('[ng-app]').injector().get('progressbar');
    document.addEventListener('turbo:before-fetch-request', function(evt) {
      if(evt.detail.fetchOptions.headers['X-Sec-Purpose']!='prefetch') {
        pb.start(); // start the on-page angularJs progress bar even if turbojs makes the network request
      }
    });
    document.addEventListener('turbo:before-fetch-response', function() {
      pb.complete(); // stop the on-page angularjs progress bar
    });
    document.addEventListener('turbo:before-fetch-response', async function(evt) {
      const resp = evt.detail.fetchResponse.response;
      if (resp.status>=500 && resp.status<600) {
        // common client-side code to handle 5xx erorrs
      } else if(resp.headers.get('x-sec-purpose')!='prefetch' && resp.status==400 && resp.headers.get('content-type').includes('application/json'))  {
        // common client side code to display validation errors
      }
    });
  } catch(e) {
    console.log('error in setting up integration with angular progress bar', e);
  }

  document.addEventListener('turbo:frame-load', function(event) {
    // re-initialize bootstrap tooltips inside turbo framss
    console.log('turbo:frame-load', $(event.target).find('[data-toggle="tooltip"]'));
    $(event.target).find('[data-toggle=tooltip]').tooltip();
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant