Redirect Search to a Results Page
In most shops, search spans two pages. The Search Bar sits on the homepage or in the header, and a results page holds the Search Bar, the filters and the Product List. Set results-page-url on the search components, and a search started anywhere opens your results page with the search in the URL. The results page reads it and runs the search.
Because the search lives in the URL, shoppers can link to their results and reload them, and search engines can index them. Results shown in a modal can’t do any of that.
flowchart LR
A["Entry point<br/>homepage or header<br/>vviinn-text-search"] -->|"navigates to<br/>/search?query=shoes"| B["Results page<br/>vviinn-text-search, filters<br/>vviinn-results"]
B -->|reads query or searchId from URL| C[Search and Discovery Engine]
C -->|ranked products| B
Data flow for a text query. An image upload takes the same path, carrying searchId instead of query.
Before You Start
Section titled “Before You Start”You need:
vviinn-widgets2.211.0 or later, or 2.217.0 or later to rename the URL parameters (see Install the Widgets)- the read-only token (see Authentication)
- a results page at a fixed path on your site, such as
/search(Build a Search Page shows a complete one) - an active Text Search campaign, plus a Visual Search campaign if shoppers search by image
Set the Results Page URL
Section titled “Set the Results Page URL”Add results-page-url to every vviinn search component on both pages, always with the same value:
<!-- Homepage --><vviinn-text-search token="YOUR_READ_ONLY_TOKEN" results-page-url="/search"></vviinn-text-search>
<!-- Results page (/search) --><vviinn-text-search token="YOUR_READ_ONLY_TOKEN" results-page-url="/search"></vviinn-text-search><vviinn-results token="YOUR_READ_ONLY_TOKEN" results-page-url="/search"></vviinn-results>On any other page, the prop is the destination: a search navigates there. On the results page, it means the shopper has already arrived, so a later search updates the results in place instead of redirecting again. It also tells vviinn-results to read the search from the URL and run it on load. You don’t pass anything else to the results page.
A text search arrives as:
/search?query=shoesThe Search Bar on the results page shows the query from the URL.
The value can be a relative path or an absolute URL. A component counts as arrived when the path of the current URL matches the path in results-page-url. Trailing slashes are normalized and the query string is ignored, so /search, /search/ and https://your-shop.example/search are the same page. Besides token, the components need nothing else here: locale and mode have defaults.
Redirect Image Searches
Section titled “Redirect Image Searches”The Search Bar also redirects on image upload, with no second prop, as long as it offers image search. Set mode="extended" for that. The default, basic, is text only. An image search travels as an ID:
/search?searchId=abc-123A standalone Search Modal (vviinn-vps-button) or Search Modal Window (vviinn-vps-widget) also needs show-results-in-modal="false". Without it, the modal shows the results itself and never redirects.
<vviinn-vps-button token="YOUR_READ_ONLY_TOKEN" results-page-url="/search" show-results-in-modal="false"></vviinn-vps-button>To show the uploaded image on the results page, add a vviinn-cropper there. The URL carries only searchId. The browser keeps the image itself in localStorage (under vviinnCropperImageSrc, with vviinnDetectedObjects beside it), and the Cropper’s save-image-mode decides for how long:
once(default): read once on the results page, then clearedalways: kept across reloadsnever: not stored
The Search Modal’s other props are in its component reference.
Rename the URL Parameters
Section titled “Rename the URL Parameters”If query and searchId don’t fit the URLs you already use, rename them with results-page-params (2.217.0 or later). It takes a URLSearchParams-style string, so results-page-params="query=q&searchId=sid" turns the URLs above into:
/search?q=shoes&sid=abc-123Set the same value on every component that has results-page-url, including vviinn-results, which reads the URL. Otherwise they don’t agree on the parameter names.
Route Redirects in a Single-Page App
Section titled “Route Redirects in a Single-Page App”In a single-page app, a full page load discards client state. Listen for the vviinnRedirect event, call preventDefault(), and navigate with your own router instead.
Three kinds of redirect share the event, told apart by detail.source. text-search and visual-search are the results-page redirect. auto-suggest is a Query-to-URL Mapping set up in Panel, which points wherever it was configured, often outside your search flow. Routing it internally usually breaks it, so branch on the source instead of intercepting every redirect:
document.addEventListener("vviinnRedirect", (event) => { const { source, url } = event.detail; if (source === "auto-suggest") return; // let the widget follow it
event.preventDefault(); router.push(url);});The event also carries query for a text search and searchId for an image search. Its full payload is in the Events reference.
Handle the Redirect Yourself
Section titled “Handle the Redirect Yourself”For a URL shape the prop can’t produce, or a redirect that depends on your own state, leave out results-page-url and redirect yourself:
- On the entry page, listen for
vviinnTextSearchStarted(detail.query) andvviinnImageUploadFinished(detail.searchId), and navigate to your results page with the value in the URL. - On the results page, read the value from the URL and pass it on: a query through
search-queryonvviinn-text-search, a search ID throughsearch-idonvviinn-results. - For image searches, add
vviinn-cropperto show the uploaded image. Its defaultsave-image-mode="once"reuses the image the shopper just uploaded. To keep it across reloads, setsave-image-mode="always". - After each new search on the results page, update the URL with the History API (
pushState()orreplaceState()), so a reload shows the same results.
Pass the query to one component only. vviinn-results also accepts it as text-search-query, but setting both that and search-query describes the same search twice.
<!-- Entry page --><script> document.addEventListener("vviinnTextSearchStarted", ({ detail }) => { // Navigate to the results page with detail.query (text search) });
document.addEventListener("vviinnImageUploadFinished", ({ detail }) => { // Navigate to the results page with detail.searchId (image search) });</script><vviinn-text-search token="YOUR_READ_ONLY_TOKEN" mode="extended" locale="en-US"></vviinn-text-search>
<!-- Results page, text search: read the query from the URL --><vviinn-text-search token="YOUR_READ_ONLY_TOKEN" mode="extended" locale="en-US" search-query="RECEIVED_QUERY"></vviinn-text-search><vviinn-filters></vviinn-filters><vviinn-extended-filters locale="en-US"></vviinn-extended-filters><vviinn-results token="YOUR_READ_ONLY_TOKEN" locale="en-US"></vviinn-results>
<!-- Results page, image search: read the searchId from the URL --><vviinn-cropper token="YOUR_READ_ONLY_TOKEN" width="300"></vviinn-cropper><vviinn-text-search token="YOUR_READ_ONLY_TOKEN" mode="extended" locale="en-US"></vviinn-text-search><vviinn-results token="YOUR_READ_ONLY_TOKEN" search-id="RECEIVED_SEARCH_ID" locale="en-US"></vviinn-results>Check That It Works
Section titled “Check That It Works”Test from a page other than the results page:
- Search for a product. The browser opens the results page with
?query=and your query in the URL, and the Search Bar there shows the query. - Search again on the results page. The results update without a page load.
- If image search is on, upload a photo. The results page opens with
?searchId=in the URL.
Related
Section titled “Related”More on the parts this guide uses:
- Build a Search Page: a complete results page with filters and image search
- Events: the
vviinnRedirectpayload and the other response events - Text Search: the campaign settings behind the results, including Query-to-URL Mapping
- Troubleshooting: Widgets: searches that redirect when they shouldn’t, or don’t when they should