|
| 1 | +import { getVariantIdParam, type StorefrontClient } from "@shopify/hydrogen"; |
| 2 | +import { NextResponse, type NextRequest } from "next/server"; |
| 3 | + |
| 4 | +import { VARIANT_DEEP_LINK_QUERY } from "./product-query"; |
| 5 | + |
| 6 | +/** Matches `getVariantIdParam`'s param — kept here to strip it on the way out. */ |
| 7 | +const VARIANT_PARAM = "variant"; |
| 8 | +const PRODUCT_PATHNAME = /^\/products\/[^/]+$/; |
| 9 | + |
| 10 | +/** Shopify's sentinel option for single-variant products — not a real choice. */ |
| 11 | +const DEFAULT_OPTION_NAME = "Title"; |
| 12 | +const DEFAULT_OPTION_VALUE = "Default Title"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Normalizes `?variant=<id>` deep links to the option-param URL the PDP reads. |
| 16 | + * |
| 17 | + * Shopify's own surfaces — Liquid storefronts, Shopping feeds, email campaigns, |
| 18 | + * paid ads, and Shop Pay — link to a product with a bare variant id. The PDP is |
| 19 | + * built around option params (`?Size=Large`), so without this those links land |
| 20 | + * on the default variant instead of the one the shopper clicked. |
| 21 | + * |
| 22 | + * This runs in `proxy.ts` rather than the page on purpose. Under Cache |
| 23 | + * Components the shell streams before a page-level `redirect()` can set a |
| 24 | + * status, so it degrades to a client-side redirect — which never runs for a |
| 25 | + * no-JS shopper, leaving them on a shell with no add-to-cart (F4). Redirecting |
| 26 | + * before framework routing keeps it a real HTTP redirect for everyone. |
| 27 | + * |
| 28 | + * Costs nothing on ordinary requests: the Storefront lookup only happens on a |
| 29 | + * `/products/*` URL that actually carries a well-formed `variant` param. |
| 30 | + * Anything unresolvable falls through and renders the default variant (F8) — |
| 31 | + * a stale id in an old feed should not 404. |
| 32 | + */ |
| 33 | +export async function variantDeepLinkRedirect( |
| 34 | + request: NextRequest, |
| 35 | + storefrontClient: StorefrontClient, |
| 36 | +): Promise<NextResponse | null> { |
| 37 | + const { pathname, searchParams } = request.nextUrl; |
| 38 | + if (!PRODUCT_PATHNAME.test(pathname)) return null; |
| 39 | + |
| 40 | + const variantId = getVariantIdParam({ searchParams }); |
| 41 | + if (!variantId) return null; |
| 42 | + |
| 43 | + const { data, errors } = await storefrontClient.graphql(VARIANT_DEEP_LINK_QUERY, { |
| 44 | + variables: { id: variantId }, |
| 45 | + }); |
| 46 | + |
| 47 | + if (errors) { |
| 48 | + console.error("[hydrogen] Variant deep link query failed", errors); |
| 49 | + } |
| 50 | + |
| 51 | + const node = data?.node; |
| 52 | + if (!node || !("selectedOptions" in node)) return null; |
| 53 | + |
| 54 | + // Clone so unrelated params (`utm_*`, `ref`) survive — a redirect must not |
| 55 | + // cost the merchant their campaign attribution. |
| 56 | + const url = request.nextUrl.clone(); |
| 57 | + // A combined listing can point at a variant on a different product. |
| 58 | + url.pathname = `/products/${node.product.handle}`; |
| 59 | + url.searchParams.delete(VARIANT_PARAM); |
| 60 | + for (const option of node.selectedOptions) { |
| 61 | + if (option.name === DEFAULT_OPTION_NAME && option.value === DEFAULT_OPTION_VALUE) continue; |
| 62 | + url.searchParams.set(option.name, option.value); |
| 63 | + } |
| 64 | + |
| 65 | + // 307, not 308: option values are merchant-editable, so this mapping must not |
| 66 | + // be cached in shoppers' browsers forever. |
| 67 | + return NextResponse.redirect(url, 307); |
| 68 | +} |
0 commit comments