View Algolia PWA Kit demo
Try the storefront demo with Autocomplete, InstantSearch, and Recommend.
Explore starter code
Browse the PWA Kit starter code and integration source on GitHub.
Integration with PWA Kit
The demo uses PWA Kit template extensibility with@salesforce/retail-react-app as its base template.
It includes only files that differ from the base template.
Copy the changes you need into your project or
use the demo repository as starter code.
For more information,
see Template extensibility.
Before you begin
This guide requires:- Algolia Search & Discovery cartridge version 25.3.0 or later.
- Salesforce’s
storefrontdatasample data. Thestorefrontdatarepository requires authenticated access. - The default Variation product record model.
- Node.js version 18 or later.
- npm 9 or later.
- If you intend to use Recommend components that require a trained model, train the corresponding model.
Installation
This guide covers@salesforce/retail-react-app version 4.0.0.
For other versions,
check the PWA Kit release notes for configuration differences.
Run the Algolia demo application
- Set up API access to your Salesforce B2C Commerce instance.
-
In Salesforce Business Manager, go to Merchant Tools > Algolia > Algolia.
In Additional Product Attributes, enter
color,size,colorVariations,masterID,lsImage,short_description,long_description,pricebooks,brand,name, andin_stock. These attributes determine which additional product fields the cartridge sends to Algolia. For more information, see Custom preferences. If you’re using other datasets or a custom indexing approach, adjust the attribute list for your implementation. - Index your products and content from your Salesforce B2C Commerce instance into Algolia.
-
Configure your indices in the Algolia dashboard:
- Create a Query Suggestions index.
- Configure facets for categories, brands, colors, and sizes for the primary product .
- Create replicas for sorting options.
- Create a rule for the primary product index to enable the rendering of product previews in the autocomplete section.
-
Import the following rule configuration using Enhanced > Rules > Import/Export.
JSON
-
Clone the Algolia PWA Kit demo repository from GitHub and go to the cloned repository:
Command lineCommand line
-
Install the dependencies:
Command line
-
Copy the example file to
config/algolia-config.json. Update the Algolia credentials and index names. -
Copy the example file to
config/commerce-api-config.json. Update the Salesforce B2C Commerce API credentials. -
Set the
PWA_KIT_SLAS_CLIENT_SECRETin your environment variables. To learn more about creating Shopper Login and API Access Service (SLAS) credentials, see Build Your Site in the PWA Kit documentation. -
Start the development server:
Command line
localhost:3000 using the credentials and index names in config/algolia-config.json.
Integrate Algolia into your existing PWA Kit project
- Index your products using the Algolia indexing jobs in Salesforce B2C Commerce.
-
Create an
algolia-config.jsonfile in your root folder or use and rename the example file. - Adjust your Commerce API and PWA Kit configuration as needed. For more information, see Configuration files.
-
Install the necessary dependencies to use Algolia styles in your PWA application:
Copy theCommand line
babel.config.jsandpostcss.config.jsfiles from the demo repository to your project’s root directory. -
Copy the
overrides/app/components/algoliadirectory from the demo repository to your project. -
Update your
overrides/app/routes.jsxfile to include the Algolia product list and detail pages:If you don’t need recommendation widgets on product detail pages, omit theJavaScriptProductDetailroute override.
algolia-config.json file
The following JavaScript excerpt shows the Algolia configuration structure.
For the complete JSON file,
see algolia-config.example.json.
JavaScript
appId: your Algolia application ID, which you can find in the Algolia dashboardapiKey: your Algolia API key, which you can find in the Algolia dashboardindices.querySuggestions: the Query Suggestions index nameindices.primary.label: the label for the default sorting optionindices.primary.value: the primary product index nameindices.replicas: a sorting options array and their corresponding index namesindices.contents: the content index nameindices.faq: the index containing FAQ records. The demo uses the content index. To search a separate FAQ index, update this value.indices.categories: the categories index name
InstantSearch implementation
The Algolia PWA Kit demo uses thealgolia-product-list page for the /search and /category/:categoryId routes.
It uses the following npm packages for building the InstantSearch experience:
InstantSearch in the demo provides the following features:
- Search results
- Category listings
- Filters for category, color, size, price, and brand
- Pagination
- Sorting by relevance or price
- Content search in a separate tab
- Color swatches on product listings
hierarchicalCategoryAttributes and
filterEls variables in the index.jsx file.
For example,
to filter by collection instead of brand,
set the AlgoliaCheckboxRefinements component’s attribute prop to "collection":
JavaScript
material,
add an AlgoliaCheckboxRefinements component to filterEls with attribute="material":
JavaScript
- For implementation details, see the product listing page and its partial components.
- For library documentation, see React InstantSearch.
Autocomplete implementation
The demo replaces the default search box with the Autocomplete widget. The Autocomplete component is in theoverrides/app/components/algolia/autocomplete directory.
The implementation in index.jsx and
uses the following plugins.
recentSearchesPlugin
Displays users’ recent searches with the createLocalStorageRecentSearchesPlugin.
The plugin stores recent searches in the browser’s local storage under the pwa-recent-searches key.
querySuggestionsPlugin
Provides Query Suggestions based on the user’s input.
It uses the createQuerySuggestionsPlugin and fetches suggestions from the indices.querySuggestions index in the configuration.
categoriesPlugin
Fetches and displays category suggestions.
It uses the getAlgoliaResults function to retrieve category results from the indices.categories index in the configuration.
brandsPlugin
Displays popular brands based on Algolia’s Trending Facets feature.
It uses the getAlgoliaFacets function to fetch trending brands from the indices.primary.value index in the configuration.
faqPlugin
Provides FAQ suggestions based on the user’s input.
It fetches results from the index specified by indices.faq using the getAlgoliaResults function.
popularPlugin
Displays popular searches based on Algolia’s Query Suggestions feature.
It uses the createQuerySuggestionsPlugin to retrieve popular searches from the indices.querySuggestions index in the configuration.
popularCategoriesPlugin
Fetches and displays popular categories based on Algolia’s facet values.
It uses the getAlgoliaFacets function to retrieve popular categories from the indices.primary.value index in the configuration.
productPlugin
Fetches and displays product suggestions based on the user’s input.
It uses the getAlgoliaResults function to retrieve product results from the indices.primary.value index in the configuration.
Customization and extensibility
ThepopularCategoriesPlugin helps display popular product categories based on search results.
Replace the hardcoded category images in popularCategoriesPlugin.tsx.
The quickAccessPlugin displays content configured through Algolia Rules.
Customize the configuration in quickAccessPlugin.tsx.
For demonstration purposes, the index.jsx file includes sample data.
Customize the Algolia component styles in style.css.
Recommend implementation
The demo includes Algolia Recommend components for product recommendations. These components are inoverrides/app/components/algolia/recommend.
The available Recommend components are:
- Frequently bought together: displays products frequently bought with the current product
- Looking similar: shows products that look like the current product
- Related products: displays products with similar attributes as the current product
- Trending items: displays trending items, based on a specific facet name and facet values
- Trending facets: shows trending facet values for a facet name
@algolia/recommend-react package to retrieve recommendations from Algolia.
The following are examples of the Recommend components.
Import them from the overrides/app/components/algolia/recommend directory for use anywhere in your project.
-
Frequently bought together:
JavaScript
-
Looking similar:
JavaScript
-
RelatedProducts:
JavaScript
-
TrendingItems:
JavaScript
-
TrendingFacets:
JavaScript
index.jsx file.
In the preceding examples:
productrepresents the current product object for displaying recommendations. It matches thecontext-objectin Einstein Recommendationisslots.facetNameandfacetValuespecify the facet name and value for fetching trending items.
recommendClientto specify the Algolia Recommend clientindexNameto specify the index to fetch recommendations fromheaderComponentanditemComponentfor rendering custom headers and items.
-
Import the desired Recommend component from the
overrides/app/components/algolia/recommenddirectory. - Configure the necessary props, such as the current product, click event handlers, and any extra customization options.
- Place the Recommend component in the appropriate location within your product detail page or any other relevant page.
Next steps
Customize the starter code for your storefront:- Integrate conversion events
- Customize the image logic in
popularCategoriesPlugin.tsx - Add localization and multi-site support
- Adjust the FAQ plugin configuration to use a specific index