Skip to content

Commit c82f735

Browse files
feat(plugin-docsearch): add debounce on search (close vuepress#216) (vuepress#771)
Co-authored-by: meteorlxy <meteor.lxy@foxmail.com>
1 parent 6d3902e commit c82f735

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

packages/@vuepress/plugin-docsearch/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@vuepress/shared": "2.0.0-beta.35",
4040
"@vuepress/utils": "2.0.0-beta.35",
4141
"preact": "^10.6.4",
42+
"ts-debounce": "^4.0.0",
4243
"vue": "^3.2.28",
4344
"vue-router": "^4.0.12"
4445
},

packages/@vuepress/plugin-docsearch/src/client/composables/useDocsearchShim.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { DocSearchProps } from '@docsearch/react'
22
import { useSiteData } from '@vuepress/client'
33
import { resolveRoutePathFromUrl } from '@vuepress/shared'
44
import { createElement } from 'preact'
5+
import { debounce } from 'ts-debounce'
56
import { useRouter } from 'vue-router'
67

78
const isSpecialClick = (event: MouseEvent): boolean =>
@@ -53,5 +54,14 @@ export const useDocsearchShim = (): Partial<DocSearchProps> => {
5354
router.push(itemUrl)
5455
},
5556
},
57+
58+
// add search debounce
59+
transformSearchClient: (searchClient) => {
60+
const searchWithDebounce = debounce(searchClient.search, 500)
61+
return {
62+
...searchClient,
63+
search: async (...args) => searchWithDebounce(...args),
64+
}
65+
},
5666
} as Partial<DocSearchProps>
5767
}

packages/@vuepress/plugin-docsearch/src/node/docsearchPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const docsearchPlugin: Plugin<DocsearchPluginOptions> = (
1414
app.options.bundlerConfig.viteOptions,
1515
{
1616
optimizeDeps: {
17-
exclude: ['@docsearch/js', 'preact'],
17+
exclude: ['@docsearch/js', 'preact', 'ts-debounce'],
1818
},
1919
}
2020
)

0 commit comments

Comments
 (0)