Skip to content

Commit d4b5d74

Browse files
committed
fix(useGlobalSearch): prevent duplicate handlers and streamline hydration recovery
- Updated logic to register reset/refocus handlers only for the content instance to avoid duplicates when useGlobalSearch is called from multiple locations. - Enhanced hydration recovery by ensuring it only occurs for the content instance, improving performance and consistency.
1 parent 85e5754 commit d4b5d74

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

app/composables/useGlobalSearch.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ export function useGlobalSearch(place: 'header' | 'content' = 'content') {
146146
// When navigating back to the homepage (e.g. via logo click from /search),
147147
// reset the global search state so the home input starts fresh and re-focus
148148
// the dedicated home search input.
149-
if (import.meta.client) {
149+
// Only register in one place (content instance) to avoid duplicate reset/refocus handlers
150+
// when useGlobalSearch is called from multiple callsites (e.g., Header/SearchBox and page components).
151+
if (import.meta.client && place === 'content') {
150152
watch(
151153
() => route.name,
152154
name => {
@@ -173,7 +175,8 @@ export function useGlobalSearch(place: 'header' | 'content' = 'content') {
173175
// On hydration, useState can reuse SSR payload (often empty), skipping initializer.
174176
// Recover fast-typed value from the focused input once on client mount.
175177
// Skip on pages with local filters to avoid importing local ?q state.
176-
if (import.meta.client) {
178+
// Only register in one place (content instance) to avoid duplicate hydration recovery.
179+
if (import.meta.client && place === 'content') {
177180
onMounted(() => {
178181
if (pagesWithLocalFilter.has(route.name as string)) return
179182
const focusedInputValue = getFocusedSearchInputValue()

0 commit comments

Comments
 (0)