Skip to content

Commit 1ebbb14

Browse files
committed
Fix sorttable on initial page load
Previously, sorttable didn't work reliably when the page was loaded without Turbo. Now, sorttable should always work correctly
1 parent 8ea6f98 commit 1ebbb14

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

app/assets/javascripts/base.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ $.fn.scrollTo = function(selector) {
2020
}, ANIMATION_DURATION);
2121
};
2222

23-
$(document).on('turbo-migration:load', function() {
23+
$(document).on('turbo-migration:load', function(event) {
2424
// Update all CSRF tokens on the page to reduce InvalidAuthenticityToken errors
2525
// See https://github.com/rails/jquery-ujs/issues/456 for details
2626
$.rails.refreshCSRFTokens();
@@ -63,7 +63,9 @@ $(document).on('turbo-migration:load', function() {
6363
}
6464

6565
// Enable sorttable again, as it is disabled otherwise by Turbo
66-
if (sorttable) {
66+
// We use the event details to determine whether an ordinary page load occurred or a Turbo visit
67+
// In some events (i.e., for a failed form submission), `event.detail.timing` is not set (despite a Turbo "visit").
68+
if (sorttable && (event.detail?.timing ? Object.keys(event.detail.timing).length > 0 : true)) {
6769
sorttable.init.done = false;
6870
sorttable.init();
6971
}

0 commit comments

Comments
 (0)