BetterErrors / better_errors

Better error page for Rack apps
MIT License
6.88k stars 437 forks source link

Problem with turbolinks 5 #420

Closed valterbarros closed 6 years ago

valterbarros commented 6 years ago

Rails: 5.2.0 Turbolinks: 5.1.1 Ruby: 2.5.1

When a enter in a error from better errors and click in back button from browser something like this happen: "rails-ujs has already been loaded!" but other happen too like select2 is not defined

And I noticed that in your code you need update the turbolinks implementation for the new api

      if (window.Turbolinks) {
          for(var i=0; i < document.styleSheets.length; i++) {
              if(document.styleSheets[i].href)
                  document.styleSheets[i].disabled = true;
          }
          document.addEventListener("page:restore", function restoreCSS(e) {
              for(var i=0; i < document.styleSheets.length; i++) {
                  document.styleSheets[i].disabled = false;
              }
              document.removeEventListener("page:restore", restoreCSS, false);
          });
      }

Turbolinks no longer uses page:restore now all events are with turbolinks prefix like turbolinks:load

To solve this I try in console from web put a reload in before the removeEventListener and added turbolinks:load intead page:restore like:

      if (window.Turbolinks) {
          for(var i=0; i < document.styleSheets.length; i++) {
              if(document.styleSheets[i].href)
                  document.styleSheets[i].disabled = true;
          }
          document.addEventListener("turbolinks:load", function restoreCSS(e) {
              for(var i=0; i < document.styleSheets.length; i++) {
                  document.styleSheets[i].disabled = false;
              }
           location.reload();
           document.removeEventListener("turbolinks:load", restoreCSS, false);

          });
      }
RobinDaugherty commented 6 years ago

Thank you @valterbarros! I don't have a project to test this with, but I can attempt a fix. I noticed that you added location.reload() in the code for Turbolinks 5, can you explain that change?

RobinDaugherty commented 6 years ago

I've opened #421 with your fix, can you test it please?

valterbarros commented 6 years ago

Hi, thanks for the help and my problem with js actually seems to be a problem with turbolinks itself and this issue https://github.com/turbolinks/turbolinks/issues/277 solved it. location.reload() is no more necessary.

valterbarros commented 6 years ago

I test #421 here and the code is ok