Skip to content

fix: add 300ms debounce to global search to prevent per-keystroke API calls#153

Merged
Mananwebdev160408 merged 1 commit into
Mananwebdev160408:mainfrom
Nakshathra-2808:fix/global-search-debounce-v2
Jul 4, 2026
Merged

fix: add 300ms debounce to global search to prevent per-keystroke API calls#153
Mananwebdev160408 merged 1 commit into
Mananwebdev160408:mainfrom
Nakshathra-2808:fix/global-search-debounce-v2

Conversation

@Nakshathra-2808

Copy link
Copy Markdown
Contributor

Problem

Fixes #139

The global search triggered a fetch to /api/global-search on every single keystroke with no debounce. Typing a 5-character word like "users" sent 5 separate network requests, causing unnecessary backend load and potential race conditions where slower responses could overwrite faster ones.

Root Cause

The useEffect watching search state called runGlobalSearch() immediately on every change with no delay. There was no mechanism to cancel or delay the request while the user was still typing.

Fix

Replaced the immediate fetch with a setTimeout of 300ms and returned clearTimeout as the effect cleanup:

const timerId = setTimeout(async () => {
  // fetch logic
}, 300);

return () => clearTimeout(timerId);
  • Every keystroke resets the 300ms timer via the cleanup function
  • The fetch only fires after the user stops typing for 300ms
  • Typing "users" quickly now sends 1 request instead of 5
  • setGlobalSearchLoading(true) is moved before the timeout so the loading spinner shows immediately for instant visual feedback

Files Changed

  • frontend/src/App.tsx — debounce added to global search useEffect only, no other logic changes

Testing

  1. Start dbportal with a valid DATABASE_URL
  2. Open DevTools → Network tab
  3. Type a word slowly one character at a time (e.g. "users")
  4. Confirm only 1 request fires to /api/global-search after you stop typing
  5. Confirm the loading spinner appears immediately on first keystroke
  6. Confirm clearing the search input clears results instantly without a network request

… calls

Global search fired a fetch on every keystroke with no debounce.
Typing a 5-character word sent 5 separate requests causing unnecessary
backend load and potential race conditions.

- Wrap fetch in setTimeout(300ms)
- Return clearTimeout as cleanup to cancel pending request on each keystroke
- Move setGlobalSearchLoading(true) before timeout for instant UI feedback

Fixes Mananwebdev160408#139
@Mananwebdev160408 Mananwebdev160408 merged commit f42095b into Mananwebdev160408:main Jul 4, 2026
1 check passed
@Nakshathra-2808

Copy link
Copy Markdown
Contributor Author

in the lables why gssoc approved it not showing i need it for my point please check it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Global search fires API request on every keystroke — missing debounce

2 participants