Skip to content

Commit

Permalink
feat(plugin-docsearch): add debounce on search (close #216) (#771)
Browse files Browse the repository at this point in the history
Co-authored-by: meteorlxy <meteor.lxy@foxmail.com>
  • Loading branch information
Mister-Hope and meteorlxy committed Mar 31, 2022
1 parent 6d3902e commit c82f735
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/@vuepress/plugin-docsearch/package.json
Expand Up @@ -39,6 +39,7 @@
"@vuepress/shared": "2.0.0-beta.35",
"@vuepress/utils": "2.0.0-beta.35",
"preact": "^10.6.4",
"ts-debounce": "^4.0.0",
"vue": "^3.2.28",
"vue-router": "^4.0.12"
},
Expand Down
Expand Up @@ -2,6 +2,7 @@ import type { DocSearchProps } from '@docsearch/react'
import { useSiteData } from '@vuepress/client'
import { resolveRoutePathFromUrl } from '@vuepress/shared'
import { createElement } from 'preact'
import { debounce } from 'ts-debounce'
import { useRouter } from 'vue-router'

const isSpecialClick = (event: MouseEvent): boolean =>
Expand Down Expand Up @@ -53,5 +54,14 @@ export const useDocsearchShim = (): Partial<DocSearchProps> => {
router.push(itemUrl)
},
},

// add search debounce
transformSearchClient: (searchClient) => {
const searchWithDebounce = debounce(searchClient.search, 500)
return {
...searchClient,
search: async (...args) => searchWithDebounce(...args),
}
},
} as Partial<DocSearchProps>
}
Expand Up @@ -14,7 +14,7 @@ export const docsearchPlugin: Plugin<DocsearchPluginOptions> = (
app.options.bundlerConfig.viteOptions,
{
optimizeDeps: {
exclude: ['@docsearch/js', 'preact'],
exclude: ['@docsearch/js', 'preact', 'ts-debounce'],
},
}
)
Expand Down

0 comments on commit c82f735

Please sign in to comment.