fix(theme-default/algolia): avoid creating multiple algolia searches (#292)

pull/421/head
Eduardo San Martin Morote 3 years ago committed by GitHub
parent 58663bbd02
commit 389e863b4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -88,8 +88,7 @@ const pageClasses = computed(() => {
<AlgoliaSearchBox <AlgoliaSearchBox
v-if="theme.algolia" v-if="theme.algolia"
:options="theme.algolia" :options="theme.algolia"
:multilang="isMultiLang" :multilang="!!theme.locales"
:key="site.lang"
/> />
</slot> </slot>
</template> </template>

@ -52,11 +52,29 @@ function update(options: any) {
const { lang } = useData() const { lang } = useData()
function initialize(userOptions: any) { // if the user has multiple locales, the search results should be filtered
// if the user has multiple locales, the search results should be filtered // based on the language
// based on the language const facetFilters: string[] = props.multilang
const facetFilters = props.multilang ? ['language:' + lang.value] : [] ? ['language:' + lang.value]
: []
if (props.options.searchParameters?.facetFilters) {
facetFilters.push(...props.options.searchParameters.facetFilters)
}
watch(
lang,
(newLang, oldLang) => {
const index = facetFilters.findIndex(
(filter) => filter === 'language:' + oldLang
)
if (index > -1) {
facetFilters.splice(index, 1, 'language:' + newLang)
}
}
)
function initialize(userOptions: any) {
docsearch( docsearch(
Object.assign({}, userOptions, { Object.assign({}, userOptions, {
container: '#docsearch', container: '#docsearch',
@ -64,9 +82,7 @@ function initialize(userOptions: any) {
searchParameters: Object.assign({}, userOptions.searchParameters, { searchParameters: Object.assign({}, userOptions.searchParameters, {
// pass a custom lang facetFilter to allow multiple language search // pass a custom lang facetFilter to allow multiple language search
// https://github.com/algolia/docsearch-configs/pull/3942 // https://github.com/algolia/docsearch-configs/pull/3942
facetFilters: facetFilters.concat( facetFilters
userOptions.searchParameters?.facetFilters || []
)
}), }),
navigator: { navigator: {

Loading…
Cancel
Save