Skip to main content

Collection URL parameters

The collection URL parameter functions read collection filters and sort from the URL and write them back, with the same parameter names that Liquid themes use. Use the functions to read filters and sort in a loader, name filter form fields, and build sort options.

Import parseCollectionParams, serializeCollectionParams, normalizeCollectionSearch, getSortByValue, and parseSortByValue from @shopify/hydrogen.


Reads collection filters and sort from URL search params. Pass the result to your Storefront API collection query.

The function reads Liquid-compatible filter.p.* and filter.v.* keys and the sort_by param.

A URL that sets availability to both in stock and out of stock produces no availability filter. The function recognizes the best-selling and manual sort values, plus created, price, and title with an ascending or descending suffix. For other sort values, the sort key is undefined. The parseSortByValue function also recognizes relevance, collection default, and ID.

Anchor to searchParams
searchParams
URLSearchParams
required

URL search params to read.

The filters, sort key, and sort direction from the URL.

Examples

TypeScript

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

declare const storefrontClient: {
graphql(query: string, options: { variables: Record<string, unknown> }): Promise<unknown>;
};
declare const COLLECTION_QUERY: string;

export async function loader({ request, params }: { request: Request; params: { handle: string } }) {
const url = new URL(request.url);
const browse = parseCollectionParams(url.searchParams);

const data = await storefrontClient.graphql(COLLECTION_QUERY, {
variables: {
handle: params.handle,
first: 24,
filters: browse.filters.length > 0 ? browse.filters : undefined,
sortKey: browse.sortKey,
reverse: browse.reverse || undefined,
},
});

return { data, dataSearch: url.searchParams.toString() };
}

Anchor to Parameter serializationParameter serialization

Converts collection filters and sort to URL search params with Liquid-compatible keys. Use the result to build collection URLs.

The result holds only the filter.* and sort_by keys. The function leaves out sort_by for the collection's default sort.

Category filters convert to filter.p.category, which parseCollectionParams doesn't read. A price filter with a minimum and a maximum produces two params. To build a filter checkbox, parse a filter value's input string and convert the parsed filter. The resulting entries give the checkbox name and value.

Anchor to state
state
Pick<, "reverse" | "filters" | "sortKey">
required

The filters, sort key, and sort direction to convert.

URLSearchParams

URL search params that hold the filters and sort.

Examples

TypeScript

import { serializeCollectionParams, type ProductFilter } from "@shopify/hydrogen";

export function filterValueInputParamEntries(input: string) {
let filter: ProductFilter;
try {
filter = JSON.parse(input) as ProductFilter;
} catch {
return [];
}

return Array.from(
serializeCollectionParams({ filters: [filter], sortKey: undefined, reverse: false }),
([name, value]) => ({ name, value }),
);
}

Anchor to Search string normalizationSearch string normalization

Removes a leading ? from a search string. Normalize search strings before you compare them.

string
required

The search string from your router, with or without a leading ?.

The search string without a leading ?.

Examples

TypeScript

import { normalizeCollectionSearch, type CollectionStore } from "@shopify/hydrogen";

export function syncStore(store: CollectionStore, urlSearch: string, dataSearch: string) {
const normalizedUrlSearch = normalizeCollectionSearch(urlSearch);
const params = new URLSearchParams(normalizedUrlSearch);

if (!store.matchesParams(params)) {
store.syncFromParams(params);
}

if (normalizeCollectionSearch(dataSearch) === normalizedUrlSearch) {
store.settle();
}
}

Converts a Storefront API sort key and direction to a Liquid-compatible sort_by value, such as price-ascending or best-selling.

Only the price, title, created, and ID sort keys get a direction suffix. Other sort keys, such as best selling, ignore the direction.

Anchor to sortKey
sortKey
ProductCollectionSortKeys
required

Storefront API product collection sort key.

Anchor to reverse
reverse
boolean
required

true for descending, false for ascending.

The value for the sort_by URL param.

Converts a Liquid-compatible sort_by value to a Storefront API sort key and direction. The getSortByValue function does the opposite conversion.

For unrecognized values, the sort key is undefined.

Anchor to value
value
string
required

A sort_by value, such as price-ascending or best-selling.

{ sortKey: ProductCollectionSortKeys | undefined; reverse: boolean; }

The sort key and reverse, which reads true for descending values.

Examples

TypeScript

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

export const COLLECTION_SORT_OPTIONS = [
{ label: "Featured", value: "" },
{ label: "Best selling", value: getSortByValue("BEST_SELLING", false) },
{ label: "Alphabetically, A-Z", value: getSortByValue("TITLE", false) },
{ label: "Alphabetically, Z-A", value: getSortByValue("TITLE", true) },
{ label: "Price, low to high", value: getSortByValue("PRICE", false) },
{ label: "Price, high to low", value: getSortByValue("PRICE", true) },
{ label: "Date, old to new", value: getSortByValue("CREATED", false) },
{ label: "Date, new to old", value: getSortByValue("CREATED", true) },
];

Was this page helpful?