Create a search results page
You can use the Cloud SDK search package to build search experiencessearch experiences and capture eventsevents that occur inside them.
In this walkthrough, you'll create a simple search results page with a search field in a Next.js app. Specifically, you'll request search content from Sitecore Search, display it in the user interface, and capture events when a site visitor views the search results.
The walkthrough describes the typical workflow for creating search experiences. After completing it, you can tailor the requests and the presentation of the content to your own needs for this search results page, or build other types of search experiences.
This walkthrough describes how to:
-
Have a search results widget and a search results page published in Sitecore Search.
-
In your code editor, open the root folder of your Next.js app. This walkthrough was tested on Next.js version 15, both for the Pages Router and the App Router.
-
Install and initialize the Cloud SDK and the
eventsandsearchpackages. -
Optionally, install Tailwind CSS in your app. This walkthrough uses Tailwind CSS to stylize the user interface, but it is not required to complete the walkthrough.
Request search content
The first step to creating a search results page is to request search content from Sitecore Search. In Sitecore Search, widgetswidgets hold this content, so you will request data for a specific widget.
-
In your code editor, open the root folder of your Next.js app.
-
Depending on your router type:
-
If using the Pages Router - in the
pagesfolder, create a file calledsearch.tsx. -
If using the App Router - in the
appfolder, create a subfolder calledsearch. Then, in thesearchfolder, create a file calledpage.tsx.
-
-
To import everything from the
searchpackage that's required to request data from Sitecore Search:-
If using the Pages Router - paste the following code into
search.tsx: -
If using the App Router - paste the following code into
page.tsx:
-
-
Inside the default function, directly before the
returnstatement, paste the following code then save your changes:This code requests product data from Sitecore Search. Specifically, the code:
-
Creates an object called
widgetRequest. This object describes a search results widgetsearch results widget with the widget IDwidget ID of"rfkid_7"and is configured to request index documentsindex documents that belong to the Product entity in Sitecore Search. This widget is already set up in your instance of Sitecore Search to return search results. -
Runs the
getWidgetDatafunction to request data associated withwidgetRequest, and saves the data that Sitecore Search returns into theresponsevariable. -
Stores the returned data, if any, in the
productsstate variable.
-
-
Update the
returnstatement to return the following, then save your changes:This code iterates through the contents of the
productsstate variable and displays each product index document in the user interface. -
In your terminal, enter
npm run devto start your Next.js app, then open the page you created in your web browser. A list of products displays. This is the content you requested from Sitecore Search. -
Open your web browser's developer tools and on the Network tab, start recording network activity.
You'll use the Network tab while building search experiences to check the network requests you make to Sitecore.
-
In your web browser, reload the page and then find a network request made to
edge-platform.sitecorecloud.io/v1/search. This is the request you made for product index documents.
Capture events
After requesting search content and displaying it in the user interface, you update your code to capture events when your site visitor views the content.
The captured event data is sent to Sitecore and becomes available in Sitecore Search. This data lets you personalize search results and track the performance of your search experiences so you can refine and improve them.
To capture events:
-
In the file you worked with in the previous procedure, at the top of the file, add
widgetViewto the list of imports from thesearchpackage. You'll use this function in the next step to start tracking when the widget is viewed in your app. -
In the default function, in the Effect Hook, directly after
setProducts(currentProducts);, paste the following code:This code sends a widget view event every time the search results are viewed.
-
In your web browser, reload the page. When the webpage with the search experience loads, the widget view event triggers and is sent to Sitecore.
TipUse your web browser's developer tools to check the payload that is sent to Sitecore.
-
Optionally, to find the event in Sitecore Search:
-
In your web browser's developer tools, view the cookies for your app.
-
Find a cookie named
sc_{SitecoreEdgeContextId}, where{SitecoreEdgeContextId}is your Context ID. -
Copy the cookie value. This value is the browser IDbrowser ID. You'll use it to find the event in Sitecore Search.
Example:
a38b230c-11eb-4cf9-8d5d-274e9f344925​ -
In Sitecore Search > Developer Resources > Event Monitor, paste the browser ID in the UUID field, then click Start Monitoring.
-
In your web browser, reload the search results page. When the webpage loads, the widget view event triggers, is sent to Sitecore Search, and the Event Monitor displays it.
-
In Sitecore Search, check that the Event Monitor now lists events. These are the events you sent when you reloaded the search results page in your app.
-
Stylize the user interface
After capturing events, you continue updating your code to stylize the user interface in your app.
To stylize the user interface:
-
In the file you worked with in the previous procedure, directly after the list of imports and before the default function, paste the following code:
This code creates a
ProductListcomponent. -
Update the
returnstatement to return the following, then save your changes:This code renders the
ProductListcomponent, including all the product index documents that you request from Sitecore Search. -
In your web browser, reload the search results page. The webpage now loads with Tailwind CSS styles applied.
Add a search field
You have now successfully requested search content and displayed it in the user interface. Next, you add a search input field to the webpage to display search results in response to what the site visitor searches for.
To add a search field:
-
Depending on your router type:
-
If using the Pages Router - replace the contents of
search.tsxwith the following, then save your changes: -
If using the App Router - replace the contents of
page.tsxwith the following, then save your changes:
Note the following differences in the updated code compared to the previous one:
-
The search term, if any, is stored in the
searchTermstate variable. -
The search request logic now uses the search term, if the site visitor enters any, to request search content that matches that specific search term.
-
The search request logic is now separated into a new function called
fetchSearchRequest. This ensures that the logic can be run in multiple places in the code. -
A second Effect Hook requests new data when the search term changes.
-
In the
returnstatement, an input field is now also rendered.
-
-
In your web browser, reload the search results page. A search field now also displays on the page.
-
Enter a search term in the search field and note that the search results change.
-
In your web browser's developer tools, on the Network tab, check how the network requests change when you enter a different search term in the search field.
Next steps
You've now successfully created a search results page with a search field and started capturing events when the site visitor views the results.
Next, you can:
-
Learn about the concepts that will help you successfully build search experiences. For example, index documents and attributes, widgets, context, filters, and facets.
-
See examples of other types of search experiences to build, such as recommendations and search suggestions.
-
Learn more about tracking, what other events you can capture, and what the collected data can be used for.
-
Explore the reference documentation for the
searchpackage.