Skip to content

Commit

Permalink
fix(theme-default/algolia): avoid creating multiple algolia searches (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
posva authored Oct 7, 2021
1 parent 58663bb commit 389e863
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/client/theme-default/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ const pageClasses = computed(() => {
<AlgoliaSearchBox
v-if="theme.algolia"
:options="theme.algolia"
:multilang="isMultiLang"
:key="site.lang"
:multilang="!!theme.locales"
/>
</slot>
</template>
Expand Down
30 changes: 23 additions & 7 deletions src/client/theme-default/components/AlgoliaSearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,37 @@ function update(options: any) {
const { lang } = useData()
function initialize(userOptions: any) {
// if the user has multiple locales, the search results should be filtered
// based on the language
const facetFilters = props.multilang ? ['language:' + lang.value] : []
// if the user has multiple locales, the search results should be filtered
// based on the language
const facetFilters: string[] = props.multilang
? ['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(
Object.assign({}, userOptions, {
container: '#docsearch',
searchParameters: Object.assign({}, userOptions.searchParameters, {
// pass a custom lang facetFilter to allow multiple language search
// https://github.com/algolia/docsearch-configs/pull/3942
facetFilters: facetFilters.concat(
userOptions.searchParameters?.facetFilters || []
)
facetFilters
}),
navigator: {
Expand Down

0 comments on commit 389e863

Please sign in to comment.