使用IntestantSearch React插入中间件(见解)

发布于 2025-01-31 05:47:13 字数 2853 浏览 2 评论 0原文

因此,我们使用Algolia/InstantSearch/React/NextJ进行了功能搜索。但是Insights中间件目前尚未设置。

以下是实现的修剪版本,我们使用自定义小部件对结果的显示更加良好的控制。

我们将挂钩实现用于自定义小部件,

const { hits, sendEvent, ... } = useInfiniteHits(props)
import { useState } from 'react'
import algoliasearch from 'algoliasearch/lite'
import { InstantSearch, InstantSearchSSRProvider } from 'react-instantsearch-hooks-web'
import SearchBox from '@components/swatches/algolia/SearchBox'
import Hits from '@components/swatches/algolia/Hits'
import RefinementList from '@components/swatches/algolia/RefinementList'
import CurrentRefinements from '@components/swatches/algolia/CurrentRefinements'
import { getServerState } from 'react-instantsearch-hooks-server'
import Container from 'react-bootstrap/Container'
import Row from 'react-bootstrap/Row'
import Col from 'react-bootstrap/Col'
import { history } from 'instantsearch.js/es/lib/routers/index.js'
import styles from '@styles/page.module.scss'



const Page = ({ serverState, url }) => {

  const searchClient = algoliasearch(
    process.env.NEXT_PUBLIC_ALGOLIA_INDEX_ID,
    process.env.NEXT_PUBLIC_ALGOLIA_SEARCH_KEY
  )
  return (
        <div className={styles.wrapper}>
            <InstantSearchSSRProvider {...serverState}>
              <InstantSearch
                searchClient={searchClient}
                indexName={process.env.NEXT_PUBLIC_ALGOLIA_INDEX}
                routing={{
                  router: history({
                    getLocation: () =>
                      typeof window === 'undefined' ? new URL(url) : window.location,
                  }),
                }}
              >

                <Container fluid="lg">
                  <div className="mb-3">
                    <SearchBox />
                  </div>
                  <CurrentRefinements />
                  <Hits />
                </Container>
              </InstantSearch>
            </InstantSearchSSRProvider>
        </div>
  )
}

export default Page

export async function getServerSideProps({ req, res, resolvedUrl}) {

  const protocol = req.headers.referer?.split('://')[0] || 'https';
  const url = `${protocol}://${req.headers.host}${req.url}`;
  const serverState = await getServerState(<Page url={url} />);

  return {
    props: {
      serverState,
      url,
    },
  }
}

所以我的问题是,我们在哪里可以将洞察力中间件连接起来?

阅读文档,( https:https://www.algolia。 com/doc/api greference/widgets/intestantsearch/react-hooks/)我并不是100%确定从哪里开始。我找不到InstantSearch React文档中的任何地方,无论如何它都可以在其中参考以配置这种东西。

我只是直接在API上而不是使用IntestantSearch触发事件,我是否会更好?

谢谢

So, we have a functional search with Algolia/Instantsearch/React/Nextjs. But the Insights middleware is currently not setup.

Below is a trimmed version of the implementation, we use custom widgets to have more fine control over the display of results.

We use the hooks implementation for the custom widgets like so

const { hits, sendEvent, ... } = useInfiniteHits(props)
import { useState } from 'react'
import algoliasearch from 'algoliasearch/lite'
import { InstantSearch, InstantSearchSSRProvider } from 'react-instantsearch-hooks-web'
import SearchBox from '@components/swatches/algolia/SearchBox'
import Hits from '@components/swatches/algolia/Hits'
import RefinementList from '@components/swatches/algolia/RefinementList'
import CurrentRefinements from '@components/swatches/algolia/CurrentRefinements'
import { getServerState } from 'react-instantsearch-hooks-server'
import Container from 'react-bootstrap/Container'
import Row from 'react-bootstrap/Row'
import Col from 'react-bootstrap/Col'
import { history } from 'instantsearch.js/es/lib/routers/index.js'
import styles from '@styles/page.module.scss'



const Page = ({ serverState, url }) => {

  const searchClient = algoliasearch(
    process.env.NEXT_PUBLIC_ALGOLIA_INDEX_ID,
    process.env.NEXT_PUBLIC_ALGOLIA_SEARCH_KEY
  )
  return (
        <div className={styles.wrapper}>
            <InstantSearchSSRProvider {...serverState}>
              <InstantSearch
                searchClient={searchClient}
                indexName={process.env.NEXT_PUBLIC_ALGOLIA_INDEX}
                routing={{
                  router: history({
                    getLocation: () =>
                      typeof window === 'undefined' ? new URL(url) : window.location,
                  }),
                }}
              >

                <Container fluid="lg">
                  <div className="mb-3">
                    <SearchBox />
                  </div>
                  <CurrentRefinements />
                  <Hits />
                </Container>
              </InstantSearch>
            </InstantSearchSSRProvider>
        </div>
  )
}

export default Page

export async function getServerSideProps({ req, res, resolvedUrl}) {

  const protocol = req.headers.referer?.split('://')[0] || 'https';
  const url = `${protocol}://${req.headers.host}${req.url}`;
  const serverState = await getServerState(<Page url={url} />);

  return {
    props: {
      serverState,
      url,
    },
  }
}

So my question is, where do we hook up the insights middleware for this specific implementation?

Reading the docs, (https://www.algolia.com/doc/api-reference/widgets/instantsearch/react-hooks/) I'm not really 100% sure where to start. I can't find anywhere in the instantsearch react docs where it references anyway to configure that sort of thing.

Am I better of just firing events at the API directly instead of with InstantSearch?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人疚 2025-02-07 05:47:13

诀窍是使用InstantSearch使用useInstantsearch

const instantSearch = useInstantSearch();
instantSearch.use(middleware)

docs 应该告诉您该怎么做。

The trick is finding the InstantSearch instance using useInstantSearch:

const instantSearch = useInstantSearch();
instantSearch.use(middleware)

The docs should tell you what to do from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文