Skip to main content

Search result URLs

The search result URL functions build links for predictive search results and search pages. Use these functions to link each result to its page and keep the result's tracking parameters in the link.

Import getPredictiveSearchItemUrl and getSearchResultUrl from @shopify/hydrogen.


Builds the link for a predictive search result. A product, collection, page, or article result links to the resource's page with the search term. A query suggestion links to the search page and searches for the suggestion's text. Every link keeps the result's tracking parameters.

For a product, collection, page, or article, pass the route templates and the search term. The function throws an error without them.

required

The predictive search result to link to.

Anchor to options
options

Route templates, search term, and path settings for the link.

The link URL. The URL is relative unless you pass an absolute search path.

Examples

TypeScript

import {
createShopifyRouteTemplates,
getPredictiveSearchItemUrl,
type PredictiveSearchData,
} from "@shopify/hydrogen";

const routeTemplates = createShopifyRouteTemplates({});

export function getResultLinks({ term, items }: PredictiveSearchData) {
return {
products: items.products.map((product) =>
getPredictiveSearchItemUrl(product, { routes: routeTemplates, term }),
),
queries: items.queries.map((query) =>
getPredictiveSearchItemUrl(query, { routes: routeTemplates }),
),
};
}

Builds a search page URL from a path, a search term, and optional extra parameters. Use the function for search page links, such as a link to every result for the current term.

The search term and the extra parameters replace values with the same names in the base URL.

Anchor to input1
input1
required

The base URL, the search term, the extra and tracking parameters, and the search parameter name.

A relative URL, or an absolute URL when the base URL is absolute.

Examples

TypeScript

import { getSearchResultUrl } from "@shopify/hydrogen";

const url = getSearchResultUrl({
baseUrl: "/search",
term: "snow board",
params: { type: "product" },
});

console.log(url);

Was this page helpful?