@@ -442,6 +442,7 @@ import { ref, reactive, toRaw } from 'vue'
442442import { api } from ' @/api'
443443import { mixinDevice } from ' @/utils/mixin.js'
444444import { genericCompare } from ' @/utils/sort.js'
445+ import { sourceToken } from ' @/utils/request'
445446import store from ' @/store'
446447import eventBus from ' @/config/eventBus'
447448
@@ -621,6 +622,9 @@ export default {
621622 next ()
622623 },
623624 beforeRouteLeave (to , from , next ) {
625+ console .log (' DEBUG - Due to route change, ignoring results for any on-going API request' , this .apiName )
626+ sourceToken .cancel ()
627+ sourceToken .init ()
624628 this .currentPath = this .$route .fullPath
625629 next ()
626630 },
@@ -920,19 +924,30 @@ export default {
920924 break
921925 }
922926 }
923- this . itemCount = 0
927+ var apiItemCount = 0
924928 for (const key in json[responseName]) {
925929 if (key === ' count' ) {
926- this . itemCount = json[responseName].count
930+ apiItemCount = json[responseName].count
927931 continue
928932 }
929933 objectName = key
930934 break
931935 }
936+
937+ if (' id' in this .$route .params && this .$route .params .id !== params .id ) {
938+ console .log (' DEBUG - Discarding API response as its `id` does not match the uuid on the browser path' )
939+ return
940+ }
941+ if (this .dataView && apiItemCount > 1 ) {
942+ console .log (' DEBUG - Discarding API response as got more than one item in data view' , this .$route .params , this .items )
943+ return
944+ }
945+
932946 this .items = json[responseName][objectName]
933947 if (! this .items || this .items .length === 0 ) {
934948 this .items = []
935949 }
950+ this .itemCount = apiItemCount
936951
937952 if ([' listTemplates' , ' listIsos' ].includes (this .apiName ) && this .items .length > 1 ) {
938953 this .items = [... new Map (this .items .map (x => [x .id , x])).values ()]
@@ -982,6 +997,10 @@ export default {
982997 }
983998 }
984999 }).catch (error => {
1000+ if (! error || ! error .message ) {
1001+ console .log (' API request likely got cancelled due to route change:' , this .apiName )
1002+ return
1003+ }
9851004 if ([401 ].includes (error .response .status )) {
9861005 return
9871006 }
0 commit comments