-
-
Notifications
You must be signed in to change notification settings - Fork 253
perf: search improvements #1431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7fe92f7
1cd2348
92e4588
ee53118
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { normalizeSearchParam } from '#shared/utils/url' | ||
| import { debounce } from 'perfect-debounce' | ||
|
|
||
| // Pages that have their own local filter using ?q | ||
| const pagesWithLocalFilter = new Set(['~username', 'org']) | ||
|
|
||
| export function useGlobalSearch() { | ||
| const { searchProvider } = useSearchProvider() | ||
| const searchProviderValue = computed(() => { | ||
| const p = normalizeSearchParam(route.query.p) | ||
| if (p === 'npm' || searchProvider.value === 'npm') return 'npm' | ||
| return 'algolia' | ||
| }) | ||
| const router = useRouter() | ||
| const route = useRoute() | ||
| const searchQuery = useState<string>('search-query', () => { | ||
| if (pagesWithLocalFilter.has(route.name as string)) { | ||
| return '' | ||
| } | ||
| return normalizeSearchParam(route.query.q) | ||
| }) | ||
|
|
||
| // clean search input when navigating away from search page | ||
| watch( | ||
| () => route.query.q, | ||
| urlQuery => { | ||
| const value = normalizeSearchParam(urlQuery) | ||
| if (!value) searchQuery.value = '' | ||
| }, | ||
| ) | ||
| const updateUrlQueryImpl = (value: string, provider: 'npm' | 'algolia') => { | ||
| const isSameQuery = route.query.q === value && route.query.p === provider | ||
| // Don't navigate away from pages that use ?q for local filtering | ||
| if (pagesWithLocalFilter.has(route.name as string) || isSameQuery) { | ||
| return | ||
|
Comment on lines
+31
to
+35
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve route-level provider overrides when updating the URL. Right now the setter passes 💡 Suggested fix- const isSameQuery = route.query.q === value && route.query.p === provider
+ const currentQuery = normalizeSearchParam(route.query.q)
+ const currentProvider = normalizeSearchParam(route.query.p) === 'npm' ? 'npm' : 'algolia'
+ const isSameQuery = currentQuery === value && currentProvider === provider
@@
- if (!updateUrlQuery.isPending()) {
- updateUrlQueryImpl(value, searchProvider.value)
- }
- updateUrlQuery(value, searchProvider.value)
+ const effectiveProvider = searchProviderValue.value
+ if (!updateUrlQuery.isPending()) {
+ updateUrlQueryImpl(value, effectiveProvider)
+ }
+ updateUrlQuery(value, effectiveProvider)Also applies to: 64-71 |
||
| } | ||
|
|
||
| if (route.name === 'search') { | ||
| router.replace({ | ||
| query: { | ||
| ...route.query, | ||
| q: value || undefined, | ||
| p: provider === 'npm' ? 'npm' : undefined, | ||
| }, | ||
| }) | ||
| return | ||
| } | ||
| router.push({ | ||
| name: 'search', | ||
| query: { | ||
| q: value, | ||
| p: provider === 'npm' ? 'npm' : undefined, | ||
| }, | ||
| }) | ||
| } | ||
| const updateUrlQuery = debounce(updateUrlQueryImpl, 250) | ||
|
|
||
| function flushUpdateUrlQuery() { | ||
| updateUrlQuery.flush() | ||
| } | ||
|
Comment on lines
+58
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Submitting a stable query can no‑op.
💡 Suggested fix function flushUpdateUrlQuery() {
- updateUrlQuery.flush()
+ if (updateUrlQuery.isPending()) {
+ updateUrlQuery.flush()
+ return
+ }
+ updateUrlQueryImpl(searchQuery.value, searchProviderValue.value)
} |
||
|
|
||
| const searchQueryValue = computed({ | ||
| get: () => searchQuery.value, | ||
| set: async (value: string) => { | ||
| searchQuery.value = value | ||
|
|
||
| // Leading debounce implementation as it doesn't work properly out of the box (https://github.com/unjs/perfect-debounce/issues/43) | ||
| if (!updateUrlQuery.isPending()) { | ||
| updateUrlQueryImpl(value, searchProvider.value) | ||
| } | ||
| updateUrlQuery(value, searchProvider.value) | ||
| }, | ||
| }) | ||
| return { model: searchQueryValue, provider: searchProviderValue, flushUpdateUrlQuery } | ||
| } | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.