Skip to content

Commit 003f95b

Browse files
authored
fix: debounce navigation to the search page (#293)
1 parent 2ddfa03 commit 003f95b

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

app/components/AppHeader.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<script setup lang="ts">
2+
import { debounce } from 'perfect-debounce'
3+
24
withDefaults(
35
defineProps<{
46
showLogo?: boolean
@@ -22,13 +24,17 @@ const showSearchBar = computed(() => {
2224
return route.name !== 'search' && route.name !== 'index'
2325
})
2426
25-
async function handleSearchInput() {
27+
const debouncedNavigate = debounce(async () => {
2628
const query = searchQuery.value.trim()
2729
await router.push({
2830
name: 'search',
2931
query: query ? { q: query } : undefined,
3032
})
3133
searchQuery.value = ''
34+
}, 100)
35+
36+
async function handleSearchInput() {
37+
debouncedNavigate()
3238
}
3339
3440
onKeyStroke(',', e => {

app/pages/index.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
<script setup lang="ts">
2+
import { debounce } from 'perfect-debounce'
3+
24
const router = useRouter()
35
const searchQuery = ref('')
46
const searchInputRef = useTemplateRef('searchInputRef')
57
const { focused: isSearchFocused } = useFocus(searchInputRef)
68
7-
function handleSearch() {
9+
const debouncedNavigate = debounce(() => {
810
router.push({
911
path: '/search',
1012
query: searchQuery.value.trim() ? { q: searchQuery.value.trim() } : undefined,
1113
})
14+
}, 250)
15+
16+
function handleSearch() {
17+
debouncedNavigate()
1218
}
1319
1420
useSeoMeta({

0 commit comments

Comments
 (0)