Hi guys!
Is there a way to create an instant-search single file component instead of an instant-search page in Nuxt?
I need to create an instant-search component because I have to replicate this on multiple pages
In the algolia tutorial there are only examples of how to create an instant-search page.
Thanks
Could you please explain what you mean by "instant-search single file component"? What do you want to share between these components?
I want to create a component that contains the search field with autocomplete in Nuxt and this component will be shared on multiple pages.
One of the problems of creating a component is about AsyncData that does not work with components
Hi @alekcarvalho.
Not being able to use asyncData from a component is rather a nuxt limitation, not specific to vue-instantsearch (see nuxt docs).
First, let me highlight that asyncData is necessary only if you want the server-side render search results (It's what's firing the search query to Algolia on the server). It sounds like you're building an autocomplete, so you might not need to server-side render search results.
However if you do need server-side rendered search results, I see 2 possible solutions:
You can encapsulate your search in a component. You'll still need asyncData on every page the component is being used but you can factor out asyncData implementation in a separate function to keep it short and DRY.
If you're using vuex, you can move asyncData business logic in a store action and trigger call the latter from nuxtServerInit. Then you can move your instantsearch components either in a layout.
Here is 2 threads covering this method:
I hope this helps!
I'll close this for now but feel free to reopen if you have any other question.
Most helpful comment
Hi @alekcarvalho.
Not being able to use asyncData from a component is rather a nuxt limitation, not specific to vue-instantsearch (see nuxt docs).
First, let me highlight that
asyncDatais necessary only if you want the server-side render search results (It's what's firing the search query to Algolia on the server). It sounds like you're building an autocomplete, so you might not need to server-side render search results.However if you do need server-side rendered search results, I see 2 possible solutions:
You can encapsulate your search in a component. You'll still need
asyncDataon every page the component is being used but you can factor out asyncData implementation in a separate function to keep it short and DRY.If you're using vuex, you can move
asyncDatabusiness logic in a store action and trigger call the latter from nuxtServerInit. Then you can move your instantsearch components either in alayout.Here is 2 threads covering this method:
I hope this helps!
I'll close this for now but feel free to reopen if you have any other question.