@shopify/hydrogen 2026.10.2 → 2026.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/README.md +3 -5
  2. package/dist/cli/index.mjs +2 -2
  3. package/dist/client/client.mjs +44 -9
  4. package/dist/client/client.mjs.map +1 -1
  5. package/dist/client/i18n-variables.d.mts +1 -1
  6. package/dist/client/i18n-variables.mjs +14 -6
  7. package/dist/client/i18n-variables.mjs.map +1 -1
  8. package/dist/client/types.d.mts +12 -6
  9. package/dist/core/cache/fetch-with-cache.mjs +8 -1
  10. package/dist/core/cache/fetch-with-cache.mjs.map +1 -1
  11. package/dist/core/cart/cart.mjs +1 -1
  12. package/dist/core/cart/cookie.mjs +6 -1
  13. package/dist/core/cart/cookie.mjs.map +1 -1
  14. package/dist/core/cart/form.d.mts +17 -5
  15. package/dist/core/cart/form.mjs +5 -0
  16. package/dist/core/cart/form.mjs.map +1 -1
  17. package/dist/core/cart/index.d.mts +1 -1
  18. package/dist/core/collection/url.mjs +8 -1
  19. package/dist/core/collection/url.mjs.map +1 -1
  20. package/dist/core/constants.mjs +2 -1
  21. package/dist/core/constants.mjs.map +1 -1
  22. package/dist/core/csp/index.d.mts +56 -0
  23. package/dist/core/csp/index.mjs +117 -0
  24. package/dist/core/csp/index.mjs.map +1 -0
  25. package/dist/core/csp/types.d.mts +52 -0
  26. package/dist/core/headers.mjs +2 -2
  27. package/dist/core/headers.mjs.map +1 -1
  28. package/dist/core/i18n/define.d.mts +54 -0
  29. package/dist/core/i18n/define.mjs +135 -0
  30. package/dist/core/i18n/define.mjs.map +1 -0
  31. package/dist/core/i18n/errors.d.mts +18 -0
  32. package/dist/core/i18n/errors.mjs +24 -0
  33. package/dist/core/i18n/errors.mjs.map +1 -0
  34. package/dist/core/i18n/href.d.mts +28 -0
  35. package/dist/core/i18n/href.mjs +45 -0
  36. package/dist/core/i18n/href.mjs.map +1 -0
  37. package/dist/core/i18n/locale.d.mts +21 -0
  38. package/dist/core/i18n/locale.mjs +36 -0
  39. package/dist/core/i18n/locale.mjs.map +1 -0
  40. package/dist/core/i18n/match.d.mts +37 -0
  41. package/dist/core/i18n/match.mjs +82 -0
  42. package/dist/core/i18n/match.mjs.map +1 -0
  43. package/dist/core/i18n/types.d.mts +80 -0
  44. package/dist/core/index.d.mts +14 -5
  45. package/dist/core/index.mjs +9 -2
  46. package/dist/core/predictive-search/index.d.mts +2 -2
  47. package/dist/core/predictive-search/server-handlers.d.mts +27 -6
  48. package/dist/core/predictive-search/server-handlers.mjs +1 -4
  49. package/dist/core/predictive-search/server-handlers.mjs.map +1 -1
  50. package/dist/core/predictive-search/url.d.mts +2 -2
  51. package/dist/core/predictive-search/url.mjs.map +1 -1
  52. package/dist/core/product/accept-variant-id.mjs +1 -1
  53. package/dist/core/product/accept-variant-id.mjs.map +1 -1
  54. package/dist/core/request-context.d.mts +61 -35
  55. package/dist/core/request-context.mjs +11 -7
  56. package/dist/core/request-context.mjs.map +1 -1
  57. package/dist/core/request-routing/handle-shopify-routes.mjs.map +1 -1
  58. package/dist/core/request-routing/interceptors/api-proxy.mjs +9 -1
  59. package/dist/core/request-routing/interceptors/api-proxy.mjs.map +1 -1
  60. package/dist/core/request-routing/interceptors/checkout.mjs +11 -5
  61. package/dist/core/request-routing/interceptors/checkout.mjs.map +1 -1
  62. package/dist/core/request-routing/interceptors/standard-routes.mjs +1 -1
  63. package/dist/core/request-routing/interceptors/standard-routes.mjs.map +1 -1
  64. package/dist/core/request-routing/interceptors/url-redirects.mjs +2 -1
  65. package/dist/core/request-routing/interceptors/url-redirects.mjs.map +1 -1
  66. package/dist/core/request-routing/registered-routes.d.mts +10 -3
  67. package/dist/core/request-routing/registered-routes.mjs +13 -3
  68. package/dist/core/request-routing/registered-routes.mjs.map +1 -1
  69. package/dist/core/request-routing/route-types.d.mts +2 -2
  70. package/dist/core/shop-pay/shop-pay.mjs +5 -4
  71. package/dist/core/shop-pay/shop-pay.mjs.map +1 -1
  72. package/dist/core/shopify-scripts/analytics.mjs +3 -5
  73. package/dist/core/shopify-scripts/analytics.mjs.map +1 -1
  74. package/dist/core/shopify-scripts/constants.mjs +3 -1
  75. package/dist/core/shopify-scripts/constants.mjs.map +1 -1
  76. package/dist/core/shopify-scripts/global.mjs +13 -11
  77. package/dist/core/shopify-scripts/global.mjs.map +1 -1
  78. package/dist/core/shopify-scripts/hashes.d.mts +17 -0
  79. package/dist/core/shopify-scripts/hashes.mjs +12 -0
  80. package/dist/core/shopify-scripts/hashes.mjs.map +1 -0
  81. package/dist/core/shopify-scripts/index.d.mts +1 -0
  82. package/dist/core/shopify-scripts/index.mjs +12 -9
  83. package/dist/core/shopify-scripts/index.mjs.map +1 -1
  84. package/dist/core/shopify-scripts/page-view.mjs +4 -1
  85. package/dist/core/shopify-scripts/page-view.mjs.map +1 -1
  86. package/dist/core/shopify-scripts/types.d.mts +4 -2
  87. package/dist/core/shopify-scripts/utils/inline-script.mjs +4 -1
  88. package/dist/core/shopify-scripts/utils/inline-script.mjs.map +1 -1
  89. package/dist/core/standard-routes/build.d.mts +7 -2
  90. package/dist/core/standard-routes/build.mjs +10 -3
  91. package/dist/core/standard-routes/build.mjs.map +1 -1
  92. package/dist/core/standard-routes/match.mjs +11 -1
  93. package/dist/core/standard-routes/match.mjs.map +1 -1
  94. package/dist/core/standard-routes/path.mjs +14 -3
  95. package/dist/core/standard-routes/path.mjs.map +1 -1
  96. package/dist/core/standard-routes/types.d.mts +6 -1
  97. package/dist/core/url.mjs +5 -3
  98. package/dist/core/url.mjs.map +1 -1
  99. package/dist/customer-account/client.d.mts +14 -10
  100. package/dist/customer-account/client.mjs +4 -5
  101. package/dist/customer-account/client.mjs.map +1 -1
  102. package/dist/customer-account/graphql.d.mts +7 -1
  103. package/dist/customer-account/graphql.mjs +6 -7
  104. package/dist/customer-account/graphql.mjs.map +1 -1
  105. package/dist/customer-account/session.mjs +3 -4
  106. package/dist/customer-account/session.mjs.map +1 -1
  107. package/dist/development/client/client.mjs +44 -9
  108. package/dist/development/client/client.mjs.map +1 -1
  109. package/dist/development/client/i18n-variables.d.mts +1 -1
  110. package/dist/development/client/i18n-variables.mjs +13 -5
  111. package/dist/development/client/i18n-variables.mjs.map +1 -1
  112. package/dist/development/client/types.d.mts +12 -6
  113. package/dist/development/core/cache/fetch-with-cache.mjs +8 -1
  114. package/dist/development/core/cache/fetch-with-cache.mjs.map +1 -1
  115. package/dist/development/core/cart/cart.mjs +1 -1
  116. package/dist/development/core/cart/cookie.mjs +6 -1
  117. package/dist/development/core/cart/cookie.mjs.map +1 -1
  118. package/dist/development/core/cart/form.d.mts +17 -5
  119. package/dist/development/core/cart/form.mjs +5 -0
  120. package/dist/development/core/cart/form.mjs.map +1 -1
  121. package/dist/development/core/cart/index.d.mts +1 -1
  122. package/dist/development/core/cart/queries.d.mts +57 -57
  123. package/dist/development/core/collection/url.mjs +8 -1
  124. package/dist/development/core/collection/url.mjs.map +1 -1
  125. package/dist/development/core/constants.mjs +2 -1
  126. package/dist/development/core/constants.mjs.map +1 -1
  127. package/dist/development/core/csp/index.d.mts +56 -0
  128. package/dist/development/core/csp/index.mjs +128 -0
  129. package/dist/development/core/csp/index.mjs.map +1 -0
  130. package/dist/development/core/csp/types.d.mts +52 -0
  131. package/dist/development/core/development.d.mts +14 -5
  132. package/dist/development/core/development.mjs +9 -2
  133. package/dist/development/core/headers.mjs +2 -2
  134. package/dist/development/core/headers.mjs.map +1 -1
  135. package/dist/development/core/i18n/define.d.mts +54 -0
  136. package/dist/development/core/i18n/define.mjs +135 -0
  137. package/dist/development/core/i18n/define.mjs.map +1 -0
  138. package/dist/development/core/i18n/errors.d.mts +18 -0
  139. package/dist/development/core/i18n/errors.mjs +24 -0
  140. package/dist/development/core/i18n/errors.mjs.map +1 -0
  141. package/dist/development/core/i18n/href.d.mts +28 -0
  142. package/dist/development/core/i18n/href.mjs +45 -0
  143. package/dist/development/core/i18n/href.mjs.map +1 -0
  144. package/dist/development/core/i18n/locale.d.mts +21 -0
  145. package/dist/development/core/i18n/locale.mjs +36 -0
  146. package/dist/development/core/i18n/locale.mjs.map +1 -0
  147. package/dist/development/core/i18n/match.d.mts +37 -0
  148. package/dist/development/core/i18n/match.mjs +82 -0
  149. package/dist/development/core/i18n/match.mjs.map +1 -0
  150. package/dist/development/core/i18n/types.d.mts +80 -0
  151. package/dist/development/core/index.d.mts +13 -4
  152. package/dist/development/core/predictive-search/index.d.mts +2 -2
  153. package/dist/development/core/predictive-search/queries.d.mts +2 -2
  154. package/dist/development/core/predictive-search/server-handlers.d.mts +27 -6
  155. package/dist/development/core/predictive-search/server-handlers.mjs +1 -4
  156. package/dist/development/core/predictive-search/server-handlers.mjs.map +1 -1
  157. package/dist/development/core/predictive-search/url.d.mts +2 -2
  158. package/dist/development/core/predictive-search/url.mjs.map +1 -1
  159. package/dist/development/core/product/accept-variant-id.mjs +1 -1
  160. package/dist/development/core/product/accept-variant-id.mjs.map +1 -1
  161. package/dist/development/core/request-context.d.mts +61 -35
  162. package/dist/development/core/request-context.mjs +11 -7
  163. package/dist/development/core/request-context.mjs.map +1 -1
  164. package/dist/development/core/request-routing/handle-shopify-routes.mjs.map +1 -1
  165. package/dist/development/core/request-routing/interceptors/api-proxy.mjs +9 -1
  166. package/dist/development/core/request-routing/interceptors/api-proxy.mjs.map +1 -1
  167. package/dist/development/core/request-routing/interceptors/checkout.mjs +11 -5
  168. package/dist/development/core/request-routing/interceptors/checkout.mjs.map +1 -1
  169. package/dist/development/core/request-routing/interceptors/standard-routes.mjs +1 -1
  170. package/dist/development/core/request-routing/interceptors/standard-routes.mjs.map +1 -1
  171. package/dist/development/core/request-routing/interceptors/url-redirects.mjs +2 -1
  172. package/dist/development/core/request-routing/interceptors/url-redirects.mjs.map +1 -1
  173. package/dist/development/core/request-routing/registered-routes.d.mts +10 -3
  174. package/dist/development/core/request-routing/registered-routes.mjs +13 -3
  175. package/dist/development/core/request-routing/registered-routes.mjs.map +1 -1
  176. package/dist/development/core/request-routing/route-types.d.mts +2 -2
  177. package/dist/development/core/shop-pay/shop-pay.mjs +5 -4
  178. package/dist/development/core/shop-pay/shop-pay.mjs.map +1 -1
  179. package/dist/development/core/shopify-scripts/analytics.mjs +3 -5
  180. package/dist/development/core/shopify-scripts/analytics.mjs.map +1 -1
  181. package/dist/development/core/shopify-scripts/constants.mjs +3 -1
  182. package/dist/development/core/shopify-scripts/constants.mjs.map +1 -1
  183. package/dist/development/core/shopify-scripts/global.mjs +13 -11
  184. package/dist/development/core/shopify-scripts/global.mjs.map +1 -1
  185. package/dist/development/core/shopify-scripts/hashes.d.mts +17 -0
  186. package/dist/development/core/shopify-scripts/hashes.mjs +12 -0
  187. package/dist/development/core/shopify-scripts/hashes.mjs.map +1 -0
  188. package/dist/development/core/shopify-scripts/index.d.mts +1 -0
  189. package/dist/development/core/shopify-scripts/index.mjs +12 -9
  190. package/dist/development/core/shopify-scripts/index.mjs.map +1 -1
  191. package/dist/development/core/shopify-scripts/page-view.mjs +4 -1
  192. package/dist/development/core/shopify-scripts/page-view.mjs.map +1 -1
  193. package/dist/development/core/shopify-scripts/types.d.mts +4 -2
  194. package/dist/development/core/shopify-scripts/utils/inline-script.mjs +4 -1
  195. package/dist/development/core/shopify-scripts/utils/inline-script.mjs.map +1 -1
  196. package/dist/development/core/standard-routes/build.d.mts +7 -2
  197. package/dist/development/core/standard-routes/build.mjs +10 -3
  198. package/dist/development/core/standard-routes/build.mjs.map +1 -1
  199. package/dist/development/core/standard-routes/match.mjs +11 -1
  200. package/dist/development/core/standard-routes/match.mjs.map +1 -1
  201. package/dist/development/core/standard-routes/path.mjs +14 -3
  202. package/dist/development/core/standard-routes/path.mjs.map +1 -1
  203. package/dist/development/core/standard-routes/types.d.mts +6 -1
  204. package/dist/development/core/url.mjs +5 -3
  205. package/dist/development/core/url.mjs.map +1 -1
  206. package/dist/development/customer-account/session.mjs.map +1 -1
  207. package/dist/development/globals.d.mts +8 -4
  208. package/dist/development/graphql/type-resolver.d.mts +13 -1
  209. package/dist/development/react/index.d.mts +2 -2
  210. package/dist/globals.d.mts +8 -4
  211. package/dist/graphql/type-resolver.d.mts +13 -1
  212. package/dist/react/index.d.mts +2 -2
  213. package/dist/vite/index.mjs +1 -1
  214. package/dist/vue/shopify-scripts.d.mts +1 -1
  215. package/package.json +13 -3
  216. package/skills/hydrogen-analytics/SKILL.md +2 -2
  217. package/skills/hydrogen-analytics/references/react.md +2 -2
  218. package/skills/hydrogen-markets/SKILL.md +127 -93
  219. package/skills/hydrogen-markets/references/nextjs.md +77 -87
  220. package/skills/hydrogen-oxygen/references/react-router.md +1 -2
  221. package/skills/hydrogen-predictive-search/SKILL.md +18 -1
  222. package/skills/hydrogen-request-handlers/SKILL.md +23 -5
  223. package/skills/hydrogen-request-handlers/references/content-security-policy.md +103 -0
  224. package/skills/hydrogen-request-handlers/references/frameworks.md +8 -6
  225. package/skills/hydrogen-request-handlers/references/nextjs.md +7 -3
  226. package/skills/hydrogen-request-handlers/references/nuxt.md +7 -5
  227. package/skills/hydrogen-robots-txt/SKILL.md +87 -0
  228. package/skills/hydrogen-routing/SKILL.md +36 -14
  229. package/skills/hydrogen-setup/SKILL.md +2 -2
  230. package/skills/hydrogen-setup/references/analytics.md +5 -5
  231. package/skills/hydrogen-setup/steps/2-scaffold.md +3 -0
  232. package/skills/hydrogen-setup/steps/7-shopify-runtime-scripts.md +4 -3
  233. package/skills/hydrogen-setup/steps/9-account-page.md +1 -1
  234. package/skills/hydrogen-smoke-test/SKILL.md +9 -2
  235. package/skills/hydrogen-storefront-client/SKILL.md +27 -9
  236. package/skills/hydrogen-storefront-client/references/astro.md +8 -3
  237. package/skills/hydrogen-storefront-client/references/caching.md +2 -0
  238. package/skills/hydrogen-storefront-client/references/nextjs.md +8 -3
  239. package/skills/hydrogen-storefront-client/references/nuxt.md +9 -5
  240. package/skills/hydrogen-storefront-client/references/react-router.md +6 -3
  241. package/skills/hydrogen-storefront-client/references/solidstart.md +6 -2
  242. package/skills/hydrogen-storefront-client/references/sveltekit.md +10 -4
  243. package/skills/migrate-to-hydrogen-2026-10/SKILL.md +20 -22
  244. package/skills/migrate-to-hydrogen-2026-10/references/sitemap-and-seo.md +1 -0
  245. package/skills/migrate-to-hydrogen-2026-10/references/typescript-and-gql-tada.md +1 -1
@@ -3,23 +3,37 @@ name: hydrogen-markets
3
3
  description: >
4
4
  Guide for implementing Shopify Markets with Hydrogen. Use when adding
5
5
  market routing, country/language localization, Shopify Markets, subdomains,
6
- per-market domains, path-prefixed markets, Next.js App Router market routing,
7
- or Storefront API @inContext wiring.
6
+ per-market domains, path-prefixed markets, defineShopifyI18n, locale switchers,
7
+ hreflang links, Next.js App Router market routing, or Storefront API @inContext wiring.
8
8
  ---
9
9
 
10
10
  # Markets
11
11
 
12
- Markets are an application routing concern plus Storefront API context. Hydrogen does not prescribe how a storefront chooses a market. Use domains, subdomains, paths, cookies, buyer preferences, geolocation, or merchant config as needed.
12
+ Markets are a routing concern plus Storefront API context. Hydrogen owns both halves once the storefront declares its locales: `defineShopifyI18n` describes which locales exist and how the URL encodes them, and `createShopifyRequestContext({ request, i18n })` resolves the request's locale from that definition. Queries that declare `$country` and `$language` with `@inContext(country: $country, language: $language)` receive those values from the resolved locale.
13
13
 
14
- The Hydrogen part is request-scoped `i18n` on `createShopifyRequestContext`. Resolve the market first, then create the request context with `i18n`. When a query declares `$country` and `$language` and uses `@inContext(country: $country, language: $language)`, the client injects those values from the resolved `i18n`.
15
-
16
- If the storefront uses Next.js App Router, read `references/nextjs.md` before writing code. Server Components do not receive a full `Request`, so host-based and path-prefixed markets need Next-specific wiring.
14
+ If the storefront uses Next.js App Router, read `references/nextjs.md` before writing code. Server Components do not receive a full `Request`, so the locale comes from the forwarded URL header or an explicit `locale` override.
17
15
 
18
16
  ---
19
17
 
20
18
  ## Core Rule
21
19
 
22
- Add `i18n` where the app already creates its Shopify request context:
20
+ Define the storefront's locales once at module scope, then pass that definition where the app creates its Shopify request context:
21
+
22
+ ```ts
23
+ // lib/i18n.ts
24
+ import { defineShopifyI18n } from "@shopify/hydrogen";
25
+
26
+ export const i18n = defineShopifyI18n({
27
+ defaultLocale: { language: "EN", country: "US" },
28
+ routing: {
29
+ type: "pathname",
30
+ locales: [
31
+ { language: "EN", country: "CA" },
32
+ { language: "FR", country: "CA" },
33
+ ],
34
+ },
35
+ });
36
+ ```
23
37
 
24
38
  ```diff
25
39
  import {
@@ -27,6 +41,7 @@ import {
27
41
  createShopifyRequestContext,
28
42
  gql,
29
43
  } from "@shopify/hydrogen";
44
+ +import { i18n } from "./lib/i18n";
30
45
 
31
46
  const PRODUCTS_PAGE_SIZE = 12;
32
47
 
@@ -47,7 +62,6 @@ export async function loadProducts(request: Request) {
47
62
  - request,
48
63
  - i18n: DEFAULT_MARKET,
49
64
  - });
50
- + const i18n = getMarketFromRequest(request);
51
65
  + const requestContext = createShopifyRequestContext({
52
66
  + request,
53
67
  + i18n,
@@ -68,114 +82,120 @@ export async function loadProducts(request: Request) {
68
82
  }
69
83
  ```
70
84
 
71
- Do not pass `country` or `language` in every query call. Resolve the market once at the request boundary, set `i18n` when creating the request context, and let the client inject the context variables into queries that declare them.
85
+ Do not pass `country` or `language` in every query call. The request context resolves the locale once at the request boundary and the client injects the context variables into queries that declare them.
72
86
 
73
- The returned Storefront client exposes the resolved locale as `client.i18n`. Use `client.i18n.pathPrefix` when app route helpers need to prepend a market path; Hydrogen normalizes it to `""` or a leading-slash prefix with no trailing slash.
87
+ `requestContext.locale` is the resolved locale for this request: `{ language, country, pathPrefix }` (`ShopifyMatchedLocale`). `pathPrefix` is `""` for the default locale and for domain routing, or a leading-slash prefix such as `/fr-ca` with no trailing slash. The Storefront client mirrors it as `client.locale`. The definition is not on the context: import it from your i18n module wherever you need it.
74
88
 
75
89
  ---
76
90
 
77
- ## Market Shape
91
+ ## Definition Shape
78
92
 
79
- Keep the resolver small and explicit. The Storefront API expects uppercase enum values like `US`, `CA`, `EN`, and `FR`.
93
+ `defineShopifyI18n` returns its input unchanged with literal types preserved, so the definition is serializable and safe to import from server and client code. It validates once at module load, so a bad entry fails at startup rather than on the first request that hits it.
80
94
 
81
95
  ```ts
82
- type Market = {
83
- country: string;
84
- language: string;
85
- pathPrefix?: string;
96
+ type ShopifyI18n = {
97
+ defaultLocale: { language: ShopifyLanguageCode; country: ShopifyCountryCode };
98
+ routing?:
99
+ | { type: "pathname"; locales: readonly ShopifyPathnameLocale[] }
100
+ | { type: "domain"; locales: readonly ShopifyDomainLocale[] };
86
101
  };
87
-
88
- const DEFAULT_MARKET = {
89
- country: "US",
90
- language: "EN",
91
- } satisfies Market;
92
102
  ```
93
103
 
94
- If the app needs labels or alternate domains, keep those as application fields. `country` and `language` are required in `i18n`; `pathPrefix` is optional on input and is normalized on the request context to `""` or a leading-slash prefix with no trailing slash.
104
+ - Omit `routing` for a single-locale storefront; every request resolves to `defaultLocale`.
105
+ - Codes are the Storefront API enums: `US`, `CA`, `EN`, `FR`, `PT_BR`.
106
+ - Extra per-locale fields (labels, `currency`) are kept on the entries returned by `getSupportedLocales(i18n)` and on `requestContext.locale`, so the app can carry its own metadata without a parallel map.
107
+ - Cookie-based, geolocation-based, or buyer-preference routing is not a routing type. Read those signals in the app and redirect to a canonical URL; the URL stays the only input to locale resolution.
95
108
 
96
109
  ---
97
110
 
98
- ## Subdomains
111
+ ## Path Prefixes
99
112
 
100
- Use this when each market lives under one storefront domain, like `ca.example.com` or `fr.example.com`.
113
+ Use `type: "pathname"` when locales share one domain and the locale is encoded in the first path segment, like `/fr-ca/products/shirt`.
101
114
 
102
115
  ```ts
103
- const MARKET_BY_SUBDOMAIN = {
104
- ca: { country: "CA", language: "EN" },
105
- fr: { country: "FR", language: "FR" },
106
- } satisfies Record<string, Market>;
107
-
108
- export function getMarketFromRequest(request: Request): Market {
109
- const { hostname } = new URL(request.url);
110
- const [subdomain] = hostname.toLowerCase().split(".");
111
-
112
- return MARKET_BY_SUBDOMAIN[subdomain] ?? DEFAULT_MARKET;
113
- }
116
+ export const i18n = defineShopifyI18n({
117
+ defaultLocale: { language: "EN", country: "US" },
118
+ routing: {
119
+ type: "pathname",
120
+ locales: [
121
+ { language: "FR", country: "CA" },
122
+ { language: "PT_BR", country: "BR", pathSegment: "br" },
123
+ ],
124
+ },
125
+ });
114
126
  ```
115
127
 
128
+ - The prefix derives as `/{language}-{country}` lowercased with `_` replaced by `-`: `{ language: "FR", country: "CA" }` is served at `/fr-ca`. Set `pathSegment` when the derived segment is undesirable (`PT_BR` + `BR` would derive to `pt-br-br`). A segment is one path part with no slashes.
129
+ - `defaultLocale` is served unprefixed only and must not appear in `locales`; the definition throws if it does. Every page has one canonical URL.
130
+ - Unknown first segments resolve to `defaultLocale`. If the router has a `$locale` param, treat a matched param with an empty `requestContext.locale.pathPrefix` as a 404 so `/en-us/...` and `/xx/...` do not render the default locale under a second URL.
131
+ - Route templates stay prefix-free. Hydrogen prepends `requestContext.locale.pathPrefix` when it builds or matches standard route URLs, and `handleShopifyRoutes` matches registered handlers with the prefix stripped, so `/fr-ca/api/cart` reaches the `/api/cart` handler. Use the local `hydrogen-routing` skill for templates.
132
+
116
133
  ---
117
134
 
118
- ## Per-Market Domains
135
+ ## Per-Market Domains And Subdomains
119
136
 
120
- Use this when markets have separate buyer-facing domains, like `example.com`, `example.ca`, and `example.fr`.
137
+ Use `type: "domain"` when each locale has its own hostname: `example.com`, `example.ca`, `fr.example.com`.
121
138
 
122
139
  ```ts
123
- const MARKET_BY_HOST = {
124
- "example.com": { country: "US", language: "EN" },
125
- "example.ca": { country: "CA", language: "EN" },
126
- "example.fr": { country: "FR", language: "FR" },
127
- } satisfies Record<string, Market>;
128
-
129
- export function getMarketFromRequest(request: Request): Market {
130
- const { hostname } = new URL(request.url);
131
- const host = hostname.toLowerCase();
132
-
133
- return MARKET_BY_HOST[host] ?? DEFAULT_MARKET;
134
- }
140
+ export const i18n = defineShopifyI18n({
141
+ defaultLocale: { language: "EN", country: "US" },
142
+ routing: {
143
+ type: "domain",
144
+ locales: [
145
+ { language: "EN", country: "US", hostname: "example.com" },
146
+ { language: "EN", country: "CA", hostname: "example.ca" },
147
+ { language: "FR", country: "FR", hostname: "fr.example.com" },
148
+ ],
149
+ },
150
+ });
135
151
  ```
136
152
 
153
+ - Match is exact on `URL.hostname` (no port), so `fr.example.com`, `example.fr`, and `example.com` are three separate entries. Hostname comparison is case-insensitive.
154
+ - `defaultLocale` must be listed so it has a canonical hostname for links and sitemaps. Unrecognized hosts (localhost, preview URLs) resolve to `defaultLocale`.
155
+ - `www.` and other aliases are a redirect concern. Do not list them as locales.
156
+ - `pathPrefix` is always `""` under domain routing.
157
+
137
158
  ---
138
159
 
139
- ## Path Prefixes
160
+ ## Locale Switchers And Alternate Links
140
161
 
141
- Use this when markets share one domain and the market is encoded in the pathname, like `/en-ca/products/shirt` or `/fr-fr/products/shirt`.
162
+ Use the exported helpers instead of string-building URLs:
142
163
 
143
164
  ```ts
144
- type PathMarket = Market & {
145
- pathPrefix: string;
146
- };
147
-
148
- const FIRST_PATH_SEGMENT_INDEX = 0;
149
-
150
- const MARKET_BY_PATH_PREFIX = {
151
- "en-ca": { country: "CA", language: "EN", pathPrefix: "/en-ca" },
152
- "fr-fr": { country: "FR", language: "FR", pathPrefix: "/fr-fr" },
153
- } satisfies Record<string, PathMarket>;
154
-
155
- function getFirstPathSegment(pathname: string): string | undefined {
156
- return pathname.split("/").filter(Boolean)[FIRST_PATH_SEGMENT_INDEX];
157
- }
158
-
159
- export function getMarketFromRequest(request: Request): PathMarket | Market {
160
- const { pathname } = new URL(request.url);
161
- const pathPrefix = getFirstPathSegment(pathname);
162
-
163
- if (!pathPrefix) return DEFAULT_MARKET;
164
-
165
- return MARKET_BY_PATH_PREFIX[pathPrefix.toLowerCase()] ?? DEFAULT_MARKET;
165
+ import {
166
+ getLocalizedHref,
167
+ getSupportedLocales,
168
+ isSameLocale,
169
+ type ShopifyLocale,
170
+ } from "@shopify/hydrogen";
171
+ import { i18n } from "./lib/i18n";
172
+
173
+ export function getAlternateLinks(currentUrl: string, currentLocale: ShopifyLocale) {
174
+ return getSupportedLocales(i18n)
175
+ .filter((locale) => !isSameLocale(locale, currentLocale))
176
+ .map((locale) => ({
177
+ hreflang: toHreflang(locale),
178
+ href: getLocalizedHref(currentUrl, { i18n, locale }),
179
+ }));
166
180
  }
167
181
 
168
- export function localizePath(pathname: string, market: PathMarket | Market): string {
169
- if (!("pathPrefix" in market)) return pathname;
170
-
171
- const normalizedPathname = pathname.replace(/^\/+/, "");
172
- return `${market.pathPrefix}/${normalizedPathname}`;
182
+ // BCP 47 allows one region subtag. Regional language codes (`PT_BR`, `ZH_TW`) already carry one,
183
+ // so keep the primary language and let `country` name the region: PT_BR + BR -> `pt-BR`. The
184
+ // Chinese codes differ by script, so they keep a script subtag: ZH_TW + HK -> `zh-Hant-HK`.
185
+ function toHreflang({ language, country }: ShopifyLocale): string {
186
+ const primary = { ZH_CN: "zh-Hans", ZH_TW: "zh-Hant" }[language] ?? language.replace(/_.*$/, "").toLowerCase();
187
+ return `${primary}-${country}`;
173
188
  }
174
189
  ```
175
190
 
176
- The router should strip or interpret the prefix before matching product and collection routes. Hydrogen's Storefront API variable injection uses `country` and `language`; `pathPrefix` is carried as a leading-slash app route prefix for helpers.
191
+ - `hreflang` values are `language-REGION` (optionally `language-Script-REGION`). Do not build them as `${language}-${country}`: Shopify's regional language codes would yield two region subtags (`pt-br-BR`), which is invalid. Two locales must never collapse to the same tag; fail loudly if they do.
192
+
193
+ - `getLocalizedHref(href, { i18n, locale })` rewrites an href to the same page in another locale. Under pathname routing it returns a path with any existing locale prefix replaced; under domain routing it returns an absolute `https:` URL on the target hostname; with no routing it returns the input unchanged. It throws when `locale` is not defined in `i18n`.
194
+ - `getSupportedLocales(i18n)` returns every locale the definition can resolve to, default first, keeping `pathSegment` / `hostname` and any extra fields.
195
+ - `matchLocale(request | url, i18n)` exposes the same resolution the request context uses, for sitemaps or tests that hold a URL but no request context.
196
+ - `resolveSupportedLocale(locale, i18n)` resolves a locale the app already chose (a selector value, a stored preference) to its `pathPrefix`/`hostname`, throwing when it is not in the definition. Use it to validate user input before redirecting.
177
197
 
178
- When the app uses custom Shopify resource paths, use the local `hydrogen-routing` skill. Keep market prefixes in `i18n.pathPrefix`, not in route template values.
198
+ Persist an explicit buyer choice by redirecting to `getLocalizedHref(...)`; do not resolve the locale from a cookie on the server.
179
199
 
180
200
  ---
181
201
 
@@ -183,37 +203,51 @@ When the app uses custom Shopify resource paths, use the local `hydrogen-routing
183
203
 
184
204
  Read the relevant reference before applying the generic `Request` examples in frameworks that do not expose the full incoming request to server code.
185
205
 
186
- - **Next.js App Router** — read `references/nextjs.md` first. Server Components can read `headers()` and route `params`, but not the current URL or a standard `Request`; host-based markets, path-prefix markets, and raw URL fallback each need different wiring.
206
+ - **Next.js App Router** — read `references/nextjs.md` first. Server Components can read `headers()` but not the current URL or a standard `Request`; the per-request client resolves the locale from the forwarded `x-storefront-url` header, and module-scope static clients pin `locale` explicitly.
187
207
 
188
208
  ---
189
209
 
190
210
  ## Rules
191
211
 
192
- - **Use whatever market strategy fits the store.** Hydrogen should not force domains, subdomains, pathnames, or detection policy.
193
- - **Resolve the market on the server from a standard `Request`.** `new URL(request.url)` is enough for host and path strategies.
194
- - **Use request-context `i18n` as the Storefront API boundary.** Prefer `createShopifyRequestContext({ request, i18n })`. Always provide a default market rather than letting it be undefined.
212
+ - **Define locales once with `defineShopifyI18n`.** Keep the definition in a module both server and client code can import. Do not hand-roll host allowlists or path parsers; the definition is the allowlist.
213
+ - **Resolve the locale through `createShopifyRequestContext({ request, i18n })`.** Pass a real `Request` when the framework has one so `request.url` drives matching. With `request: { headers }`, the forwarded `x-storefront-url` header is used instead.
214
+ - **Pass `locale` only when there is no meaningful URL.** Static rendering, background jobs, and module-scope clients set `locale: i18n.defaultLocale` (or another supported locale). The override is validated against the definition and throws for unsupported locales.
215
+ - **Read the resolved locale from `requestContext.locale`, never from `requestContext.i18n`.** `i18n` is a deprecated alias kept for apps that still pass a fixed `{language, country, pathPrefix}`; it is removed in the next major release.
195
216
  - **Use market-contextualized queries.** The Storefront client injects `country` and `language` variable values when the document declares `$country` and `$language`; it does not rewrite query text. Market-sensitive queries still need `@inContext(country: $country, language: $language)` or equivalent Storefront API context in the document.
196
217
  - **Do not calculate currency locally.** Render `amount` and `currencyCode` returned by Shopify and format them with the local `hydrogen-money` skill's `formatMoney()` guidance.
197
- - **Keep ShopifyScripts in the same market.** When the app renders Shopify browser runtime scripts, pass the resolved `country` and `language` into `ShopifyScripts` i18n so Shopify globals match Storefront API context.
198
- - **Keep route templates prefix-free.** When route templates are configured, pass market prefixes through `i18n.pathPrefix`; do not bake prefixes such as `/en-ca` into template values.
218
+ - **Keep ShopifyScripts in the same locale.** Pass `requestContext.locale` (plus optional `currency`) as the `ShopifyScripts` `i18n` prop so Shopify globals match Storefront API context.
219
+ - **Keep route templates prefix-free.** Hydrogen applies `requestContext.locale.pathPrefix`; do not bake prefixes such as `/en-ca` into template values.
199
220
  - **Keep translations separate.** Shopify Markets context localizes Shopify data. Application UI strings still need the app's translation system.
200
- - **Treat geolocation as a hint, not truth.** Buyers travel, use VPNs, and intentionally choose markets. Persist explicit choices when the app supports switching.
201
- - **Ask before choosing the strategy.** If the app does not already make market selection clear, ask whether domains, subdomains, path prefixes, cookies, buyer preference, geolocation, or merchant config should own it before writing code.
221
+ - **Treat geolocation as a hint, not truth.** Buyers travel, use VPNs, and intentionally choose markets. Redirect to a canonical locale URL when the app supports switching.
222
+ - **Ask before choosing the routing type.** If the app does not already make locale selection clear, ask whether path prefixes or per-locale domains should own it before writing code.
202
223
 
203
224
  ---
204
225
 
205
226
  ## Testing
206
227
 
207
- Test market resolution with plain `Request` objects. You do not need a framework test harness.
228
+ Test locale resolution with plain `Request` objects against the definition. You do not need a framework test harness.
208
229
 
209
230
  ```ts
231
+ import { matchLocale } from "@shopify/hydrogen";
210
232
  import { describe, expect, it } from "vitest";
211
233
 
212
- describe("getMarketFromRequest", () => {
213
- it("resolves a subdomain market", () => {
214
- const request = new Request("https://ca.example.com/products/shirt");
234
+ import { i18n } from "./i18n";
235
+
236
+ describe("i18n", () => {
237
+ it("resolves a prefixed locale", () => {
238
+ const request = new Request("https://example.com/fr-ca/products/shirt");
239
+
240
+ expect(matchLocale(request, i18n)).toEqual({
241
+ language: "FR",
242
+ country: "CA",
243
+ pathPrefix: "/fr-ca",
244
+ });
245
+ });
246
+
247
+ it("falls back to the default locale for unknown prefixes", () => {
248
+ const request = new Request("https://example.com/xx/products/shirt");
215
249
 
216
- expect(getMarketFromRequest(request)).toEqual({ country: "CA", language: "EN" });
250
+ expect(matchLocale(request, i18n).pathPrefix).toBe("");
217
251
  });
218
252
  });
219
253
  ```
@@ -2,55 +2,46 @@
2
2
 
3
3
  ## Contents
4
4
 
5
- - Host-Based Markets
6
- - Path-Prefix Markets
7
- - Raw URL Fallback
5
+ - Definition
6
+ - Per-Request Client
7
+ - Static Client
8
+ - Path-Prefix Routes
8
9
 
9
- Next.js App Router Server Components do not receive a standard `Request` object or direct access to the current URL. Use the source that matches the market strategy:
10
+ Next.js App Router Server Components do not receive a standard `Request` object or direct access to the current URL. The locale still comes from the shared `defineShopifyI18n` definition; what changes is where `createShopifyRequestContext` reads the URL from:
10
11
 
11
- - Host or subdomain markets: read the URL from Hydrogen's request context when the app proxy forwards it.
12
- - Path-prefix markets: model the market as a route segment, such as `app/[market]/products/[handle]/page.tsx`, and read it from `params`.
13
- - Raw URL markets: use a request-context handoff in `proxy.ts`; do not invent a second URL header.
12
+ - Per-request clients: `proxy.ts` forwards the original URL as `x-storefront-url`; a request context created from `{ headers: await headers() }` matches the locale from that header automatically.
13
+ - Module-scope static clients: there is no request URL, so pin `locale` explicitly.
14
14
 
15
- ---
16
-
17
- ## Host-Based Markets
15
+ Do not invent a second URL header or a parallel market map. There is no `lib/markets.ts` in the Next.js template; the definition lives in `lib/i18n.ts`.
18
16
 
19
- Use this for domain or subdomain routing, like `example.ca`, `fr.example.com`, or `ca.example.com`.
17
+ ---
20
18
 
21
- `proxy.ts` creates a request context from the `NextRequest`, forwards `requestContext.getForwardedRequestHeaders()`, and Server Components recreate the request context from `headers()`. That handoff includes the original URL.
19
+ ## Definition
22
20
 
23
21
  ```ts
24
- // lib/markets.ts
25
- type Market = {
26
- country: string;
27
- language: string;
28
- pathPrefix?: string;
29
- };
30
-
31
- const DEFAULT_MARKET = {
32
- country: "US",
33
- language: "EN",
34
- } satisfies Market;
35
-
36
- const MARKET_BY_HOST = {
37
- "example.com": { country: "US", language: "EN" },
38
- "example.ca": { country: "CA", language: "EN" },
39
- "fr.example.com": { country: "FR", language: "FR" },
40
- } satisfies Record<string, Market>;
41
-
42
- export function getMarketFromHeaders(headers: Pick<Headers, "get">): Market {
43
- const forwardedUrl = headers.get("x-storefront-url");
44
- if (!forwardedUrl) return DEFAULT_MARKET;
45
-
46
- const { hostname } = new URL(forwardedUrl);
47
- const host = hostname.toLowerCase();
48
-
49
- return MARKET_BY_HOST[host] ?? DEFAULT_MARKET;
50
- }
22
+ // lib/i18n.ts
23
+ import { defineShopifyI18n } from "@shopify/hydrogen";
24
+
25
+ export const i18n = defineShopifyI18n({
26
+ defaultLocale: { language: "EN", country: "US" },
27
+ routing: {
28
+ type: "domain",
29
+ locales: [
30
+ { language: "EN", country: "US", hostname: "example.com" },
31
+ { language: "EN", country: "CA", hostname: "example.ca" },
32
+ { language: "FR", country: "FR", hostname: "fr.example.com" },
33
+ ],
34
+ },
35
+ });
51
36
  ```
52
37
 
53
- Use an allowlist like `MARKET_BY_HOST`; do not blindly trust request hosts for anything security-sensitive.
38
+ Swap `routing` for `type: "pathname"` when locales share one domain. Omit `routing` for a single-locale storefront. The definition is the host allowlist; unrecognized hosts resolve to `defaultLocale`.
39
+
40
+ ---
41
+
42
+ ## Per-Request Client
43
+
44
+ `proxy.ts` creates a request context from the `NextRequest` and forwards `requestContext.getForwardedRequestHeaders()`. That handoff carries the original URL in `x-storefront-url`, and `createShopifyRequestContext` reads it when `request` has only `headers`.
54
45
 
55
46
  ```ts
56
47
  // lib/storefront.ts
@@ -62,15 +53,13 @@ import {
62
53
  import { headers } from "next/headers";
63
54
  import { cache } from "react";
64
55
 
65
- import { storefrontConfig } from "./config";
66
- import { getMarketFromHeaders } from "./markets";
56
+ import { i18n, storefrontConfig } from "./config";
67
57
 
68
58
  export const getStorefrontClient = cache(async () => {
69
59
  const requestHeaders = await headers();
70
- const request = { headers: requestHeaders };
71
60
  const requestContext = createShopifyRequestContext({
72
- request,
73
- i18n: getMarketFromHeaders(requestHeaders),
61
+ request: { headers: requestHeaders },
62
+ i18n,
74
63
  });
75
64
 
76
65
  return createStorefrontClient({
@@ -84,49 +73,62 @@ export const getStorefrontClient = cache(async () => {
84
73
  });
85
74
  ```
86
75
 
87
- `headers()` is a request-time API, so pages using this helper are dynamically rendered.
76
+ `requestContext.locale` is the locale matched from the forwarded URL. `headers()` is a request-time API, so pages using this helper are dynamically rendered.
88
77
 
89
78
  ---
90
79
 
91
- ## Path-Prefix Markets
80
+ ## Static Client
92
81
 
93
- Use this for routes like `/en-ca/products/shirt` or `/fr-fr/products/shirt`. Model the market prefix as a route segment so Server Components can read it from `params` without `headers()`.
82
+ Module-scope clients have no request URL. Pin the locale so the request context does not need to match one:
94
83
 
95
84
  ```ts
96
- // lib/path-markets.ts
97
- type Market = {
98
- country: string;
99
- language: string;
100
- pathPrefix?: string;
101
- };
102
-
103
- const DEFAULT_MARKET = {
104
- country: "US",
105
- language: "EN",
106
- } satisfies Market;
107
-
108
- const MARKET_BY_PARAM = {
109
- "en-ca": { country: "CA", language: "EN", pathPrefix: "/en-ca" },
110
- "fr-fr": { country: "FR", language: "FR", pathPrefix: "/fr-fr" },
111
- } satisfies Record<string, Market>;
112
-
113
- export function getMarketFromParam(marketParam: string): Market {
114
- return MARKET_BY_PARAM[marketParam.toLowerCase()] ?? DEFAULT_MARKET;
115
- }
85
+ // lib/storefront-static.ts
86
+ import "server-only";
87
+ import { createStorefrontClient, createShopifyRequestContext } from "@shopify/hydrogen";
88
+
89
+ import { i18n } from "./config";
90
+
91
+ const requestContext = createShopifyRequestContext({
92
+ request: { headers: new Headers() },
93
+ i18n,
94
+ locale: i18n.defaultLocale,
95
+ });
96
+
97
+ export const staticStorefrontClient = createStorefrontClient({
98
+ type: "private_no_buyer_context",
99
+ requestContext,
100
+ config: {
101
+ storeDomain: process.env.NEXT_PUBLIC_STORE_DOMAIN!,
102
+ privateStorefrontToken: process.env.PRIVATE_STOREFRONT_API_TOKEN!,
103
+ },
104
+ });
116
105
  ```
117
106
 
118
- For static or ISR-cached pages, use a `private_no_buyer_context` client and pass the market from route params. The page can be prerendered when the route uses `generateStaticParams` and does not call request-time APIs like `headers()` or `cookies()`.
107
+ `locale` must be one of the definition's supported locales; the request context throws otherwise. Pages that use this client can be prerendered when the route uses `generateStaticParams` and does not call request-time APIs like `headers()` or `cookies()`.
108
+
109
+ ---
110
+
111
+ ## Path-Prefix Routes
112
+
113
+ With `type: "pathname"`, model the prefix as a route segment such as `app/[locale]/products/[handle]/page.tsx` so the router can render the localized tree. Resolve the locale from the segment with `matchLocale` rather than a second lookup table, then create a static client for that locale:
119
114
 
120
115
  ```ts
121
116
  // lib/static-storefront.ts
122
- import { createStorefrontClient, createShopifyRequestContext } from "@shopify/hydrogen";
117
+ import "server-only";
118
+ import {
119
+ createStorefrontClient,
120
+ createShopifyRequestContext,
121
+ matchLocale,
122
+ } from "@shopify/hydrogen";
123
123
 
124
- import { getMarketFromParam } from "./path-markets";
124
+ import { i18n } from "./config";
125
125
 
126
- export function createStaticStorefrontClient(marketParam: string) {
126
+ export function createStaticStorefrontClient(localeParam: string) {
127
+ const locale = matchLocale(`https://shopify.local/${localeParam}/`, i18n);
127
128
  const requestContext = createShopifyRequestContext({
128
129
  request: { headers: new Headers() },
129
- i18n: getMarketFromParam(marketParam),
130
+ i18n,
131
+ locale,
130
132
  });
131
133
 
132
134
  return createStorefrontClient({
@@ -140,18 +142,6 @@ export function createStaticStorefrontClient(marketParam: string) {
140
142
  }
141
143
  ```
142
144
 
143
- Use the query shape from `SKILL.md`: declare `$country` and `$language`, use `@inContext`, and let the client inject the market variables. Use the private client with trusted buyer context instead when the page needs buyer-specific headers, cookies, or personalized data. That path becomes dynamic because it must read request-time data.
144
-
145
- ---
146
-
147
- ## Raw URL Fallback
148
-
149
- Use raw URLs only when host headers or route params cannot represent the market, usually with rewrites or catch-all route setups.
150
-
151
- Use this proxy handoff:
152
-
153
- - In `proxy.ts`, create a request context from the `NextRequest`.
154
- - Forward `requestContext.getForwardedRequestHeaders()` through `NextResponse.next({ request: { headers } })`.
155
- - In Server Components, recreate a request-like object with `request: { headers: await headers() }`, resolve the market from the forwarded URL header, then create the client request context with that `i18n`.
145
+ Unknown segments resolve to `defaultLocale`; return `notFound()` from the page when `locale.pathPrefix` is `""` but a segment was present, so the default locale keeps one canonical URL. Use `getSupportedLocales(i18n)` in `generateStaticParams` to enumerate the prefixed locales.
156
146
 
157
- `createShopifyRequestContext` already persists the URL through `x-storefront-url`, so do not set a parallel URL header unless the app cannot use the proxy handoff.
147
+ Use the query shape from `SKILL.md`: declare `$country` and `$language`, use `@inContext`, and let the client inject the locale variables. Use the per-request client instead when the page needs buyer-specific headers, cookies, or personalized data; that path becomes dynamic because it reads request-time data.
@@ -77,7 +77,7 @@ Do not add classic Hydrogen's Vite plugin or unrelated development plugins solel
77
77
 
78
78
  ## React Router Configuration
79
79
 
80
- Keep SSR enabled. Preserve existing future flags and app behavior. Middleware-based apps generally need:
80
+ Keep SSR enabled. Preserve existing future flags and app behavior, and make sure middleware is enabled for your version of React Router. Middleware-based apps generally need:
81
81
 
82
82
  ```ts
83
83
  import type { Config } from "@react-router/dev/config";
@@ -86,7 +86,6 @@ export default {
86
86
  ssr: true,
87
87
  subResourceIntegrity: false,
88
88
  future: {
89
- v8_middleware: true,
90
89
  unstable_optimizeDeps: true,
91
90
  },
92
91
  } satisfies Config;
@@ -77,7 +77,7 @@ const predictiveSearchFragments = {
77
77
  `),
78
78
  };
79
79
 
80
- const predictiveSearchHandlers = createPredictiveSearchServerHandlers({
80
+ export const predictiveSearchHandlers = createPredictiveSearchServerHandlers({
81
81
  fragments: predictiveSearchFragments,
82
82
  });
83
83
 
@@ -94,6 +94,23 @@ const data = await queryPredictiveSearch({
94
94
 
95
95
  Use the route-handler form for browser-backed autocomplete. Use `makePredictiveSearchQueries()` when querying directly from server code. Keep fragment names as documented by the API types. Hydrogen composes these with required base fields such as IDs, handles, titles, prices, images, and tracking parameters.
96
96
 
97
+ Type client state from the same handlers so the UI sees the custom fields:
98
+
99
+ ```ts
100
+ import type { PredictiveSearchDataFromHandlers } from "@shopify/hydrogen";
101
+ import { usePredictiveSearch } from "@shopify/hydrogen/react"; // or "@shopify/hydrogen/vue"
102
+ import type { predictiveSearchHandlers } from "./predictive-search-handlers";
103
+
104
+ type SearchData = PredictiveSearchDataFromHandlers<typeof predictiveSearchHandlers>;
105
+
106
+ // Call inside a component or setup().
107
+ export function usePredictiveSearchState() {
108
+ return usePredictiveSearch<SearchData>();
109
+ }
110
+ ```
111
+
112
+ Import the handlers with `import type` so server code stays out of the client bundle. Derive the type from the handlers rather than writing it by hand, and point the provider at those handlers' route: the client doesn't validate responses, and when there's no response to show, the result only holds empty item arrays.
113
+
97
114
  ## Rules
98
115
 
99
116
  - Use the packaged route handler as the default browser transport.