Load search index, when component is shown.
chrispreisler opened this issue · 2 comments
Hey,
I would like to load the search index, when the user opens a search overlay on a page. The overlay can be opened from different routes, so I can't use the autoFetch with defining pathes. As I understand your text right I can set the autoFetch to false and then load the search index in someway. Do you have an example for that or can point me in the right direction?
Thank you very much for your help in advance.
Best regards
Chris
That's correct - if you look at this line here: https://github.com/thetre97/gridsome-plugin-flexsearch/blob/2d166f5e7fce275ba7b2f8597b0a3ed416578c1b/gridsome.client.js#L10 that shows an example of how to fetch the json.
You could do something like:
import FlexSearch from 'flexsearch'
import cjson from 'compressed-json'
export default {
async mounted () {
const searchIndex = await fetch(`/flexsearch.json`).then(r => r.json()).then(j => cjson.decompress(j))
this.$search.import(searchIndex, { serialize: false })
}
}
Thank you so much. Included it right now and it works perfectly.