@@ -437,6 +437,7 @@ import { ref, reactive, toRaw } from 'vue'
437437import { api } from ' @/api'
438438import { mixinDevice } from ' @/utils/mixin.js'
439439import { genericCompare } from ' @/utils/sort.js'
440+ import { sourceToken } from ' @/utils/request'
440441import store from ' @/store'
441442import eventBus from ' @/config/eventBus'
442443
@@ -616,6 +617,9 @@ export default {
616617 next ()
617618 },
618619 beforeRouteLeave (to , from , next ) {
620+ console .log (' DEBUG - Due to route change, ignoring results for any on-going API request' , this .apiName )
621+ sourceToken .cancel ()
622+ sourceToken .init ()
619623 this .currentPath = this .$route .fullPath
620624 next ()
621625 },
@@ -885,19 +889,30 @@ export default {
885889 break
886890 }
887891 }
888- this . itemCount = 0
892+ var apiItemCount = 0
889893 for (const key in json[responseName]) {
890894 if (key === ' count' ) {
891- this . itemCount = json[responseName].count
895+ apiItemCount = json[responseName].count
892896 continue
893897 }
894898 objectName = key
895899 break
896900 }
901+
902+ if (' id' in this .$route .params && this .$route .params .id !== params .id ) {
903+ console .log (' DEBUG - Discarding API response as its `id` does not match the uuid on the browser path' )
904+ return
905+ }
906+ if (this .dataView && apiItemCount > 1 ) {
907+ console .log (' DEBUG - Discarding API response as got more than one item in data view' , this .$route .params , this .items )
908+ return
909+ }
910+
897911 this .items = json[responseName][objectName]
898912 if (! this .items || this .items .length === 0 ) {
899913 this .items = []
900914 }
915+ this .itemCount = apiItemCount
901916
902917 if ([' listTemplates' , ' listIsos' ].includes (this .apiName ) && this .items .length > 1 ) {
903918 this .items = [... new Map (this .items .map (x => [x .id , x])).values ()]
@@ -946,6 +961,10 @@ export default {
946961 }
947962 }
948963 }).catch (error => {
964+ if (! error || ! error .message ) {
965+ console .log (' API request likely got cancelled due to route change:' , this .apiName )
966+ return
967+ }
949968 if ([401 ].includes (error .response .status )) {
950969 return
951970 }
0 commit comments