Skip to content

Commit

Permalink
fix: ensure unique component keys in Vue app
Browse files Browse the repository at this point in the history
Improve key generation logic in swapComponent to ensure unique keys when navigating between pages.
This fixes the failing tests by:
- Using timestamp + random number for non-preserved state
- Maintaining explicit handling of KeepAliveId
- Ensuring consistent behavior with preserveState
  • Loading branch information
杨伟杰 authored and 杨伟杰 committed Dec 28, 2024
1 parent 5c6ea61 commit 15d466b
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/vue3/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,16 @@ const App: InertiaApp = defineComponent({

const urlParams = new URLSearchParams(args.page.url.split('?')[1] || '')
const id = urlParams.get('KeepAliveId')

const componentName = (args.component as any)?.name || args.page.component
key.value = id ? `${componentName}-${id}` : (args.preserveState ? key.value : Date.now())

if (id) {
key.value = `${componentName}-${id}`
} else if (args.preserveState) {
key.value = key.value
} else {
// Use a combination of timestamp and random number to ensure uniqueness
key.value = `${Date.now()}-${Math.random()}`
}
},
})

Expand Down

0 comments on commit 15d466b

Please sign in to comment.