diff --git a/.changeset/orange-snakes-shout.md b/.changeset/orange-snakes-shout.md new file mode 100644 index 0000000000..73897f9d1e --- /dev/null +++ b/.changeset/orange-snakes-shout.md @@ -0,0 +1,6 @@ +--- +'skeleton': patch +'@shopify/hydrogen': patch +--- + +Add JSdoc to `getSelectedProductOptions` utility and cleanup the skeleton implementation diff --git a/packages/hydrogen/src/product/VariantSelector.ts b/packages/hydrogen/src/product/VariantSelector.ts index 779a8685ba..4605c0458c 100644 --- a/packages/hydrogen/src/product/VariantSelector.ts +++ b/packages/hydrogen/src/product/VariantSelector.ts @@ -133,6 +133,26 @@ export function VariantSelector({ type GetSelectedProductOptions = (request: Request) => SelectedOptionInput[]; +/** + * Extract searchParams from a Request instance and return an array of selected options. + * @param request - The Request instance to extract searchParams from. + * @returns An array of selected options. + * @example Basic usage: + * ```tsx + * + * import {getSelectedProductOptions} from '@shopify/hydrogen'; + * + * // Given a request url of `/products/product-handle?color=red&size=large` + * + * const selectedOptions = getSelectedProductOptions(request); + * + * // selectedOptions will equal: + * // [ + * // {name: 'color', value: 'red'}, + * // {name: 'size', value: 'large'} + * // ] + * ``` + **/ export const getSelectedProductOptions: GetSelectedProductOptions = ( request, ) => { diff --git a/templates/skeleton/app/routes/products.$handle.tsx b/templates/skeleton/app/routes/products.$handle.tsx index 0123b3309d..3c0636cd47 100644 --- a/templates/skeleton/app/routes/products.$handle.tsx +++ b/templates/skeleton/app/routes/products.$handle.tsx @@ -26,7 +26,7 @@ import type { } from '@shopify/hydrogen/storefront-api-types'; import {getVariantUrl} from '~/lib/variants'; -export const meta: MetaFunction = ({data, location}) => { +export const meta: MetaFunction = ({data}) => { return [{title: `Hydrogen | ${data?.product.title ?? ''}`}]; }; @@ -34,25 +34,13 @@ export async function loader({params, request, context}: LoaderFunctionArgs) { const {handle} = params; const {storefront} = context; - const selectedOptions = getSelectedProductOptions(request).filter( - (option) => - // Filter out Shopify predictive search query params - !option.name.startsWith('_sid') && - !option.name.startsWith('_pos') && - !option.name.startsWith('_psq') && - !option.name.startsWith('_ss') && - !option.name.startsWith('_v') && - // Filter out third party tracking params - !option.name.startsWith('fbclid'), - ); - if (!handle) { throw new Error('Expected product handle to be defined'); } // await the query for the critical product data const {product} = await storefront.query(PRODUCT_QUERY, { - variables: {handle, selectedOptions}, + variables: {handle, selectedOptions: getSelectedProductOptions(request)}, }); if (!product?.id) {