Sitecore Search witn Next.js

Here, we will build a sample of Sitecore Search from the initial setup of Next.js. Since XM Cloud’s Headless SXA is currently provided based on the Next.js Pages Router, we will also create the sample here using the Pages router.

This time, we will create a search site for both Japanese and English content.

Create a Next.js Project

This time, we will select version 14.2.18, which is as close as possible to the version of Next.js used by XM Cloud at the time of writing this document.

sh
Image

Add Packages

Install the packages provided by the Sitecore Search SDK. The procedure for this is provided on the following pages.

Refer to the documentation and proceed as follows.

sh
sh
sh
.sc-search-settings.json
package.json

This completes the package settings required for Sitecore Search.

Implement Language Switching

Create a Language Switching Component

This sample will create a search service for English and Japanese content.

First, define the language definitions to be used on the site in the src/data/locales.ts file.

src/data/locales.ts

Next, create the src/contexts/languageContext.ts file to share the context so that language settings can be shared across the application.

src/contexts/languageContext.ts

Also, create the src/lib/useLocalStorage.ts file to store language information using the browser’s local storage.

src/lib/useLocalStorage.ts

Finally, create the src/components/LocaleSelector.tsx file as a language

src/components/LocaleSelector.tsx

With this, the component setup is complete.

Implementing the Component

Next, we will implement the created language switcher component to make it functional. First, we will implement the created components in src/pages/_app.tsx. Below is the modified code.

src/pages/_app.tsx

To display it on the actual page, change the content of the top page to the following code.

src/pages/index.tsx

The execution result will be as follows, and when you switch the language, the selected language information will be saved in the local storage under lang.

Image

Adding the First Widgets

Now that the Next.js side is ready, we will create a search using the Widget.

Adding PreviewSearch

First, we will implement the PreviewSearch Widget on the top page.

sh
Image
          • index.tsx
      • index.tsx
tailwind.config.ts

With this, the PreviewSearch Widget is ready.

Adding Widget Settings

To use the created Widget in Next.js, proceed with the following code changes.

Create a .env.local file and set the values as environment variables to connect to Sitecore Search.

.env.local

Once the file is ready, add the necessary code to src/pages/_app.tsx.

The result is as follows.

src/pages/_app.tsx

With this, the widget is ready to be placed. First, place the widget in src/pages/index.tsx.

src/pages/index.tsx

The execution result is as follows. Since no stylesheets have been adjusted, the search results are displayed vertically, but they are working correctly.

Image

Creating the Header

To create the header for this sample site, we will place a logo on the top left, a search box in the center, and a language switcher on the right.

Adding the Header Component

First, we will place the following component for the header.

src/components/Header.tsx

Next, we will modify the Next.js file to load the Header in src/pages/_app.tsx.

src/pages/_app.tsx

Next, we will remove the PreviewSearch added to the src/pages/index.tsx page.

src/pages/index.tsx

Finally, we will change the width of the PreviewSearch from a fixed 800 to a flexible size.

src/widgets/PreviewSearchBasic/index.tsx
Image

Adding Dark Mode

The Sitecore Search Widget supports dark mode. Therefore, we will also enable dark mode for this Next.js sample.

powershell
tailwind.config.ts
src/pages/_app.tsx
src/components/mode.tsx
src/components/Header.tsx

The button now toggles the mode as shown below.

Image

We will add a page to display the search results instead of using the PreviewSearch search box. We will copy the src/pages/index.tsx file and create src/pages/search/index.tsx.

Modifying PreviewSearch

The PreviewSearch widget we implemented so far displays search results using the keywords entered in the input box. We will change this to navigate to the next page using a form tag.

First, add the form tag.

src/widgets/PreviewSearchBasic/index.tsx

Next, implement the handleSubmit function to handle form submission as follows. Note that some imports are added, so the code is shown with those parts as well.

src/widgets/PreviewSearchBasic/index.tsx

With the above changes, we can now pass the keyword to the search results page. To confirm the search keyword, modify the copied file src/pages/search/index.tsx.

src/pages/search/index.tsx

We can now retrieve the keyword on the /search page.

Image

Adding SearchResult

In this sample page, we will add the Basic Search Results widget provided by the SDK.

To add the widget, run the following command.

sh

Select the SearchResults widget.

Image

The widget and the necessary files for the widget will be added.

          • index.tsx
          • index.tsx
          • index.tsx
          • index.tsx
          • index.tsx
          • index.tsx
          • index.tsx
      • index.tsx

To use the added widget on the Search page, modify the code as follows:

src/pages/search/index.tsx

Actually, enter the keyword and execute it. At this stage, the result is 0. The error screen shows the message sorting option 'featured_desc' not configured.

Image

The reason is that the Sitecore Search display order does not include this featured order setting, so the results cannot be retrieved.

Image

Since the purpose is to make it work before creating a new one, we will change the order to title_ascending.

src/widgets/SearchResults/index.tsx

After making the change, the search results were displayed.

Image

Fixing Build Errors

The search is now working as above, but when you run the build, an error is displayed.

Image

This is because the widget interface provided by default uses any. We will fix the following files.

src/widgets/components/ArticleHorizontalCard/index.tsx
src/widgets/components/Filter/index.tsx
examples/sitecore-search-pages/src/widgets/components/SortOrder/index.tsx

After making the above changes, when you run the build again, this time an error occurs in the SearchResults widget. This can be resolved by making url and source_id required values.

src/widgets/SearchResults/index.tsx

This resolves the error and completes the build successfully.

Creating Content Pages

In the search site, we will configure it to display the target page when clicking on the search results. For this sample site, we will prepare a page to check the information held by Sitecore Search for the target page.

To configure the page to display using the ID, we first created the following page.

src/pages/detail/[id]/index.tsx

When accessing the URL http://localhost:3000/detail/test, the following result is displayed.

Image

This time, we will create the detail page by referring to the code of the Sitecore Search SDK. The sample code for the widget is as follows.

We have rewritten the page as follows.

src/pages/detail/[id]/index.tsx

Additionally, we added an export to ArticleModel to use it as an interface for search results on this page.

src/widgets/SearchResults/index.tsx

To use the search result images in Next.js, we added the following code to the next.config.mjs file.

next.config.mjs

As a result of the above work, it is now possible to display the results using the ID.

To access the result page, we will add the page transition when clicking on the items displayed in PreviewSearch and the same behavior to ArticleHorizontalCard displayed in SearchResults.

src/widgets/PreviewSearchBasic/index.tsx
src/widgets/components/ArticleHorizontalCard/index.tsx

With this, we were able to add links to the pages displaying the search results.

Summary

This time, we confirmed the steps to display the search results of Sitecore Search in a vanilla Next.js + Tailwind CSS environment. We proceeded with the steps to add widgets and display them in Next.js.

Regarding the code introduced here, there are parts where a footer is added, etc., but all the code is shared on the following site.

References