added new improved searchbar functionality#523
Conversation
Signed-off-by: antedotee <soniyadav2051982@gmail.com>
✅ Deploy Preview for krkn-chaos ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
Review Summary by QodoImplement custom Cmd+K search overlay with Lunr integration
WalkthroughsDescription• Replaced Docsy's offline search with custom Cmd+K overlay powered by Lunr index • Added live-as-you-type search with highlighted snippets and keyboard navigation • Implemented new search UI with breadcrumb paths and result excerpts • Integrated krkn-search.js for independent site search functionality Diagramflowchart LR
A["Docsy Offline Search"] -->|replaced| B["Custom Cmd+K Overlay"]
C["Lunr Index"] -->|reused| B
B -->|renders| D["Live Results with Snippets"]
E["krkn-search.js"] -->|powers| B
F["Keyboard Navigation"] -->|enhances| B
File Changes1. layouts/_partials/hooks/body-end.html
|
Code Review by Qodo
Context used 1. Stale results race
|
|
Have a look when free @paigerube14. |
| function onInput() { | ||
| var query = input.value.trim(); | ||
| clearTimeout(debounceTimer); | ||
|
|
||
| if (!query) { renderState(''); return; } | ||
| if (loadFailed) { failLoad(); return; } | ||
| if (!idx) { | ||
| loadIndex(); | ||
| renderState('<div class="krkn-search-msg">Loading search…</div>'); | ||
| return; | ||
| } | ||
| debounceTimer = setTimeout(function () { runSearch(query); }, DEBOUNCE_MS); | ||
| } | ||
|
|
||
| function runSearch(query) { | ||
| var token = ++queryToken; | ||
| var lunr = window.lunr; | ||
| var terms = tokenize(query); | ||
|
|
||
| var matches; | ||
| try { | ||
| matches = idx.query(function (q) { | ||
| terms.forEach(function (term) { | ||
| q.term(term, { boost: 100 }); | ||
| q.term(term, { wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING, boost: 10 }); | ||
| q.term(term, { editDistance: 2 }); | ||
| }); | ||
| }); | ||
| } catch (e) { | ||
| matches = []; | ||
| } | ||
| if (token !== queryToken) return; | ||
| renderResults(query, terms, matches); | ||
| } |
There was a problem hiding this comment.
1. Stale results race 🐞 Bug ≡ Correctness
runSearch() increments queryToken only when the debounced search begins, so if the user edits the query while an earlier search is running (before the next debounced runSearch fires), the earlier search can still render results that no longer match the current input.
Agent Prompt
### Issue description
`queryToken` is incremented inside `runSearch()`, not when the input changes. If the user types again while a previous `runSearch()` is executing (and before the next debounced `runSearch()` starts), the old search will still pass `token === queryToken` and render stale results.
### Issue Context
The debounce delays starting the next search, which leaves a window where the current query has changed but `queryToken` has not.
### Fix Focus Areas
- static/js/krkn-search.js[96-129]
### Suggested fix
- Increment a version/token in `onInput()` immediately when the query changes.
- Capture that token when scheduling the debounced work, and pass it into `runSearch(query, token)`.
- In `runSearch`, before calling `renderResults`, verify the token still matches the latest token **and** (optionally) the current `input.value.trim()` still equals the query being rendered.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
Shifted the searchbar position and now instead of just giving the section, it gives the exact place in where inside the section that keyword is written, making it more close to the docs-search functionality available on other sites.
Fixes #512