Prevent Safari from dispatching search requests twice
authorAlexander Ebert <ebert@woltlab.com>
Thu, 18 Aug 2022 13:19:38 +0000 (15:19 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 18 Aug 2022 13:19:38 +0000 (15:19 +0200)
See https://www.woltlab.com/community/thread/296867-fetch-is-aborted-bei-suchergebnissen/

ts/WoltLabSuite/Core/Ui/Search/Extended.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Search/Extended.js

index 065e5fd681cbfc838818a6b577cff6995b4c3f58..ee1525bddcc238fa83d75d3f0797f8ab6a32d77d 100644 (file)
@@ -70,7 +70,13 @@ export class UiSearchExtended {
     });
     this.typeInput.addEventListener("change", () => this.changeType());
 
-    window.addEventListener("popstate", () => {
+    window.addEventListener("popstate", (event) => {
+      if (event.state.searchAction && event.state.searchAction === SearchAction.Init) {
+        // Safari fires the `popstate` for the initial request on
+        // navigation, causing the search to be dispatched twice.
+        return;
+      }
+
       this.initQueryString();
     });
   }
@@ -151,9 +157,9 @@ export class UiSearchExtended {
     url.search += new URLSearchParams(parameters);
 
     if (searchAction === SearchAction.Init) {
-      window.history.replaceState({}, document.title, url.toString());
+      window.history.replaceState({ searchAction }, document.title, url.toString());
     } else {
-      window.history.pushState({}, document.title, url.toString());
+      window.history.pushState({ searchAction }, document.title, url.toString());
     }
   }
 
index 0fea94ba6540f312d1996049a24c8cf36cfaf746..0165ec5a61db11a783b2bb4d4f599c7ad116ec7c 100644 (file)
@@ -40,7 +40,12 @@ define(["require", "exports", "tslib", "../../Ajax", "../../Date/Picker", "../..
                 void this.search(0 /* SearchAction.Modify */);
             });
             this.typeInput.addEventListener("change", () => this.changeType());
-            window.addEventListener("popstate", () => {
+            window.addEventListener("popstate", (event) => {
+                if (event.state.searchAction && event.state.searchAction === 2 /* SearchAction.Init */) {
+                    // Safari fires the `popstate` for the initial request on
+                    // navigation, causing the search to be dispatched twice.
+                    return;
+                }
                 this.initQueryString();
             });
         }
@@ -109,10 +114,10 @@ define(["require", "exports", "tslib", "../../Ajax", "../../Date/Picker", "../..
             }
             url.search += new URLSearchParams(parameters);
             if (searchAction === 2 /* SearchAction.Init */) {
-                window.history.replaceState({}, document.title, url.toString());
+                window.history.replaceState({ searchAction }, document.title, url.toString());
             }
             else {
-                window.history.pushState({}, document.title, url.toString());
+                window.history.pushState({ searchAction }, document.title, url.toString());
             }
         }
         getFormData() {