Skip to content

Commit ad4bf02

Browse files
brc-ddcuebit
andcommitted
fix(Url): observe route.path in useUrlQuerySync to preserve state during navigation (#579)
Co-authored-by: Cue <hello@cuebit.dev>
1 parent fadd75d commit ad4bf02

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

lib/composables/Url.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import isEqual from 'lodash-es/isEqual'
2-
import { type MaybeRef, nextTick, unref, watch } from 'vue'
2+
import { type MaybeRef, computed, nextTick, unref, watch } from 'vue'
33
import { type LocationQuery, useRoute, useRouter } from 'vue-router'
44

55
export interface UseUrlQuerySyncOptions {
@@ -21,13 +21,22 @@ export function useUrlQuerySync(
2121
const route = useRoute()
2222
const router = useRouter()
2323

24+
const routeQuery = computed(() => ({
25+
path: route.path,
26+
query: route.query
27+
}))
28+
2429
const flattenedDefaultState = flattenObject(unref(state))
2530

2631
let isSyncing = false
2732

2833
watch(
29-
() => route.query,
30-
async () => {
34+
routeQuery,
35+
async (to, from) => {
36+
if (from && from.path !== to.path) {
37+
return
38+
}
39+
3140
if (!isSyncing) {
3241
isSyncing = true
3342
await setState()

0 commit comments

Comments
 (0)