nuxt-modules / algolia

🔎 Algolia module for Nuxt
https://algolia.nuxtjs.org/
MIT License
189 stars 35 forks source link

support SSR requests #117

Closed Baroshem closed 1 year ago

Baroshem commented 1 year ago

Is your feature request related to a problem? Please describe.

This topic have appeared some time ago but back then, I was not able to make it work. Today I managed to find a way to be able to send a request to Algolia from useAsyncData but it is not very elegant.

const { pending } = await useAsyncData('test', async () => {
  app.$algolia.transporter.requester = await (await import('@algolia/requester-node-http').then(lib => lib.default || lib)).createNodeHttpRequester()

  await search({ query: 'Samsung' })
})

As a part of this issue, I would like to create a new configuration entry nodeRequester that when set to true, it will create a new global object in nuxt App called algoliaNode so that later in useAlgoliaSearch composable users could do something like this:

const { pending } = await useAsyncData('test', () => search({ query: 'Samsung' , requester: 'node'}))

Describe the solution you'd like

Describe alternatives you've considered

Additional context

Baroshem commented 1 year ago

I have managed to make it work in a even simpler approach.

useAlgoliaSearch composable automatically finds out whether a request is made from SSR or client and based on that, an appriopriate requester is used.

So users can now use:

const { data } = await useAsyncData('test', () => search({ query: 'Samsung'  }))