Problem with turbolinks 5
valterbarros opened this issue · 4 comments
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);
});
}
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?
I've opened #421 with your fix, can you test it please?
Hi, thanks for the help and my problem with js actually seems to be a problem with turbolinks itself and this issue turbolinks/turbolinks#277 solved it. location.reload()
is no more necessary.
I test #421 here and the code is ok