docs: resolved occurrences of gatsby storefront (#3754)

This commit is contained in:
Shahed Nasser
2023-04-06 16:41:07 +03:00
committed by GitHub
parent 8277a74257
commit 3693322a83
5 changed files with 7 additions and 439 deletions
+3 -124
View File
@@ -287,134 +287,13 @@ To make sure the Next.js storefront properly displays the products in the search
![Search pop up in the Next.js storefront](https://res.cloudinary.com/dza7lstvk/image/upload/v1668000082/Medusa%20Docs/Algolia/1f9qqK6_c0z8zi.png)
### Add to Gatsby and React-Based Storefronts
### Add to Other Storefronts
This section covers adding the search UI to React-based storefronts. It uses the Gatsby storefront as an example, but you can use the same steps on any React-based framework.
:::tip
For other frontend frameworks, please check out [Algolias Integrations guide](https://www.algolia.com/developers/?ui-libraries&client-libraries) for steps based on your framework.
:::
In the directory that contains your storefront, run the following command to install the necessary dependencies:
```bash npm2yarn
npm install algoliasearch react-instantsearch-dom
```
Then, add the following environment variables:
```bash
GATSBY_ALGOLIA_APP_ID=<YOUR_APP_ID>
GATSBY_ALGOLIA_SEARCH_API_KEY=<YOUR_SEARCH_API_KEY>
GATSBY_SEARCH_INDEX_NAME=products
```
Where `<YOUR_APP_ID>` and `<YOUR_SEARCH_API_KEY>` are respectively the Application ID and Search-Only API Key on the [API Keys page](#retrieve-api-keys).
:::note
In Gatsby, environment variables that should be public and available in the browser are prefixed with `GATSBY_`. If youre using another React-based framework, you might need to use a different prefix to ensure these variables can be used in your code. Please refer to your frameworks documentation for help on this.
:::
Then, create the file `src/components/header/search.jsx` with the following content:
```jsx title=src/components/header/search.jsx
import {
Highlight,
Hits,
InstantSearch,
SearchBox,
connectStateResults,
} from "react-instantsearch-dom"
import React from "react"
import algoliasearch from "algoliasearch/lite"
const searchClient = algoliasearch(
process.env.GATSBY_ALGOLIA_APP_ID,
process.env.GATSBY_ALGOLIA_SEARCH_API_KEY
)
const Search = () => {
const Results = connectStateResults(
({ searchState, searchResults, children }) => {
return (
searchState && searchState.query &&
searchResults && searchResults.nbHits !== 0 ?
(
<div
className="absolute ...">
{children}
</div>
) : (
<div></div>
)
)
}
)
return (
<div className="relative">
<InstantSearch
indexName={process.env.GATSBY_SEARCH_INDEX_NAME}
searchClient={searchClient}>
<SearchBox submit={null} reset={null} />
<Results>
<Hits hitComponent={Hit} />
</Results>
</InstantSearch>
</div>
)
}
const Hit = ({ hit }) => {
return (
<div key={hit.id} className="relative">
<div className="hit-name">
<Highlight attribute="title" hit={hit} tagName="mark" />
</div>
</div>
)
}
export default Search
```
This file uses the dependencies you installed to show the search results. It also initializes Algolia using the environment variables you added.
:::note
If you named your environment variables differently based on your framework, make sure to rename them here as well.
:::note
Finally, import this file at the beginning of `src/components/header/index.jsx`:
```jsx title=src/components/header/index.jsx
import Search from "./search"
```
And add the `Search` component in the returned JSX before `RegionPopover`:
```jsx title=src/components/header/index.jsx
// ...
<div className="...">
<Search />
<RegionPopover regions={mockData.regions} />
</div>
// ...
```
If you run your Gatsby storefront while the Medusa backend is running, you should find a search bar in the header of the page. Try entering a query to search through the products in your store.
![Search bar in the Gatsby storefront](https://res.cloudinary.com/dza7lstvk/image/upload/v1668000097/Medusa%20Docs/Algolia/INtlcIo_jlh16x.png)
To integrate Algolia's search functionalities in your storefront, please refer to [Algolia's InstantSearch.js documentation](https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/js/). You'll find packages for different frontend frameworks and how you can use them.
---
## See Also
- [Deploy your Medusa backend](../../deployments/server/index.mdx)
- [Deploy your Gatsby storefront](../../deployments/storefront/deploying-gatsby-on-netlify.md)
- [Deploy your storefront](../../deployments/storefront/index.mdx)
+3 -126
View File
@@ -251,136 +251,13 @@ To make sure the Next.js storefront properly displays the products in the search
![Search Result on Next.js storefront](https://res.cloudinary.com/dza7lstvk/image/upload/v1668000298/Medusa%20Docs/MeiliSearch/gQVWvH2_datei5.png)
### Add to Gatsby and React-Based Storefronts
### Add to Other Storefronts
This section covers adding the search UI to React-based storefronts. It uses the Gatsby storefront as an example, but you can use the same steps on any React-based framework.
:::tip
For other frontend frameworks, please check out [MeiliSearchs Integrations guide](https://github.com/meilisearch/integration-guides) for steps based on your framework.
:::
In the directory that contains your storefront, run the following command to install the necessary dependencies:
```bash npm2yarn
npm install react-instantsearch-dom @meilisearch/instant-meilisearch
```
Then, add the following environment variables:
```bash
GATSBY_MEILISEARCH_HOST=<YOUR_MEILISEARCH_HOST>
GATSBY_MEILISEARCH_API_KEY=<YOUR_API_KEY>
GATSBY_SEARCH_INDEX_NAME=products
```
Make sure to replace `<YOUR_MEILISEARCH_HOST>` with your MeiliSearch host and `<YOUR_API_KEY>` with the API key you created as instructed in the [Storefront Prerequisites](#storefront-prerequisites) section.
:::caution
In Gatsby, environment variables that should be public and available in the browser are prefixed with `GATSBY_`. If youre using another React-based framework, you might need to use a different prefix to ensure these variables can be used in your code. Please refer to your frameworks documentation for help on this.
:::
Then, create the file `src/components/header/search.jsx` with the following content:
```jsx title=src/components/header/search.jsx
import {
Highlight,
Hits,
InstantSearch,
SearchBox,
connectStateResults,
} from "react-instantsearch-dom"
import React from "react"
import {
instantMeiliSearch,
} from "@meilisearch/instant-meilisearch"
const searchClient = instantMeiliSearch(
process.env.GATSBY_MEILISEARCH_HOST,
process.env.GATSBY_MEILISEARCH_API_KEY
)
const Search = () => {
const Results = connectStateResults(
({ searchState, searchResults, children }) => {
return (
searchState && searchState.query &&
searchResults && searchResults.nbHits !== 0 ?
(
<div
className="absolute ...">
{children}
</div>
) : (
<div></div>
)
)
}
)
return (
<div className="relative">
<InstantSearch
indexName={process.env.GATSBY_SEARCH_INDEX_NAME}
searchClient={searchClient}>
<SearchBox submit={null} reset={null} />
<Results>
<Hits hitComponent={Hit} />
</Results>
</InstantSearch>
</div>
)
}
const Hit = ({ hit }) => {
return (
<div key={hit.id} className="relative">
<div className="hit-name">
<Highlight attribute="title" hit={hit} tagName="mark" />
</div>
</div>
)
}
export default Search
```
This file uses the dependencies you installed to show the search results. It also initializes MeiliSearch using the environment variables you added.
:::caution
If you named your environment variables differently based on your framework, make sure to rename them here as well.
:::
Finally, import this file at the beginning of `src/components/header/index.jsx`:
```jsx title=src/components/header/index.jsx
import Search from "./search"
```
And add the `Search` component in the returned JSX before `RegionPopover`:
```jsx title=src/components/header/index.jsx
// ...
<div className="...">
<Search />
<RegionPopover regions={mockData.regions} />
</div>
// ...
```
If you run your Gatsby storefront while the Medusa backend and the MeiliSearch instance are running, you should find a search bar in the header of the page. Try entering a query to search through the products in your store.
![Search box in the header of the storefront](https://res.cloudinary.com/dza7lstvk/image/upload/v1668000317/Medusa%20Docs/MeiliSearch/ZkRgF2h_ytnpv9.png)
To integrate MeiliSearch's search functionalities in your storefront, please refer to [MeiliSearch's documentation](https://docs.meilisearch.com/learn/what_is_meilisearch/sdks.html#front-end-tools). They offer different tools that you can use based on the frontend framework of your storefront.
---
## See Also
- [Deploy your Medusa backend](../../deployments/server/index.mdx).
- [Deploy your Gatsby storefront](../../deployments/storefront/deploying-gatsby-on-netlify.md).
- [Deploy your storefront](../../deployments/storefront/index.mdx).