@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.
- package/README.md +3 -5
- package/dist/cli/index.mjs +2 -2
- package/dist/client/client.mjs +44 -9
- package/dist/client/client.mjs.map +1 -1
- package/dist/client/i18n-variables.d.mts +1 -1
- package/dist/client/i18n-variables.mjs +14 -6
- package/dist/client/i18n-variables.mjs.map +1 -1
- package/dist/client/types.d.mts +12 -6
- package/dist/core/cache/fetch-with-cache.mjs +8 -1
- package/dist/core/cache/fetch-with-cache.mjs.map +1 -1
- package/dist/core/cart/cart.mjs +1 -1
- package/dist/core/cart/cookie.mjs +6 -1
- package/dist/core/cart/cookie.mjs.map +1 -1
- package/dist/core/cart/form.d.mts +17 -5
- package/dist/core/cart/form.mjs +5 -0
- package/dist/core/cart/form.mjs.map +1 -1
- package/dist/core/cart/index.d.mts +1 -1
- package/dist/core/collection/url.mjs +8 -1
- package/dist/core/collection/url.mjs.map +1 -1
- package/dist/core/constants.mjs +2 -1
- package/dist/core/constants.mjs.map +1 -1
- package/dist/core/csp/index.d.mts +56 -0
- package/dist/core/csp/index.mjs +117 -0
- package/dist/core/csp/index.mjs.map +1 -0
- package/dist/core/csp/types.d.mts +52 -0
- package/dist/core/headers.mjs +2 -2
- package/dist/core/headers.mjs.map +1 -1
- package/dist/core/i18n/define.d.mts +54 -0
- package/dist/core/i18n/define.mjs +135 -0
- package/dist/core/i18n/define.mjs.map +1 -0
- package/dist/core/i18n/errors.d.mts +18 -0
- package/dist/core/i18n/errors.mjs +24 -0
- package/dist/core/i18n/errors.mjs.map +1 -0
- package/dist/core/i18n/href.d.mts +28 -0
- package/dist/core/i18n/href.mjs +45 -0
- package/dist/core/i18n/href.mjs.map +1 -0
- package/dist/core/i18n/locale.d.mts +21 -0
- package/dist/core/i18n/locale.mjs +36 -0
- package/dist/core/i18n/locale.mjs.map +1 -0
- package/dist/core/i18n/match.d.mts +37 -0
- package/dist/core/i18n/match.mjs +82 -0
- package/dist/core/i18n/match.mjs.map +1 -0
- package/dist/core/i18n/types.d.mts +80 -0
- package/dist/core/index.d.mts +14 -5
- package/dist/core/index.mjs +9 -2
- package/dist/core/predictive-search/index.d.mts +2 -2
- package/dist/core/predictive-search/server-handlers.d.mts +27 -6
- package/dist/core/predictive-search/server-handlers.mjs +1 -4
- package/dist/core/predictive-search/server-handlers.mjs.map +1 -1
- package/dist/core/predictive-search/url.d.mts +2 -2
- package/dist/core/predictive-search/url.mjs.map +1 -1
- package/dist/core/product/accept-variant-id.mjs +1 -1
- package/dist/core/product/accept-variant-id.mjs.map +1 -1
- package/dist/core/request-context.d.mts +61 -35
- package/dist/core/request-context.mjs +11 -7
- package/dist/core/request-context.mjs.map +1 -1
- package/dist/core/request-routing/handle-shopify-routes.mjs.map +1 -1
- package/dist/core/request-routing/interceptors/api-proxy.mjs +9 -1
- package/dist/core/request-routing/interceptors/api-proxy.mjs.map +1 -1
- package/dist/core/request-routing/interceptors/checkout.mjs +11 -5
- package/dist/core/request-routing/interceptors/checkout.mjs.map +1 -1
- package/dist/core/request-routing/interceptors/standard-routes.mjs +1 -1
- package/dist/core/request-routing/interceptors/standard-routes.mjs.map +1 -1
- package/dist/core/request-routing/interceptors/url-redirects.mjs +2 -1
- package/dist/core/request-routing/interceptors/url-redirects.mjs.map +1 -1
- package/dist/core/request-routing/registered-routes.d.mts +10 -3
- package/dist/core/request-routing/registered-routes.mjs +13 -3
- package/dist/core/request-routing/registered-routes.mjs.map +1 -1
- package/dist/core/request-routing/route-types.d.mts +2 -2
- package/dist/core/shop-pay/shop-pay.mjs +5 -4
- package/dist/core/shop-pay/shop-pay.mjs.map +1 -1
- package/dist/core/shopify-scripts/analytics.mjs +3 -5
- package/dist/core/shopify-scripts/analytics.mjs.map +1 -1
- package/dist/core/shopify-scripts/constants.mjs +3 -1
- package/dist/core/shopify-scripts/constants.mjs.map +1 -1
- package/dist/core/shopify-scripts/global.mjs +13 -11
- package/dist/core/shopify-scripts/global.mjs.map +1 -1
- package/dist/core/shopify-scripts/hashes.d.mts +17 -0
- package/dist/core/shopify-scripts/hashes.mjs +12 -0
- package/dist/core/shopify-scripts/hashes.mjs.map +1 -0
- package/dist/core/shopify-scripts/index.d.mts +1 -0
- package/dist/core/shopify-scripts/index.mjs +12 -9
- package/dist/core/shopify-scripts/index.mjs.map +1 -1
- package/dist/core/shopify-scripts/page-view.mjs +4 -1
- package/dist/core/shopify-scripts/page-view.mjs.map +1 -1
- package/dist/core/shopify-scripts/types.d.mts +4 -2
- package/dist/core/shopify-scripts/utils/inline-script.mjs +4 -1
- package/dist/core/shopify-scripts/utils/inline-script.mjs.map +1 -1
- package/dist/core/standard-routes/build.d.mts +7 -2
- package/dist/core/standard-routes/build.mjs +10 -3
- package/dist/core/standard-routes/build.mjs.map +1 -1
- package/dist/core/standard-routes/match.mjs +11 -1
- package/dist/core/standard-routes/match.mjs.map +1 -1
- package/dist/core/standard-routes/path.mjs +14 -3
- package/dist/core/standard-routes/path.mjs.map +1 -1
- package/dist/core/standard-routes/types.d.mts +6 -1
- package/dist/core/url.mjs +5 -3
- package/dist/core/url.mjs.map +1 -1
- package/dist/customer-account/client.d.mts +14 -10
- package/dist/customer-account/client.mjs +4 -5
- package/dist/customer-account/client.mjs.map +1 -1
- package/dist/customer-account/graphql.d.mts +7 -1
- package/dist/customer-account/graphql.mjs +6 -7
- package/dist/customer-account/graphql.mjs.map +1 -1
- package/dist/customer-account/session.mjs +3 -4
- package/dist/customer-account/session.mjs.map +1 -1
- package/dist/development/client/client.mjs +44 -9
- package/dist/development/client/client.mjs.map +1 -1
- package/dist/development/client/i18n-variables.d.mts +1 -1
- package/dist/development/client/i18n-variables.mjs +13 -5
- package/dist/development/client/i18n-variables.mjs.map +1 -1
- package/dist/development/client/types.d.mts +12 -6
- package/dist/development/core/cache/fetch-with-cache.mjs +8 -1
- package/dist/development/core/cache/fetch-with-cache.mjs.map +1 -1
- package/dist/development/core/cart/cart.mjs +1 -1
- package/dist/development/core/cart/cookie.mjs +6 -1
- package/dist/development/core/cart/cookie.mjs.map +1 -1
- package/dist/development/core/cart/form.d.mts +17 -5
- package/dist/development/core/cart/form.mjs +5 -0
- package/dist/development/core/cart/form.mjs.map +1 -1
- package/dist/development/core/cart/index.d.mts +1 -1
- package/dist/development/core/cart/queries.d.mts +57 -57
- package/dist/development/core/collection/url.mjs +8 -1
- package/dist/development/core/collection/url.mjs.map +1 -1
- package/dist/development/core/constants.mjs +2 -1
- package/dist/development/core/constants.mjs.map +1 -1
- package/dist/development/core/csp/index.d.mts +56 -0
- package/dist/development/core/csp/index.mjs +128 -0
- package/dist/development/core/csp/index.mjs.map +1 -0
- package/dist/development/core/csp/types.d.mts +52 -0
- package/dist/development/core/development.d.mts +14 -5
- package/dist/development/core/development.mjs +9 -2
- package/dist/development/core/headers.mjs +2 -2
- package/dist/development/core/headers.mjs.map +1 -1
- package/dist/development/core/i18n/define.d.mts +54 -0
- package/dist/development/core/i18n/define.mjs +135 -0
- package/dist/development/core/i18n/define.mjs.map +1 -0
- package/dist/development/core/i18n/errors.d.mts +18 -0
- package/dist/development/core/i18n/errors.mjs +24 -0
- package/dist/development/core/i18n/errors.mjs.map +1 -0
- package/dist/development/core/i18n/href.d.mts +28 -0
- package/dist/development/core/i18n/href.mjs +45 -0
- package/dist/development/core/i18n/href.mjs.map +1 -0
- package/dist/development/core/i18n/locale.d.mts +21 -0
- package/dist/development/core/i18n/locale.mjs +36 -0
- package/dist/development/core/i18n/locale.mjs.map +1 -0
- package/dist/development/core/i18n/match.d.mts +37 -0
- package/dist/development/core/i18n/match.mjs +82 -0
- package/dist/development/core/i18n/match.mjs.map +1 -0
- package/dist/development/core/i18n/types.d.mts +80 -0
- package/dist/development/core/index.d.mts +13 -4
- package/dist/development/core/predictive-search/index.d.mts +2 -2
- package/dist/development/core/predictive-search/queries.d.mts +2 -2
- package/dist/development/core/predictive-search/server-handlers.d.mts +27 -6
- package/dist/development/core/predictive-search/server-handlers.mjs +1 -4
- package/dist/development/core/predictive-search/server-handlers.mjs.map +1 -1
- package/dist/development/core/predictive-search/url.d.mts +2 -2
- package/dist/development/core/predictive-search/url.mjs.map +1 -1
- package/dist/development/core/product/accept-variant-id.mjs +1 -1
- package/dist/development/core/product/accept-variant-id.mjs.map +1 -1
- package/dist/development/core/request-context.d.mts +61 -35
- package/dist/development/core/request-context.mjs +11 -7
- package/dist/development/core/request-context.mjs.map +1 -1
- package/dist/development/core/request-routing/handle-shopify-routes.mjs.map +1 -1
- package/dist/development/core/request-routing/interceptors/api-proxy.mjs +9 -1
- package/dist/development/core/request-routing/interceptors/api-proxy.mjs.map +1 -1
- package/dist/development/core/request-routing/interceptors/checkout.mjs +11 -5
- package/dist/development/core/request-routing/interceptors/checkout.mjs.map +1 -1
- package/dist/development/core/request-routing/interceptors/standard-routes.mjs +1 -1
- package/dist/development/core/request-routing/interceptors/standard-routes.mjs.map +1 -1
- package/dist/development/core/request-routing/interceptors/url-redirects.mjs +2 -1
- package/dist/development/core/request-routing/interceptors/url-redirects.mjs.map +1 -1
- package/dist/development/core/request-routing/registered-routes.d.mts +10 -3
- package/dist/development/core/request-routing/registered-routes.mjs +13 -3
- package/dist/development/core/request-routing/registered-routes.mjs.map +1 -1
- package/dist/development/core/request-routing/route-types.d.mts +2 -2
- package/dist/development/core/shop-pay/shop-pay.mjs +5 -4
- package/dist/development/core/shop-pay/shop-pay.mjs.map +1 -1
- package/dist/development/core/shopify-scripts/analytics.mjs +3 -5
- package/dist/development/core/shopify-scripts/analytics.mjs.map +1 -1
- package/dist/development/core/shopify-scripts/constants.mjs +3 -1
- package/dist/development/core/shopify-scripts/constants.mjs.map +1 -1
- package/dist/development/core/shopify-scripts/global.mjs +13 -11
- package/dist/development/core/shopify-scripts/global.mjs.map +1 -1
- package/dist/development/core/shopify-scripts/hashes.d.mts +17 -0
- package/dist/development/core/shopify-scripts/hashes.mjs +12 -0
- package/dist/development/core/shopify-scripts/hashes.mjs.map +1 -0
- package/dist/development/core/shopify-scripts/index.d.mts +1 -0
- package/dist/development/core/shopify-scripts/index.mjs +12 -9
- package/dist/development/core/shopify-scripts/index.mjs.map +1 -1
- package/dist/development/core/shopify-scripts/page-view.mjs +4 -1
- package/dist/development/core/shopify-scripts/page-view.mjs.map +1 -1
- package/dist/development/core/shopify-scripts/types.d.mts +4 -2
- package/dist/development/core/shopify-scripts/utils/inline-script.mjs +4 -1
- package/dist/development/core/shopify-scripts/utils/inline-script.mjs.map +1 -1
- package/dist/development/core/standard-routes/build.d.mts +7 -2
- package/dist/development/core/standard-routes/build.mjs +10 -3
- package/dist/development/core/standard-routes/build.mjs.map +1 -1
- package/dist/development/core/standard-routes/match.mjs +11 -1
- package/dist/development/core/standard-routes/match.mjs.map +1 -1
- package/dist/development/core/standard-routes/path.mjs +14 -3
- package/dist/development/core/standard-routes/path.mjs.map +1 -1
- package/dist/development/core/standard-routes/types.d.mts +6 -1
- package/dist/development/core/url.mjs +5 -3
- package/dist/development/core/url.mjs.map +1 -1
- package/dist/development/customer-account/session.mjs.map +1 -1
- package/dist/development/globals.d.mts +8 -4
- package/dist/development/graphql/type-resolver.d.mts +13 -1
- package/dist/development/react/index.d.mts +2 -2
- package/dist/globals.d.mts +8 -4
- package/dist/graphql/type-resolver.d.mts +13 -1
- package/dist/react/index.d.mts +2 -2
- package/dist/vite/index.mjs +1 -1
- package/dist/vue/shopify-scripts.d.mts +1 -1
- package/package.json +13 -3
- package/skills/hydrogen-analytics/SKILL.md +2 -2
- package/skills/hydrogen-analytics/references/react.md +2 -2
- package/skills/hydrogen-markets/SKILL.md +127 -93
- package/skills/hydrogen-markets/references/nextjs.md +77 -87
- package/skills/hydrogen-oxygen/references/react-router.md +1 -2
- package/skills/hydrogen-predictive-search/SKILL.md +18 -1
- package/skills/hydrogen-request-handlers/SKILL.md +23 -5
- package/skills/hydrogen-request-handlers/references/content-security-policy.md +103 -0
- package/skills/hydrogen-request-handlers/references/frameworks.md +8 -6
- package/skills/hydrogen-request-handlers/references/nextjs.md +7 -3
- package/skills/hydrogen-request-handlers/references/nuxt.md +7 -5
- package/skills/hydrogen-robots-txt/SKILL.md +87 -0
- package/skills/hydrogen-routing/SKILL.md +36 -14
- package/skills/hydrogen-setup/SKILL.md +2 -2
- package/skills/hydrogen-setup/references/analytics.md +5 -5
- package/skills/hydrogen-setup/steps/2-scaffold.md +3 -0
- package/skills/hydrogen-setup/steps/7-shopify-runtime-scripts.md +4 -3
- package/skills/hydrogen-setup/steps/9-account-page.md +1 -1
- package/skills/hydrogen-smoke-test/SKILL.md +9 -2
- package/skills/hydrogen-storefront-client/SKILL.md +27 -9
- package/skills/hydrogen-storefront-client/references/astro.md +8 -3
- package/skills/hydrogen-storefront-client/references/caching.md +2 -0
- package/skills/hydrogen-storefront-client/references/nextjs.md +8 -3
- package/skills/hydrogen-storefront-client/references/nuxt.md +9 -5
- package/skills/hydrogen-storefront-client/references/react-router.md +6 -3
- package/skills/hydrogen-storefront-client/references/solidstart.md +6 -2
- package/skills/hydrogen-storefront-client/references/sveltekit.md +10 -4
- package/skills/migrate-to-hydrogen-2026-10/SKILL.md +20 -22
- package/skills/migrate-to-hydrogen-2026-10/references/sitemap-and-seo.md +1 -0
- 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,
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
-
|
|
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
|
-
##
|
|
91
|
+
## Definition Shape
|
|
78
92
|
|
|
79
|
-
|
|
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
|
|
83
|
-
country:
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
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
|
-
##
|
|
111
|
+
## Path Prefixes
|
|
99
112
|
|
|
100
|
-
Use
|
|
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
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
|
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
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
-
##
|
|
160
|
+
## Locale Switchers And Alternate Links
|
|
140
161
|
|
|
141
|
-
Use
|
|
162
|
+
Use the exported helpers instead of string-building URLs:
|
|
142
163
|
|
|
143
164
|
```ts
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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()`
|
|
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
|
-
- **
|
|
193
|
-
- **Resolve the
|
|
194
|
-
- **
|
|
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
|
|
198
|
-
- **Keep route templates prefix-free.**
|
|
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.
|
|
201
|
-
- **Ask before choosing the
|
|
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
|
|
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
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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(
|
|
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
|
-
-
|
|
6
|
-
-
|
|
7
|
-
-
|
|
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.
|
|
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
|
-
-
|
|
12
|
-
-
|
|
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
|
-
|
|
17
|
+
---
|
|
20
18
|
|
|
21
|
-
|
|
19
|
+
## Definition
|
|
22
20
|
|
|
23
21
|
```ts
|
|
24
|
-
// lib/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
##
|
|
80
|
+
## Static Client
|
|
92
81
|
|
|
93
|
-
|
|
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/
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
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
|
|
117
|
+
import "server-only";
|
|
118
|
+
import {
|
|
119
|
+
createStorefrontClient,
|
|
120
|
+
createShopifyRequestContext,
|
|
121
|
+
matchLocale,
|
|
122
|
+
} from "@shopify/hydrogen";
|
|
123
123
|
|
|
124
|
-
import {
|
|
124
|
+
import { i18n } from "./config";
|
|
125
125
|
|
|
126
|
-
export function createStaticStorefrontClient(
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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.
|