@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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"headers.mjs","names":[],"sources":["../../../src/core/headers.ts"],"sourcesContent":["export const CACHE_CONTROL_HEADER = \"Cache-Control\";\nexport const CONSENT_MANAGEMENT_HEADER = \"Shopify-Storefront-Consent-Management\";\nexport const HYDROGEN_VERSION_HEADER = \"X-Hydrogen-Version\";\nexport const SURROGATE_CONTROL_HEADER = \"Surrogate-Control\";\nexport const REQUEST_GROUP_ID_HEADER = \"Custom-Storefront-Request-Group-ID\";\nexport const SDK_VARIANT_HEADER = \"X-SDK-Variant\";\nexport const SDK_VARIANT_SOURCE_HEADER = \"X-SDK-Variant-Source\";\nexport const SDK_VERSION_HEADER = \"X-SDK-Version\";\nexport const SEC_GPC_HEADER = \"Sec-GPC\";\nexport const SHOPIFY_STOREFRONT_ORIGIN_HEADER = \"Sec-Shopify-Storefront-Origin\";\nexport const STOREFRONT_ID_HEADER = \"Shopify-Storefront-Id\";\nexport const SHOPIFY_STOREFRONT_S_HEADER = \"Shopify-Storefront-S\";\nexport const SHOPIFY_STOREFRONT_Y_HEADER = \"Shopify-Storefront-Y\";\nexport const SHOPIFY_UNIQUE_TOKEN_HEADER = \"X-Shopify-UniqueToken\";\nexport const SHOPIFY_VISIT_TOKEN_HEADER = \"X-Shopify-VisitToken\";\nexport const STOREFRONT_ACCESS_TOKEN_HEADER = \"X-Shopify-Storefront-Access-Token\";\nexport const STOREFRONT_BUYER_IP_HEADER = \"Shopify-Storefront-Buyer-IP\";\nexport const STOREFRONT_PRIVATE_TOKEN_HEADER = \"Shopify-Storefront-Private-Token\";\nexport const STOREFRONT_URL_HEADER = \"x-storefront-url\";\n\nexport type StandardHeaderName =\n | \"accept\"\n | \"accept-encoding\"\n | \"accept-language\"\n | \"access-control-request-headers\"\n | \"access-control-request-method\"\n | typeof CACHE_CONTROL_HEADER\n | \"cf-connecting-ip\"\n | \"connection\"\n | \"content-length\"\n | \"content-type\"\n | \"cookie\"\n | \"host\"\n | \"keep-alive\"\n | \"origin\"\n | \"proxy-authenticate\"\n | \"proxy-authorization\"\n | \"referer\"\n | typeof SEC_GPC_HEADER\n | typeof SURROGATE_CONTROL_HEADER\n | \"te\"\n | \"trailer\"\n | \"transfer-encoding\"\n | \"upgrade\"\n | \"user-agent\"\n | \"x-requested-with\";\n\nexport type ShopifyHeaderName =\n | typeof CONSENT_MANAGEMENT_HEADER\n | typeof HYDROGEN_VERSION_HEADER\n | typeof REQUEST_GROUP_ID_HEADER\n | typeof SDK_VARIANT_HEADER\n | typeof SDK_VARIANT_SOURCE_HEADER\n | typeof SDK_VERSION_HEADER\n | typeof SHOPIFY_STOREFRONT_ORIGIN_HEADER\n | typeof SHOPIFY_STOREFRONT_S_HEADER\n | typeof SHOPIFY_STOREFRONT_Y_HEADER\n | typeof SHOPIFY_UNIQUE_TOKEN_HEADER\n | typeof SHOPIFY_VISIT_TOKEN_HEADER\n | typeof STOREFRONT_ACCESS_TOKEN_HEADER\n | typeof STOREFRONT_BUYER_IP_HEADER\n | typeof STOREFRONT_ID_HEADER\n | typeof STOREFRONT_PRIVATE_TOKEN_HEADER\n | typeof STOREFRONT_URL_HEADER;\n\ntype KnownHeaderName = StandardHeaderName | ShopifyHeaderName;\n\nexport function defineHeaderList<const HeaderNames extends readonly KnownHeaderName[]>(\n ...headerNames: HeaderNames\n): HeaderNames {\n return headerNames;\n}\n\nexport function extractHeaders(\n getter: (key: string) => string | null,\n allowlist: readonly string[],\n): [string, string][] {\n return allowlist.reduce<[string, string][]>((acc, key) => {\n const value = getter(key);\n if (value != null) acc.push([key, value]);\n return acc;\n }, []);\n}\n\nexport function applyPrivateResponseCacheHeaders(headers: Headers): void {\n headers.set(CACHE_CONTROL_HEADER, \"private, no-store, max-age=0, must-revalidate\");\n for (const header of Array.from(headers.keys())) {\n if (\n
|
|
1
|
+
{"version":3,"file":"headers.mjs","names":[],"sources":["../../../src/core/headers.ts"],"sourcesContent":["export const CACHE_CONTROL_HEADER = \"Cache-Control\";\nexport const CONSENT_MANAGEMENT_HEADER = \"Shopify-Storefront-Consent-Management\";\nexport const HYDROGEN_VERSION_HEADER = \"X-Hydrogen-Version\";\nexport const SURROGATE_CONTROL_HEADER = \"Surrogate-Control\";\nexport const REQUEST_GROUP_ID_HEADER = \"Custom-Storefront-Request-Group-ID\";\nexport const SDK_VARIANT_HEADER = \"X-SDK-Variant\";\nexport const SDK_VARIANT_SOURCE_HEADER = \"X-SDK-Variant-Source\";\nexport const SDK_VERSION_HEADER = \"X-SDK-Version\";\nexport const SEC_GPC_HEADER = \"Sec-GPC\";\nexport const SHOPIFY_STOREFRONT_ORIGIN_HEADER = \"Sec-Shopify-Storefront-Origin\";\nexport const STOREFRONT_ID_HEADER = \"Shopify-Storefront-Id\";\nexport const SHOPIFY_STOREFRONT_S_HEADER = \"Shopify-Storefront-S\";\nexport const SHOPIFY_STOREFRONT_Y_HEADER = \"Shopify-Storefront-Y\";\nexport const SHOPIFY_UNIQUE_TOKEN_HEADER = \"X-Shopify-UniqueToken\";\nexport const SHOPIFY_VISIT_TOKEN_HEADER = \"X-Shopify-VisitToken\";\nexport const STOREFRONT_ACCESS_TOKEN_HEADER = \"X-Shopify-Storefront-Access-Token\";\nexport const STOREFRONT_BUYER_IP_HEADER = \"Shopify-Storefront-Buyer-IP\";\nexport const STOREFRONT_PRIVATE_TOKEN_HEADER = \"Shopify-Storefront-Private-Token\";\nexport const STOREFRONT_URL_HEADER = \"x-storefront-url\";\n\nexport type StandardHeaderName =\n | \"accept\"\n | \"accept-encoding\"\n | \"accept-language\"\n | \"access-control-request-headers\"\n | \"access-control-request-method\"\n | typeof CACHE_CONTROL_HEADER\n | \"cf-connecting-ip\"\n | \"connection\"\n | \"content-length\"\n | \"content-type\"\n | \"cookie\"\n | \"forwarded\"\n | \"host\"\n | \"keep-alive\"\n | \"origin\"\n | \"oxygen-buyer-ip\"\n | \"proxy-authenticate\"\n | \"proxy-authorization\"\n | \"referer\"\n | typeof SEC_GPC_HEADER\n | typeof SURROGATE_CONTROL_HEADER\n | \"te\"\n | \"trailer\"\n | \"transfer-encoding\"\n | \"true-client-ip\"\n | \"upgrade\"\n | \"user-agent\"\n | \"x-client-ip\"\n | \"x-forwarded-for\"\n | \"x-forwarded-host\"\n | \"x-forwarded-port\"\n | \"x-forwarded-proto\"\n | \"x-forwarded-scheme\"\n | \"x-forwarded-ssl\"\n | \"x-real-ip\"\n | \"x-requested-with\"\n | \"x-shopify-client-ip\";\n\nexport type ShopifyHeaderName =\n | typeof CONSENT_MANAGEMENT_HEADER\n | typeof HYDROGEN_VERSION_HEADER\n | typeof REQUEST_GROUP_ID_HEADER\n | typeof SDK_VARIANT_HEADER\n | typeof SDK_VARIANT_SOURCE_HEADER\n | typeof SDK_VERSION_HEADER\n | typeof SHOPIFY_STOREFRONT_ORIGIN_HEADER\n | typeof SHOPIFY_STOREFRONT_S_HEADER\n | typeof SHOPIFY_STOREFRONT_Y_HEADER\n | typeof SHOPIFY_UNIQUE_TOKEN_HEADER\n | typeof SHOPIFY_VISIT_TOKEN_HEADER\n | typeof STOREFRONT_ACCESS_TOKEN_HEADER\n | typeof STOREFRONT_BUYER_IP_HEADER\n | typeof STOREFRONT_ID_HEADER\n | typeof STOREFRONT_PRIVATE_TOKEN_HEADER\n | typeof STOREFRONT_URL_HEADER;\n\ntype KnownHeaderName = StandardHeaderName | ShopifyHeaderName;\n\nexport function defineHeaderList<const HeaderNames extends readonly KnownHeaderName[]>(\n ...headerNames: HeaderNames\n): HeaderNames {\n return headerNames;\n}\n\nexport function extractHeaders(\n getter: (key: string) => string | null,\n allowlist: readonly string[],\n): [string, string][] {\n return allowlist.reduce<[string, string][]>((acc, key) => {\n const value = getter(key);\n if (value != null) acc.push([key, value]);\n return acc;\n }, []);\n}\n\nexport function applyPrivateResponseCacheHeaders(headers: Headers): void {\n headers.set(CACHE_CONTROL_HEADER, \"private, no-store, max-age=0, must-revalidate\");\n for (const header of Array.from(headers.keys())) {\n // Delete every vendor `*-Cache-Control` header (`CDN-`, `Cloudflare-CDN-`, `Oxygen-`, ...):\n // targeted caches such as Oxygen's full-page cache prefer their own header over\n // `Cache-Control`, so leaving one in place would override the private value set above.\n if (\n /^.+-cache-control$/i.test(header) ||\n header.toLowerCase() === SURROGATE_CONTROL_HEADER.toLowerCase()\n ) {\n headers.delete(header);\n }\n }\n}\n\nconst COMMON_PROXY_HEADER_ALLOWLIST = defineHeaderList(\n \"accept\",\n \"accept-encoding\",\n \"accept-language\",\n \"content-type\",\n \"cookie\",\n \"origin\",\n \"referer\",\n \"user-agent\",\n);\n\nexport const SFAPI_REQUEST_HEADER_ALLOWLIST = defineHeaderList(\n ...COMMON_PROXY_HEADER_ALLOWLIST,\n \"content-length\",\n \"access-control-request-headers\",\n \"access-control-request-method\",\n STOREFRONT_ACCESS_TOKEN_HEADER,\n STOREFRONT_PRIVATE_TOKEN_HEADER,\n SHOPIFY_UNIQUE_TOKEN_HEADER,\n SHOPIFY_VISIT_TOKEN_HEADER,\n);\n\nexport const MCP_REQUEST_HEADER_ALLOWLIST = defineHeaderList(...COMMON_PROXY_HEADER_ALLOWLIST);\n\nexport const AJAX_API_REQUEST_HEADER_ALLOWLIST = defineHeaderList(\n ...COMMON_PROXY_HEADER_ALLOWLIST,\n \"content-length\",\n \"x-requested-with\",\n);\n\n// Hop-by-hop headers must not be forwarded by proxies. `host` and\n// `content-length` are also removed so fetch can derive them for the upstream\n// request. See https://www.rfc-editor.org/rfc/rfc9110.html#section-7.6.1\n// Client IP and forwarding headers are removed because the caller controls them.\nexport const PROXY_REQUEST_HEADER_DENYLIST = defineHeaderList(\n \"cf-connecting-ip\",\n \"connection\",\n \"content-length\",\n \"forwarded\",\n \"host\",\n \"keep-alive\",\n \"oxygen-buyer-ip\",\n \"proxy-authenticate\",\n \"proxy-authorization\",\n STOREFRONT_BUYER_IP_HEADER,\n \"te\",\n \"trailer\",\n \"transfer-encoding\",\n \"true-client-ip\",\n \"upgrade\",\n \"x-client-ip\",\n \"x-forwarded-for\",\n \"x-forwarded-host\",\n \"x-forwarded-port\",\n \"x-forwarded-proto\",\n \"x-forwarded-scheme\",\n \"x-forwarded-ssl\",\n \"x-real-ip\",\n \"x-shopify-client-ip\",\n);\n"],"mappings":";AAAA,MAAa,uBAAuB;AACpC,MAAa,4BAA4B;AACzC,MAAa,0BAA0B;AACvC,MAAa,2BAA2B;AACxC,MAAa,0BAA0B;AACvC,MAAa,qBAAqB;AAClC,MAAa,4BAA4B;AACzC,MAAa,qBAAqB;AAClC,MAAa,iBAAiB;AAC9B,MAAa,mCAAmC;AAChD,MAAa,uBAAuB;AACpC,MAAa,8BAA8B;AAC3C,MAAa,8BAA8B;AAC3C,MAAa,8BAA8B;AAC3C,MAAa,6BAA6B;AAC1C,MAAa,iCAAiC;AAC9C,MAAa,6BAA6B;AAC1C,MAAa,kCAAkC;AAC/C,MAAa,wBAAwB;AA6DrC,SAAgB,iBACd,GAAG,aACU;AACb,QAAO;;AAGT,SAAgB,eACd,QACA,WACoB;AACpB,QAAO,UAAU,QAA4B,KAAK,QAAQ;EACxD,MAAM,QAAQ,OAAO,IAAI;AACzB,MAAI,SAAS,KAAM,KAAI,KAAK,CAAC,KAAK,MAAM,CAAC;AACzC,SAAO;IACN,EAAE,CAAC;;AAGR,SAAgB,iCAAiC,SAAwB;AACvE,SAAQ,IAAI,sBAAsB,gDAAgD;AAClF,MAAK,MAAM,UAAU,MAAM,KAAK,QAAQ,MAAM,CAAC,CAI7C,KACE,sBAAsB,KAAK,OAAO,IAClC,OAAO,aAAa,yBAA8B,aAAa,CAE/D,SAAQ,OAAO,OAAO;;AAK5B,MAAM,gCAAgC,iBACpC,UACA,mBACA,mBACA,gBACA,UACA,UACA,WACA,aACD;AAED,MAAa,iCAAiC,iBAC5C,GAAG,+BACH,kBACA,kCACA,iCACA,gCACA,iCACA,6BACA,2BACD;AAED,MAAa,+BAA+B,iBAAiB,GAAG,8BAA8B;AAE9F,MAAa,oCAAoC,iBAC/C,GAAG,+BACH,kBACA,mBACD;AAMD,MAAa,gCAAgC,iBAC3C,oBACA,cACA,kBACA,aACA,QACA,cACA,mBACA,sBACA,uBACA,4BACA,MACA,WACA,qBACA,kBACA,WACA,eACA,mBACA,oBACA,oBACA,qBACA,sBACA,mBACA,aACA,sBACD"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ShopifyI18n, ShopifySupportedLocale } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/core/i18n/define.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Defines the module-scope internationalization configuration for a storefront.
|
|
6
|
+
*
|
|
7
|
+
* Returns the input unchanged with its literal types preserved, so it is serializable and safe to
|
|
8
|
+
* import from server and client code alike. Pass it to `createShopifyRequestContext({i18n})` to
|
|
9
|
+
* resolve the request locale, and to the URL helpers (`getLocalizedHref`) to build links.
|
|
10
|
+
*
|
|
11
|
+
* Validation happens here, once at module load, so an invalid definition fails at startup rather
|
|
12
|
+
* than on the first request that would hit the bad entry.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* // Single locale: every request resolves to the default.
|
|
17
|
+
* export const i18n = defineShopifyI18n({
|
|
18
|
+
* defaultLocale: { language: "EN", country: "US" },
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* // Path-prefixed locales: `/fr-ca/products/x`, default served unprefixed.
|
|
22
|
+
* export const i18n = defineShopifyI18n({
|
|
23
|
+
* defaultLocale: { language: "EN", country: "US" },
|
|
24
|
+
* routing: {
|
|
25
|
+
* type: "pathname",
|
|
26
|
+
* locales: [
|
|
27
|
+
* { language: "FR", country: "CA" },
|
|
28
|
+
* { language: "PT_BR", country: "BR", pathSegment: "br" },
|
|
29
|
+
* ],
|
|
30
|
+
* },
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* // One hostname per locale; the default must be listed so it has a canonical host.
|
|
34
|
+
* export const i18n = defineShopifyI18n({
|
|
35
|
+
* defaultLocale: { language: "EN", country: "US" },
|
|
36
|
+
* routing: {
|
|
37
|
+
* type: "domain",
|
|
38
|
+
* locales: [
|
|
39
|
+
* { language: "EN", country: "US", hostname: "example.com" },
|
|
40
|
+
* { language: "FR", country: "CA", hostname: "fr.example.ca" },
|
|
41
|
+
* ],
|
|
42
|
+
* },
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare function defineShopifyI18n<const TI18n extends ShopifyI18n>(i18n: TI18n): TI18n;
|
|
47
|
+
/**
|
|
48
|
+
* Every locale the definition can resolve to, default first. Entries keep their routing keys
|
|
49
|
+
* (`pathSegment`, `hostname`) and any extra fields, so sitemaps and selectors can enumerate URLs.
|
|
50
|
+
*/
|
|
51
|
+
declare function getSupportedLocales<const TI18n extends ShopifyI18n>(i18n: TI18n): ShopifySupportedLocale<TI18n>[];
|
|
52
|
+
//#endregion
|
|
53
|
+
export { defineShopifyI18n, getSupportedLocales };
|
|
54
|
+
//# sourceMappingURL=define.d.mts.map
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { formatLocale, getLocaleHostname, getLocalePathSegment, isSameLocale } from "./locale.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/core/i18n/define.ts
|
|
4
|
+
const VALIDATION_URL_BASE = "https://shopify.local";
|
|
5
|
+
/**
|
|
6
|
+
* Defines the module-scope internationalization configuration for a storefront.
|
|
7
|
+
*
|
|
8
|
+
* Returns the input unchanged with its literal types preserved, so it is serializable and safe to
|
|
9
|
+
* import from server and client code alike. Pass it to `createShopifyRequestContext({i18n})` to
|
|
10
|
+
* resolve the request locale, and to the URL helpers (`getLocalizedHref`) to build links.
|
|
11
|
+
*
|
|
12
|
+
* Validation happens here, once at module load, so an invalid definition fails at startup rather
|
|
13
|
+
* than on the first request that would hit the bad entry.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* // Single locale: every request resolves to the default.
|
|
18
|
+
* export const i18n = defineShopifyI18n({
|
|
19
|
+
* defaultLocale: { language: "EN", country: "US" },
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* // Path-prefixed locales: `/fr-ca/products/x`, default served unprefixed.
|
|
23
|
+
* export const i18n = defineShopifyI18n({
|
|
24
|
+
* defaultLocale: { language: "EN", country: "US" },
|
|
25
|
+
* routing: {
|
|
26
|
+
* type: "pathname",
|
|
27
|
+
* locales: [
|
|
28
|
+
* { language: "FR", country: "CA" },
|
|
29
|
+
* { language: "PT_BR", country: "BR", pathSegment: "br" },
|
|
30
|
+
* ],
|
|
31
|
+
* },
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* // One hostname per locale; the default must be listed so it has a canonical host.
|
|
35
|
+
* export const i18n = defineShopifyI18n({
|
|
36
|
+
* defaultLocale: { language: "EN", country: "US" },
|
|
37
|
+
* routing: {
|
|
38
|
+
* type: "domain",
|
|
39
|
+
* locales: [
|
|
40
|
+
* { language: "EN", country: "US", hostname: "example.com" },
|
|
41
|
+
* { language: "FR", country: "CA", hostname: "fr.example.ca" },
|
|
42
|
+
* ],
|
|
43
|
+
* },
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
function defineShopifyI18n(i18n) {
|
|
48
|
+
assertLocale(i18n.defaultLocale, "defaultLocale");
|
|
49
|
+
const routing = i18n.routing;
|
|
50
|
+
if (!routing) return i18n;
|
|
51
|
+
if (routing.locales.length === 0) throw new Error(`defineShopifyI18n: routing.locales is empty. Omit "routing" for a single-locale storefront.`);
|
|
52
|
+
switch (routing.type) {
|
|
53
|
+
case "pathname":
|
|
54
|
+
validatePathnameLocales(i18n.defaultLocale, routing.locales);
|
|
55
|
+
break;
|
|
56
|
+
case "domain":
|
|
57
|
+
validateDomainLocales(i18n.defaultLocale, routing.locales);
|
|
58
|
+
break;
|
|
59
|
+
default: throw new Error(`defineShopifyI18n: unknown routing.type.`);
|
|
60
|
+
}
|
|
61
|
+
return i18n;
|
|
62
|
+
}
|
|
63
|
+
function getSupportedLocales(i18n) {
|
|
64
|
+
const routing = i18n.routing;
|
|
65
|
+
if (!routing) return [i18n.defaultLocale];
|
|
66
|
+
if (routing.type === "domain") {
|
|
67
|
+
const isDefault = (locale) => isSameLocale(locale, i18n.defaultLocale);
|
|
68
|
+
return [...routing.locales.filter(isDefault), ...routing.locales.filter((locale) => !isDefault(locale))];
|
|
69
|
+
}
|
|
70
|
+
return [i18n.defaultLocale, ...routing.locales];
|
|
71
|
+
}
|
|
72
|
+
function assertLocale(locale, label) {
|
|
73
|
+
if (!locale?.language || !locale?.country) throw new Error(`defineShopifyI18n: ${label} requires both "language" and "country".`);
|
|
74
|
+
}
|
|
75
|
+
function validatePathnameLocales(defaultLocale, locales) {
|
|
76
|
+
const seenSegments = new Map([[getLocalePathSegment(defaultLocale), defaultLocale]]);
|
|
77
|
+
const seenLocales = [];
|
|
78
|
+
for (const locale of locales) {
|
|
79
|
+
const label = `routing.locales entry ${formatLocale(locale)}`;
|
|
80
|
+
assertLocale(locale, label);
|
|
81
|
+
if (isSameLocale(locale, defaultLocale)) throw new Error(`defineShopifyI18n: defaultLocale ${formatLocale(defaultLocale)} must not appear in pathname routing.locales. It is served unprefixed so each page has one canonical URL.`);
|
|
82
|
+
if (seenLocales.some((seen) => isSameLocale(seen, locale))) throw new Error(`defineShopifyI18n: ${label} is listed more than once.`);
|
|
83
|
+
seenLocales.push(locale);
|
|
84
|
+
if (locale.pathSegment !== void 0 && !isSinglePathSegment(locale.pathSegment)) throw new Error(`defineShopifyI18n: ${label} has invalid pathSegment ${JSON.stringify(locale.pathSegment)}. Use one path segment that URL parsing leaves unchanged: no slashes, whitespace, "?", "#", "\\", or characters that need percent-encoding, for example "br".`);
|
|
85
|
+
const segment = getLocalePathSegment(locale);
|
|
86
|
+
const collision = seenSegments.get(segment);
|
|
87
|
+
if (collision) throw new Error(`defineShopifyI18n: ${label} and ${formatLocale(collision)} both resolve to the path segment "${segment}". Set a distinct pathSegment on one of them.`);
|
|
88
|
+
seenSegments.set(segment, locale);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Matching compares `URL.pathname` against the segment verbatim, so anything the URL parser
|
|
93
|
+
* normalizes away (`.`, `..`, `?`, `#`, `\`) or percent-encodes (whitespace, non-ASCII) would
|
|
94
|
+
* define fine but never match a request.
|
|
95
|
+
*/
|
|
96
|
+
function isSinglePathSegment(segment) {
|
|
97
|
+
if (segment === "" || segment.includes("/")) return false;
|
|
98
|
+
return new URL(`/${segment}`, VALIDATION_URL_BASE).pathname === `/${segment}`;
|
|
99
|
+
}
|
|
100
|
+
function validateDomainLocales(defaultLocale, locales) {
|
|
101
|
+
const seenHostnames = /* @__PURE__ */ new Map();
|
|
102
|
+
const seenLocales = [];
|
|
103
|
+
let defaultListed = false;
|
|
104
|
+
for (const locale of locales) {
|
|
105
|
+
const label = `routing.locales entry ${formatLocale(locale)}`;
|
|
106
|
+
assertLocale(locale, label);
|
|
107
|
+
if (seenLocales.some((seen) => isSameLocale(seen, locale))) throw new Error(`defineShopifyI18n: ${label} is listed more than once.`);
|
|
108
|
+
seenLocales.push(locale);
|
|
109
|
+
if (isSameLocale(locale, defaultLocale)) defaultListed = true;
|
|
110
|
+
const hostname = locale.hostname;
|
|
111
|
+
if (typeof hostname !== "string" || parseHostname(hostname) !== hostname.toLowerCase()) throw new Error(`defineShopifyI18n: ${label} has invalid hostname ${JSON.stringify(hostname)}. Use the bare hostname exactly as URL.hostname reports it (no scheme, port, or path; punycode for non-ASCII), for example "fr.example.com".`);
|
|
112
|
+
const normalizedHostname = getLocaleHostname(locale);
|
|
113
|
+
const collision = seenHostnames.get(normalizedHostname);
|
|
114
|
+
if (collision) throw new Error(`defineShopifyI18n: ${label} and ${formatLocale(collision)} both use hostname "${normalizedHostname}". Each hostname maps to exactly one locale.`);
|
|
115
|
+
seenHostnames.set(normalizedHostname, locale);
|
|
116
|
+
}
|
|
117
|
+
if (!defaultListed) throw new Error(`defineShopifyI18n: defaultLocale ${formatLocale(defaultLocale)} must appear in domain routing.locales so it has a canonical hostname.`);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Matching compares `URL.hostname`, which lowercases, strips userinfo/port/path, and encodes
|
|
121
|
+
* Unicode as punycode. Returns that canonical form, or `undefined` when the value is not a
|
|
122
|
+
* hostname at all, so the caller can reject anything that would define fine but never match.
|
|
123
|
+
*/
|
|
124
|
+
function parseHostname(hostname) {
|
|
125
|
+
if (hostname === "") return void 0;
|
|
126
|
+
try {
|
|
127
|
+
return new URL(`https://${hostname}`).hostname;
|
|
128
|
+
} catch {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
//#endregion
|
|
134
|
+
export { defineShopifyI18n, getSupportedLocales };
|
|
135
|
+
//# sourceMappingURL=define.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"define.mjs","names":[],"sources":["../../../../src/core/i18n/define.ts"],"sourcesContent":["import { formatLocale, getLocaleHostname, getLocalePathSegment, isSameLocale } from \"./locale\";\nimport type {\n ShopifyDomainLocale,\n ShopifyI18n,\n ShopifyLocale,\n ShopifyPathnameLocale,\n ShopifySupportedLocale,\n} from \"./types\";\n\nconst VALIDATION_URL_BASE = \"https://shopify.local\";\n\n/**\n * Defines the module-scope internationalization configuration for a storefront.\n *\n * Returns the input unchanged with its literal types preserved, so it is serializable and safe to\n * import from server and client code alike. Pass it to `createShopifyRequestContext({i18n})` to\n * resolve the request locale, and to the URL helpers (`getLocalizedHref`) to build links.\n *\n * Validation happens here, once at module load, so an invalid definition fails at startup rather\n * than on the first request that would hit the bad entry.\n *\n * @example\n * ```ts\n * // Single locale: every request resolves to the default.\n * export const i18n = defineShopifyI18n({\n * defaultLocale: { language: \"EN\", country: \"US\" },\n * });\n *\n * // Path-prefixed locales: `/fr-ca/products/x`, default served unprefixed.\n * export const i18n = defineShopifyI18n({\n * defaultLocale: { language: \"EN\", country: \"US\" },\n * routing: {\n * type: \"pathname\",\n * locales: [\n * { language: \"FR\", country: \"CA\" },\n * { language: \"PT_BR\", country: \"BR\", pathSegment: \"br\" },\n * ],\n * },\n * });\n *\n * // One hostname per locale; the default must be listed so it has a canonical host.\n * export const i18n = defineShopifyI18n({\n * defaultLocale: { language: \"EN\", country: \"US\" },\n * routing: {\n * type: \"domain\",\n * locales: [\n * { language: \"EN\", country: \"US\", hostname: \"example.com\" },\n * { language: \"FR\", country: \"CA\", hostname: \"fr.example.ca\" },\n * ],\n * },\n * });\n * ```\n */\nexport function defineShopifyI18n<const TI18n extends ShopifyI18n>(i18n: TI18n): TI18n {\n assertLocale(i18n.defaultLocale, \"defaultLocale\");\n\n const routing = i18n.routing;\n if (!routing) return i18n;\n\n if (routing.locales.length === 0) {\n throw new Error(\n `defineShopifyI18n: routing.locales is empty. Omit \"routing\" for a single-locale storefront.`,\n );\n }\n\n switch (routing.type) {\n case \"pathname\":\n validatePathnameLocales(i18n.defaultLocale, routing.locales);\n break;\n case \"domain\":\n validateDomainLocales(i18n.defaultLocale, routing.locales);\n break;\n default:\n routing satisfies never;\n throw new Error(`defineShopifyI18n: unknown routing.type.`);\n }\n\n return i18n;\n}\n\n/**\n * Every locale the definition can resolve to, default first. Entries keep their routing keys\n * (`pathSegment`, `hostname`) and any extra fields, so sitemaps and selectors can enumerate URLs.\n */\nexport function getSupportedLocales<const TI18n extends ShopifyI18n>(\n i18n: TI18n,\n): ShopifySupportedLocale<TI18n>[];\nexport function getSupportedLocales(i18n: ShopifyI18n): ShopifySupportedLocale[] {\n const routing = i18n.routing;\n if (!routing) return [i18n.defaultLocale];\n\n // Domain routing lists the default locale itself (it needs a hostname); pathname routing\n // keeps it out of the list so the default stays unprefixed.\n if (routing.type === \"domain\") {\n const isDefault = (locale: ShopifyLocale) => isSameLocale(locale, i18n.defaultLocale);\n return [\n ...routing.locales.filter(isDefault),\n ...routing.locales.filter((locale) => !isDefault(locale)),\n ];\n }\n\n return [i18n.defaultLocale, ...routing.locales];\n}\n\nfunction assertLocale(locale: ShopifyLocale | undefined, label: string): void {\n if (!locale?.language || !locale?.country) {\n throw new Error(`defineShopifyI18n: ${label} requires both \"language\" and \"country\".`);\n }\n}\n\nfunction validatePathnameLocales(\n defaultLocale: ShopifyLocale,\n locales: readonly ShopifyPathnameLocale[],\n): void {\n // The default locale is served unprefixed but keeps a segment for `[locale]` route params, so\n // no routed locale may reuse it.\n const seenSegments = new Map<string, ShopifyLocale>([\n [getLocalePathSegment(defaultLocale), defaultLocale],\n ]);\n const seenLocales: ShopifyLocale[] = [];\n\n for (const locale of locales) {\n const label = `routing.locales entry ${formatLocale(locale)}`;\n assertLocale(locale, label);\n\n if (isSameLocale(locale, defaultLocale)) {\n throw new Error(\n `defineShopifyI18n: defaultLocale ${formatLocale(defaultLocale)} must not appear in pathname routing.locales. It is served unprefixed so each page has one canonical URL.`,\n );\n }\n if (seenLocales.some((seen) => isSameLocale(seen, locale))) {\n throw new Error(`defineShopifyI18n: ${label} is listed more than once.`);\n }\n seenLocales.push(locale);\n\n if (locale.pathSegment !== undefined && !isSinglePathSegment(locale.pathSegment)) {\n throw new Error(\n `defineShopifyI18n: ${label} has invalid pathSegment ${JSON.stringify(locale.pathSegment)}. Use one path segment that URL parsing leaves unchanged: no slashes, whitespace, \"?\", \"#\", \"\\\\\", or characters that need percent-encoding, for example \"br\".`,\n );\n }\n\n const segment = getLocalePathSegment(locale);\n const collision = seenSegments.get(segment);\n if (collision) {\n throw new Error(\n `defineShopifyI18n: ${label} and ${formatLocale(collision)} both resolve to the path segment \"${segment}\". Set a distinct pathSegment on one of them.`,\n );\n }\n seenSegments.set(segment, locale);\n }\n}\n\n/**\n * Matching compares `URL.pathname` against the segment verbatim, so anything the URL parser\n * normalizes away (`.`, `..`, `?`, `#`, `\\`) or percent-encodes (whitespace, non-ASCII) would\n * define fine but never match a request.\n */\nfunction isSinglePathSegment(segment: string): boolean {\n if (segment === \"\" || segment.includes(\"/\")) return false;\n return new URL(`/${segment}`, VALIDATION_URL_BASE).pathname === `/${segment}`;\n}\n\nfunction validateDomainLocales(\n defaultLocale: ShopifyLocale,\n locales: readonly ShopifyDomainLocale[],\n): void {\n const seenHostnames = new Map<string, ShopifyDomainLocale>();\n const seenLocales: ShopifyLocale[] = [];\n let defaultListed = false;\n\n for (const locale of locales) {\n const label = `routing.locales entry ${formatLocale(locale)}`;\n assertLocale(locale, label);\n\n if (seenLocales.some((seen) => isSameLocale(seen, locale))) {\n throw new Error(`defineShopifyI18n: ${label} is listed more than once.`);\n }\n seenLocales.push(locale);\n if (isSameLocale(locale, defaultLocale)) defaultListed = true;\n\n const hostname = locale.hostname;\n if (typeof hostname !== \"string\" || parseHostname(hostname) !== hostname.toLowerCase()) {\n throw new Error(\n `defineShopifyI18n: ${label} has invalid hostname ${JSON.stringify(hostname)}. Use the bare hostname exactly as URL.hostname reports it (no scheme, port, or path; punycode for non-ASCII), for example \"fr.example.com\".`,\n );\n }\n\n const normalizedHostname = getLocaleHostname(locale);\n const collision = seenHostnames.get(normalizedHostname);\n if (collision) {\n throw new Error(\n `defineShopifyI18n: ${label} and ${formatLocale(collision)} both use hostname \"${normalizedHostname}\". Each hostname maps to exactly one locale.`,\n );\n }\n seenHostnames.set(normalizedHostname, locale);\n }\n\n if (!defaultListed) {\n throw new Error(\n `defineShopifyI18n: defaultLocale ${formatLocale(defaultLocale)} must appear in domain routing.locales so it has a canonical hostname.`,\n );\n }\n}\n\n/**\n * Matching compares `URL.hostname`, which lowercases, strips userinfo/port/path, and encodes\n * Unicode as punycode. Returns that canonical form, or `undefined` when the value is not a\n * hostname at all, so the caller can reject anything that would define fine but never match.\n */\nfunction parseHostname(hostname: string): string | undefined {\n if (hostname === \"\") return undefined;\n try {\n return new URL(`https://${hostname}`).hostname;\n } catch {\n return undefined;\n }\n}\n"],"mappings":";;;AASA,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4C5B,SAAgB,kBAAmD,MAAoB;AACrF,cAAa,KAAK,eAAe,gBAAgB;CAEjD,MAAM,UAAU,KAAK;AACrB,KAAI,CAAC,QAAS,QAAO;AAErB,KAAI,QAAQ,QAAQ,WAAW,EAC7B,OAAM,IAAI,MACR,8FACD;AAGH,SAAQ,QAAQ,MAAhB;EACE,KAAK;AACH,2BAAwB,KAAK,eAAe,QAAQ,QAAQ;AAC5D;EACF,KAAK;AACH,yBAAsB,KAAK,eAAe,QAAQ,QAAQ;AAC1D;EACF,QAEE,OAAM,IAAI,MAAM,2CAA2C;;AAG/D,QAAO;;AAUT,SAAgB,oBAAoB,MAA6C;CAC/E,MAAM,UAAU,KAAK;AACrB,KAAI,CAAC,QAAS,QAAO,CAAC,KAAK,cAAc;AAIzC,KAAI,QAAQ,SAAS,UAAU;EAC7B,MAAM,aAAa,WAA0B,aAAa,QAAQ,KAAK,cAAc;AACrF,SAAO,CACL,GAAG,QAAQ,QAAQ,OAAO,UAAU,EACpC,GAAG,QAAQ,QAAQ,QAAQ,WAAW,CAAC,UAAU,OAAO,CAAC,CAC1D;;AAGH,QAAO,CAAC,KAAK,eAAe,GAAG,QAAQ,QAAQ;;AAGjD,SAAS,aAAa,QAAmC,OAAqB;AAC5E,KAAI,CAAC,QAAQ,YAAY,CAAC,QAAQ,QAChC,OAAM,IAAI,MAAM,sBAAsB,MAAM,0CAA0C;;AAI1F,SAAS,wBACP,eACA,SACM;CAGN,MAAM,eAAe,IAAI,IAA2B,CAClD,CAAC,qBAAqB,cAAc,EAAE,cAAc,CACrD,CAAC;CACF,MAAM,cAA+B,EAAE;AAEvC,MAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,QAAQ,yBAAyB,aAAa,OAAO;AAC3D,eAAa,QAAQ,MAAM;AAE3B,MAAI,aAAa,QAAQ,cAAc,CACrC,OAAM,IAAI,MACR,oCAAoC,aAAa,cAAc,CAAC,2GACjE;AAEH,MAAI,YAAY,MAAM,SAAS,aAAa,MAAM,OAAO,CAAC,CACxD,OAAM,IAAI,MAAM,sBAAsB,MAAM,4BAA4B;AAE1E,cAAY,KAAK,OAAO;AAExB,MAAI,OAAO,gBAAgB,UAAa,CAAC,oBAAoB,OAAO,YAAY,CAC9E,OAAM,IAAI,MACR,sBAAsB,MAAM,2BAA2B,KAAK,UAAU,OAAO,YAAY,CAAC,+JAC3F;EAGH,MAAM,UAAU,qBAAqB,OAAO;EAC5C,MAAM,YAAY,aAAa,IAAI,QAAQ;AAC3C,MAAI,UACF,OAAM,IAAI,MACR,sBAAsB,MAAM,OAAO,aAAa,UAAU,CAAC,qCAAqC,QAAQ,+CACzG;AAEH,eAAa,IAAI,SAAS,OAAO;;;;;;;;AASrC,SAAS,oBAAoB,SAA0B;AACrD,KAAI,YAAY,MAAM,QAAQ,SAAS,IAAI,CAAE,QAAO;AACpD,QAAO,IAAI,IAAI,IAAI,WAAW,oBAAoB,CAAC,aAAa,IAAI;;AAGtE,SAAS,sBACP,eACA,SACM;CACN,MAAM,gCAAgB,IAAI,KAAkC;CAC5D,MAAM,cAA+B,EAAE;CACvC,IAAI,gBAAgB;AAEpB,MAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,QAAQ,yBAAyB,aAAa,OAAO;AAC3D,eAAa,QAAQ,MAAM;AAE3B,MAAI,YAAY,MAAM,SAAS,aAAa,MAAM,OAAO,CAAC,CACxD,OAAM,IAAI,MAAM,sBAAsB,MAAM,4BAA4B;AAE1E,cAAY,KAAK,OAAO;AACxB,MAAI,aAAa,QAAQ,cAAc,CAAE,iBAAgB;EAEzD,MAAM,WAAW,OAAO;AACxB,MAAI,OAAO,aAAa,YAAY,cAAc,SAAS,KAAK,SAAS,aAAa,CACpF,OAAM,IAAI,MACR,sBAAsB,MAAM,wBAAwB,KAAK,UAAU,SAAS,CAAC,8IAC9E;EAGH,MAAM,qBAAqB,kBAAkB,OAAO;EACpD,MAAM,YAAY,cAAc,IAAI,mBAAmB;AACvD,MAAI,UACF,OAAM,IAAI,MACR,sBAAsB,MAAM,OAAO,aAAa,UAAU,CAAC,sBAAsB,mBAAmB,8CACrG;AAEH,gBAAc,IAAI,oBAAoB,OAAO;;AAG/C,KAAI,CAAC,cACH,OAAM,IAAI,MACR,oCAAoC,aAAa,cAAc,CAAC,wEACjE;;;;;;;AASL,SAAS,cAAc,UAAsC;AAC3D,KAAI,aAAa,GAAI,QAAO;AAC5B,KAAI;AACF,SAAO,IAAI,IAAI,WAAW,WAAW,CAAC;SAChC;AACN"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ShopifyLocale } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/core/i18n/errors.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A locale (or its path segment) is not one of the definition's supported locales.
|
|
6
|
+
*
|
|
7
|
+
* Thrown by `resolveSupportedLocale`, and therefore by `createShopifyRequestContext({locale})`
|
|
8
|
+
* and `getLocalizedHref`. Catch it where an unknown locale is a routing outcome rather than a
|
|
9
|
+
* bug, for example to return a 404 from a `[locale]` route segment.
|
|
10
|
+
*/
|
|
11
|
+
declare class UnsupportedLocaleError extends Error {
|
|
12
|
+
readonly requested: ShopifyLocale | string;
|
|
13
|
+
readonly supported: readonly ShopifyLocale[];
|
|
14
|
+
constructor(requested: ShopifyLocale | string, supported: readonly ShopifyLocale[]);
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { UnsupportedLocaleError };
|
|
18
|
+
//# sourceMappingURL=errors.d.mts.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/core/i18n/errors.ts
|
|
2
|
+
/**
|
|
3
|
+
* A locale (or its path segment) is not one of the definition's supported locales.
|
|
4
|
+
*
|
|
5
|
+
* Thrown by `resolveSupportedLocale`, and therefore by `createShopifyRequestContext({locale})`
|
|
6
|
+
* and `getLocalizedHref`. Catch it where an unknown locale is a routing outcome rather than a
|
|
7
|
+
* bug, for example to return a 404 from a `[locale]` route segment.
|
|
8
|
+
*/
|
|
9
|
+
var UnsupportedLocaleError = class extends Error {
|
|
10
|
+
requested;
|
|
11
|
+
supported;
|
|
12
|
+
constructor(requested, supported) {
|
|
13
|
+
const requestedLabel = typeof requested === "string" ? `Path segment "${requested}"` : `Locale ${requested.language}-${requested.country}`;
|
|
14
|
+
const supportedLabel = supported.map((l) => `${l.language}-${l.country}`).join(", ");
|
|
15
|
+
super(`${requestedLabel} is not defined in this storefront's i18n. Supported locales: ${supportedLabel}.`);
|
|
16
|
+
this.name = "UnsupportedLocaleError";
|
|
17
|
+
this.requested = requested;
|
|
18
|
+
this.supported = supported;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { UnsupportedLocaleError };
|
|
24
|
+
//# sourceMappingURL=errors.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.mjs","names":[],"sources":["../../../../src/core/i18n/errors.ts"],"sourcesContent":["import type { ShopifyLocale } from \"./types\";\n\n/**\n * A locale (or its path segment) is not one of the definition's supported locales.\n *\n * Thrown by `resolveSupportedLocale`, and therefore by `createShopifyRequestContext({locale})`\n * and `getLocalizedHref`. Catch it where an unknown locale is a routing outcome rather than a\n * bug, for example to return a 404 from a `[locale]` route segment.\n */\nexport class UnsupportedLocaleError extends Error {\n readonly requested: ShopifyLocale | string;\n readonly supported: readonly ShopifyLocale[];\n\n constructor(requested: ShopifyLocale | string, supported: readonly ShopifyLocale[]) {\n const requestedLabel =\n typeof requested === \"string\"\n ? `Path segment \"${requested}\"`\n : `Locale ${requested.language}-${requested.country}`;\n const supportedLabel = supported.map((l) => `${l.language}-${l.country}`).join(\", \");\n super(\n `${requestedLabel} is not defined in this storefront's i18n. Supported locales: ${supportedLabel}.`,\n );\n this.name = \"UnsupportedLocaleError\";\n this.requested = requested;\n this.supported = supported;\n }\n}\n"],"mappings":";;;;;;;;AASA,IAAa,yBAAb,cAA4C,MAAM;CAChD,AAAS;CACT,AAAS;CAET,YAAY,WAAmC,WAAqC;EAClF,MAAM,iBACJ,OAAO,cAAc,WACjB,iBAAiB,UAAU,KAC3B,UAAU,UAAU,SAAS,GAAG,UAAU;EAChD,MAAM,iBAAiB,UAAU,KAAK,MAAM,GAAG,EAAE,SAAS,GAAG,EAAE,UAAU,CAAC,KAAK,KAAK;AACpF,QACE,GAAG,eAAe,gEAAgE,eAAe,GAClG;AACD,OAAK,OAAO;AACZ,OAAK,YAAY;AACjB,OAAK,YAAY"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ShopifyI18n, ShopifyLocale } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/core/i18n/href.d.ts
|
|
4
|
+
type GetLocalizedHrefOptions = {
|
|
5
|
+
i18n: ShopifyI18n; /** Target locale. Must be one of the definition's supported locales. */
|
|
6
|
+
locale: ShopifyLocale;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Rewrites an href so it points at the same page in another locale.
|
|
10
|
+
*
|
|
11
|
+
* The result is always valid for `href` or a `Location` header, but its shape follows the routing
|
|
12
|
+
* strategy:
|
|
13
|
+
* - pathname routing returns a path (`/fr-ca/products/x`); any existing locale prefix is replaced.
|
|
14
|
+
* - domain routing returns an absolute URL on the target locale's hostname.
|
|
15
|
+
* - no routing returns the href unchanged.
|
|
16
|
+
*
|
|
17
|
+
* Absolute inputs keep their scheme; relative inputs stay relative under pathname routing and use
|
|
18
|
+
* `https:` under domain routing.
|
|
19
|
+
*
|
|
20
|
+
* @throws when `locale` is not defined in `i18n`.
|
|
21
|
+
*/
|
|
22
|
+
declare function getLocalizedHref(href: string, {
|
|
23
|
+
i18n,
|
|
24
|
+
locale
|
|
25
|
+
}: GetLocalizedHrefOptions): string;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { GetLocalizedHrefOptions, getLocalizedHref };
|
|
28
|
+
//# sourceMappingURL=href.d.mts.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { isAbsoluteUrl, stripI18nPathPrefix } from "../standard-routes/path.mjs";
|
|
2
|
+
import { matchLocale, resolveSupportedLocale } from "./match.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/core/i18n/href.ts
|
|
5
|
+
const RELATIVE_URL_BASE = "https://shopify.local";
|
|
6
|
+
/**
|
|
7
|
+
* Rewrites an href so it points at the same page in another locale.
|
|
8
|
+
*
|
|
9
|
+
* The result is always valid for `href` or a `Location` header, but its shape follows the routing
|
|
10
|
+
* strategy:
|
|
11
|
+
* - pathname routing returns a path (`/fr-ca/products/x`); any existing locale prefix is replaced.
|
|
12
|
+
* - domain routing returns an absolute URL on the target locale's hostname.
|
|
13
|
+
* - no routing returns the href unchanged.
|
|
14
|
+
*
|
|
15
|
+
* Absolute inputs keep their scheme; relative inputs stay relative under pathname routing and use
|
|
16
|
+
* `https:` under domain routing.
|
|
17
|
+
*
|
|
18
|
+
* @throws when `locale` is not defined in `i18n`.
|
|
19
|
+
*/
|
|
20
|
+
function getLocalizedHref(href, { i18n, locale }) {
|
|
21
|
+
const target = resolveSupportedLocale(locale, i18n);
|
|
22
|
+
const routing = i18n.routing;
|
|
23
|
+
if (!routing) return href;
|
|
24
|
+
const absolute = isAbsoluteUrl(href);
|
|
25
|
+
const url = new URL(href, RELATIVE_URL_BASE);
|
|
26
|
+
switch (routing.type) {
|
|
27
|
+
case "pathname": {
|
|
28
|
+
const current = matchLocale(url, i18n);
|
|
29
|
+
const unprefixedPathname = stripI18nPathPrefix(url.pathname, current.pathPrefix);
|
|
30
|
+
url.pathname = unprefixedPathname === "/" ? target.pathPrefix || "/" : `${target.pathPrefix}${unprefixedPathname}`;
|
|
31
|
+
return absolute ? url.toString() : `${url.pathname}${url.search}${url.hash}`;
|
|
32
|
+
}
|
|
33
|
+
case "domain":
|
|
34
|
+
if (!("hostname" in target) || typeof target.hostname !== "string") throw new Error(`getLocalizedHref: locale ${target.language}-${target.country} has no hostname under domain routing.`);
|
|
35
|
+
url.hostname = target.hostname;
|
|
36
|
+
url.port = "";
|
|
37
|
+
if (!absolute) url.protocol = "https:";
|
|
38
|
+
return url.toString();
|
|
39
|
+
default: return href;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
export { getLocalizedHref };
|
|
45
|
+
//# sourceMappingURL=href.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"href.mjs","names":[],"sources":["../../../../src/core/i18n/href.ts"],"sourcesContent":["import { isAbsoluteUrl, stripI18nPathPrefix } from \"../standard-routes/path\";\nimport { matchLocale, resolveSupportedLocale } from \"./match\";\nimport type { ShopifyI18n, ShopifyLocale } from \"./types\";\n\nconst RELATIVE_URL_BASE = \"https://shopify.local\";\n\nexport type GetLocalizedHrefOptions = {\n i18n: ShopifyI18n;\n /** Target locale. Must be one of the definition's supported locales. */\n locale: ShopifyLocale;\n};\n\n/**\n * Rewrites an href so it points at the same page in another locale.\n *\n * The result is always valid for `href` or a `Location` header, but its shape follows the routing\n * strategy:\n * - pathname routing returns a path (`/fr-ca/products/x`); any existing locale prefix is replaced.\n * - domain routing returns an absolute URL on the target locale's hostname.\n * - no routing returns the href unchanged.\n *\n * Absolute inputs keep their scheme; relative inputs stay relative under pathname routing and use\n * `https:` under domain routing.\n *\n * @throws when `locale` is not defined in `i18n`.\n */\nexport function getLocalizedHref(href: string, { i18n, locale }: GetLocalizedHrefOptions): string {\n // Validate first so an unsupported locale throws regardless of routing strategy.\n const target = resolveSupportedLocale(locale, i18n);\n\n const routing = i18n.routing;\n if (!routing) return href;\n\n const absolute = isAbsoluteUrl(href);\n const url = new URL(href, RELATIVE_URL_BASE);\n\n switch (routing.type) {\n case \"pathname\": {\n const current = matchLocale(url, i18n);\n const unprefixedPathname = stripI18nPathPrefix(url.pathname, current.pathPrefix);\n // Keep the locale root free of a trailing slash so `/fr-ca` stays the one canonical URL.\n url.pathname =\n unprefixedPathname === \"/\"\n ? target.pathPrefix || \"/\"\n : `${target.pathPrefix}${unprefixedPathname}`;\n\n return absolute ? url.toString() : `${url.pathname}${url.search}${url.hash}`;\n }\n case \"domain\": {\n // Define-time validation guarantees every domain-routed locale carries a hostname.\n if (!(\"hostname\" in target) || typeof target.hostname !== \"string\") {\n throw new Error(\n `getLocalizedHref: locale ${target.language}-${target.country} has no hostname under domain routing.`,\n );\n }\n url.hostname = target.hostname;\n url.port = \"\";\n if (!absolute) url.protocol = \"https:\";\n\n return url.toString();\n }\n default:\n routing satisfies never;\n return href;\n }\n}\n"],"mappings":";;;;AAIA,MAAM,oBAAoB;;;;;;;;;;;;;;;AAsB1B,SAAgB,iBAAiB,MAAc,EAAE,MAAM,UAA2C;CAEhG,MAAM,SAAS,uBAAuB,QAAQ,KAAK;CAEnD,MAAM,UAAU,KAAK;AACrB,KAAI,CAAC,QAAS,QAAO;CAErB,MAAM,WAAW,cAAc,KAAK;CACpC,MAAM,MAAM,IAAI,IAAI,MAAM,kBAAkB;AAE5C,SAAQ,QAAQ,MAAhB;EACE,KAAK,YAAY;GACf,MAAM,UAAU,YAAY,KAAK,KAAK;GACtC,MAAM,qBAAqB,oBAAoB,IAAI,UAAU,QAAQ,WAAW;AAEhF,OAAI,WACF,uBAAuB,MACnB,OAAO,cAAc,MACrB,GAAG,OAAO,aAAa;AAE7B,UAAO,WAAW,IAAI,UAAU,GAAG,GAAG,IAAI,WAAW,IAAI,SAAS,IAAI;;EAExE,KAAK;AAEH,OAAI,EAAE,cAAc,WAAW,OAAO,OAAO,aAAa,SACxD,OAAM,IAAI,MACR,4BAA4B,OAAO,SAAS,GAAG,OAAO,QAAQ,wCAC/D;AAEH,OAAI,WAAW,OAAO;AACtB,OAAI,OAAO;AACX,OAAI,CAAC,SAAU,KAAI,WAAW;AAE9B,UAAO,IAAI,UAAU;EAEvB,QAEE,QAAO"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ShopifyLocale, ShopifyMatchedLocale, ShopifyPathnameLocale } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/core/i18n/locale.d.ts
|
|
4
|
+
declare function isSameLocale(a: ShopifyLocale, b: ShopifyLocale): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* The URL path segment that identifies a locale: `pathSegment` when set, otherwise
|
|
7
|
+
* `{language}-{country}` lowercased with `_` replaced by `-` (`FR`/`CA` → `fr-ca`, `PT_BR`/`BR` →
|
|
8
|
+
* `pt-br-br`).
|
|
9
|
+
*
|
|
10
|
+
* Defined for every locale regardless of routing type, so it doubles as a stable identifier
|
|
11
|
+
* where the URL cannot carry the locale itself: a `[locale]` route param that static rendering
|
|
12
|
+
* derives the locale from, or the internal path a domain-routed request is rewritten to.
|
|
13
|
+
* `resolveSupportedLocale` accepts this segment back.
|
|
14
|
+
*
|
|
15
|
+
* Accepts matched locales too: `matchLocale` replaces `pathSegment` with the derived
|
|
16
|
+
* `pathPrefix`, so a non-empty prefix is the segment's source of truth there.
|
|
17
|
+
*/
|
|
18
|
+
declare function getLocalePathSegment(locale: ShopifyPathnameLocale | ShopifyMatchedLocale): string;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { getLocalePathSegment, isSameLocale };
|
|
21
|
+
//# sourceMappingURL=locale.d.mts.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/core/i18n/locale.ts
|
|
2
|
+
function isSameLocale(a, b) {
|
|
3
|
+
return a.language === b.language && a.country === b.country;
|
|
4
|
+
}
|
|
5
|
+
function formatLocale(locale) {
|
|
6
|
+
return `${locale.language}-${locale.country}`;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The URL path segment that identifies a locale: `pathSegment` when set, otherwise
|
|
10
|
+
* `{language}-{country}` lowercased with `_` replaced by `-` (`FR`/`CA` → `fr-ca`, `PT_BR`/`BR` →
|
|
11
|
+
* `pt-br-br`).
|
|
12
|
+
*
|
|
13
|
+
* Defined for every locale regardless of routing type, so it doubles as a stable identifier
|
|
14
|
+
* where the URL cannot carry the locale itself: a `[locale]` route param that static rendering
|
|
15
|
+
* derives the locale from, or the internal path a domain-routed request is rewritten to.
|
|
16
|
+
* `resolveSupportedLocale` accepts this segment back.
|
|
17
|
+
*
|
|
18
|
+
* Accepts matched locales too: `matchLocale` replaces `pathSegment` with the derived
|
|
19
|
+
* `pathPrefix`, so a non-empty prefix is the segment's source of truth there.
|
|
20
|
+
*/
|
|
21
|
+
function getLocalePathSegment(locale) {
|
|
22
|
+
if ("pathSegment" in locale && locale.pathSegment !== void 0) return locale.pathSegment.toLowerCase();
|
|
23
|
+
if ("pathPrefix" in locale && locale.pathPrefix !== "") return locale.pathPrefix.slice(1).toLowerCase();
|
|
24
|
+
return deriveLocalePathSegment(locale);
|
|
25
|
+
}
|
|
26
|
+
/** The locale's hostname in the form `URL.hostname` reports it. */
|
|
27
|
+
function getLocaleHostname(locale) {
|
|
28
|
+
return locale.hostname.toLowerCase();
|
|
29
|
+
}
|
|
30
|
+
function deriveLocalePathSegment(locale) {
|
|
31
|
+
return `${locale.language.replaceAll("_", "-")}-${locale.country}`.toLowerCase();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { formatLocale, getLocaleHostname, getLocalePathSegment, isSameLocale };
|
|
36
|
+
//# sourceMappingURL=locale.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locale.mjs","names":[],"sources":["../../../../src/core/i18n/locale.ts"],"sourcesContent":["import type {\n ShopifyDomainLocale,\n ShopifyLocale,\n ShopifyMatchedLocale,\n ShopifyPathnameLocale,\n} from \"./types\";\n\nexport function isSameLocale(a: ShopifyLocale, b: ShopifyLocale): boolean {\n return a.language === b.language && a.country === b.country;\n}\n\nexport function formatLocale(locale: ShopifyLocale): string {\n return `${locale.language}-${locale.country}`;\n}\n\n/**\n * The URL path segment that identifies a locale: `pathSegment` when set, otherwise\n * `{language}-{country}` lowercased with `_` replaced by `-` (`FR`/`CA` → `fr-ca`, `PT_BR`/`BR` →\n * `pt-br-br`).\n *\n * Defined for every locale regardless of routing type, so it doubles as a stable identifier\n * where the URL cannot carry the locale itself: a `[locale]` route param that static rendering\n * derives the locale from, or the internal path a domain-routed request is rewritten to.\n * `resolveSupportedLocale` accepts this segment back.\n *\n * Accepts matched locales too: `matchLocale` replaces `pathSegment` with the derived\n * `pathPrefix`, so a non-empty prefix is the segment's source of truth there.\n */\nexport function getLocalePathSegment(locale: ShopifyPathnameLocale | ShopifyMatchedLocale): string {\n if (\"pathSegment\" in locale && locale.pathSegment !== undefined) {\n return locale.pathSegment.toLowerCase();\n }\n if (\"pathPrefix\" in locale && locale.pathPrefix !== \"\") {\n return locale.pathPrefix.slice(1).toLowerCase();\n }\n return deriveLocalePathSegment(locale);\n}\n\n/** The locale's hostname in the form `URL.hostname` reports it. */\nexport function getLocaleHostname(locale: ShopifyDomainLocale): string {\n return locale.hostname.toLowerCase();\n}\n\nfunction deriveLocalePathSegment(locale: ShopifyLocale): string {\n return `${locale.language.replaceAll(\"_\", \"-\")}-${locale.country}`.toLowerCase();\n}\n"],"mappings":";AAOA,SAAgB,aAAa,GAAkB,GAA2B;AACxE,QAAO,EAAE,aAAa,EAAE,YAAY,EAAE,YAAY,EAAE;;AAGtD,SAAgB,aAAa,QAA+B;AAC1D,QAAO,GAAG,OAAO,SAAS,GAAG,OAAO;;;;;;;;;;;;;;;AAgBtC,SAAgB,qBAAqB,QAA8D;AACjG,KAAI,iBAAiB,UAAU,OAAO,gBAAgB,OACpD,QAAO,OAAO,YAAY,aAAa;AAEzC,KAAI,gBAAgB,UAAU,OAAO,eAAe,GAClD,QAAO,OAAO,WAAW,MAAM,EAAE,CAAC,aAAa;AAEjD,QAAO,wBAAwB,OAAO;;;AAIxC,SAAgB,kBAAkB,QAAqC;AACrE,QAAO,OAAO,SAAS,aAAa;;AAGtC,SAAS,wBAAwB,QAA+B;AAC9D,QAAO,GAAG,OAAO,SAAS,WAAW,KAAK,IAAI,CAAC,GAAG,OAAO,UAAU,aAAa"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ShopifyI18n, ShopifyLocale, ShopifyMatchedLocale } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/core/i18n/match.d.ts
|
|
4
|
+
/** Anything the locale can be matched from: a `Request`, a `URL`, or a URL or pathname string. */
|
|
5
|
+
type LocaleMatchSource = Pick<Request, "url"> | URL | string;
|
|
6
|
+
/**
|
|
7
|
+
* Resolves the locale of a request URL according to the definition's routing.
|
|
8
|
+
*
|
|
9
|
+
* Pure and synchronous: the URL is the only input, so the same URL always resolves to the same
|
|
10
|
+
* locale. Unknown prefixes or hostnames, and a missing URL, resolve to `defaultLocale`. Pathnames
|
|
11
|
+
* such as `/fr-ca/products/x` are matched under pathname routing; domain routing needs an
|
|
12
|
+
* absolute URL to read the hostname from.
|
|
13
|
+
*
|
|
14
|
+
* @throws when the source is a string that is neither a URL nor a pathname.
|
|
15
|
+
*
|
|
16
|
+
* Only the first path segment (pathname routing) or the hostname (domain routing) is read. The
|
|
17
|
+
* URL is matched as-is: framework-specific rewrites of that segment must be normalized by the
|
|
18
|
+
* caller. React Router single-fetch requests the locale root as `/fr-ca.data`, for example, so
|
|
19
|
+
* strip the suffix before matching or pass the resolved locale to `createShopifyRequestContext`.
|
|
20
|
+
*/
|
|
21
|
+
declare function matchLocale<const TI18n extends ShopifyI18n>(source: LocaleMatchSource | undefined, i18n: TI18n): ShopifyMatchedLocale<TI18n>;
|
|
22
|
+
/**
|
|
23
|
+
* Resolves an explicitly chosen locale against the definition, for callers that already know the
|
|
24
|
+
* locale and must not derive it from a URL: static rendering, locale switchers, or a `[locale]`
|
|
25
|
+
* route param.
|
|
26
|
+
*
|
|
27
|
+
* Accepts either a `{language, country}` pair or the locale's path segment as returned by
|
|
28
|
+
* `getLocalePathSegment` (case-insensitive), so a route param can be resolved directly.
|
|
29
|
+
*
|
|
30
|
+
* @throws {UnsupportedLocaleError} when the locale or segment is not one of the definition's
|
|
31
|
+
* supported locales, so a stale or mistyped choice fails fast instead of silently producing an
|
|
32
|
+
* unroutable URL or request context. Catch it to turn an unknown route param into a 404.
|
|
33
|
+
*/
|
|
34
|
+
declare function resolveSupportedLocale<const TI18n extends ShopifyI18n>(locale: ShopifyLocale | string, i18n: TI18n): ShopifyMatchedLocale<TI18n>;
|
|
35
|
+
//#endregion
|
|
36
|
+
export { LocaleMatchSource, matchLocale, resolveSupportedLocale };
|
|
37
|
+
//# sourceMappingURL=match.d.mts.map
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { getLocaleHostname, getLocalePathSegment, isSameLocale } from "./locale.mjs";
|
|
2
|
+
import { getSupportedLocales } from "./define.mjs";
|
|
3
|
+
import { UnsupportedLocaleError } from "./errors.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/core/i18n/match.ts
|
|
6
|
+
const RELATIVE_URL_BASE = "https://shopify.local";
|
|
7
|
+
function matchLocale(source, i18n) {
|
|
8
|
+
const routing = i18n.routing;
|
|
9
|
+
if (!routing) return toDefaultMatchedLocale(i18n);
|
|
10
|
+
const url = toUrl(source);
|
|
11
|
+
if (!url) return toDefaultMatchedLocale(i18n);
|
|
12
|
+
switch (routing.type) {
|
|
13
|
+
case "pathname": {
|
|
14
|
+
const segment = getFirstPathSegment(url.pathname);
|
|
15
|
+
const locale = segment === "" ? void 0 : routing.locales.find((candidate) => getLocalePathSegment(candidate) === segment);
|
|
16
|
+
return locale ? toPathnameMatchedLocale(locale) : toDefaultMatchedLocale(i18n);
|
|
17
|
+
}
|
|
18
|
+
case "domain": {
|
|
19
|
+
const hostname = url.hostname.toLowerCase();
|
|
20
|
+
const locale = routing.locales.find((candidate) => getLocaleHostname(candidate) === hostname);
|
|
21
|
+
return locale ? toDomainMatchedLocale(locale) : toDefaultMatchedLocale(i18n);
|
|
22
|
+
}
|
|
23
|
+
default: return toDefaultMatchedLocale(i18n);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function resolveSupportedLocale(locale, i18n) {
|
|
27
|
+
const matches = typeof locale === "string" ? (candidate) => getLocalePathSegment(candidate) === locale.toLowerCase() : (candidate) => isSameLocale(candidate, locale);
|
|
28
|
+
if (matches(i18n.defaultLocale)) return toDefaultMatchedLocale(i18n);
|
|
29
|
+
const routing = i18n.routing;
|
|
30
|
+
switch (routing?.type) {
|
|
31
|
+
case "pathname": {
|
|
32
|
+
const match = routing.locales.find(matches);
|
|
33
|
+
if (match) return toPathnameMatchedLocale(match);
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
case "domain": {
|
|
37
|
+
const match = routing.locales.find(matches);
|
|
38
|
+
if (match) return toDomainMatchedLocale(match);
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
case void 0: break;
|
|
42
|
+
default:
|
|
43
|
+
}
|
|
44
|
+
throw new UnsupportedLocaleError(locale, getSupportedLocales(i18n));
|
|
45
|
+
}
|
|
46
|
+
function toDefaultMatchedLocale(i18n) {
|
|
47
|
+
return {
|
|
48
|
+
...i18n.routing?.type === "domain" ? i18n.routing.locales.find((candidate) => isSameLocale(candidate, i18n.defaultLocale)) ?? i18n.defaultLocale : i18n.defaultLocale,
|
|
49
|
+
pathPrefix: ""
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function toPathnameMatchedLocale(locale) {
|
|
53
|
+
const { pathSegment: _pathSegment, ...rest } = locale;
|
|
54
|
+
return {
|
|
55
|
+
...rest,
|
|
56
|
+
pathPrefix: `/${getLocalePathSegment(locale)}`
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function toDomainMatchedLocale(locale) {
|
|
60
|
+
return {
|
|
61
|
+
...locale,
|
|
62
|
+
pathPrefix: ""
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function getFirstPathSegment(pathname) {
|
|
66
|
+
const [segment = ""] = pathname.replace(/^\/+/, "").split("/", 1);
|
|
67
|
+
return segment.toLowerCase();
|
|
68
|
+
}
|
|
69
|
+
function toUrl(source) {
|
|
70
|
+
if (source === void 0) return null;
|
|
71
|
+
if (source instanceof URL) return source;
|
|
72
|
+
const url = typeof source === "string" ? source : source.url;
|
|
73
|
+
try {
|
|
74
|
+
return new URL(url, RELATIVE_URL_BASE);
|
|
75
|
+
} catch {
|
|
76
|
+
throw new Error(`matchLocale: ${JSON.stringify(url)} is not a URL or pathname.`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
//#endregion
|
|
81
|
+
export { matchLocale, resolveSupportedLocale };
|
|
82
|
+
//# sourceMappingURL=match.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"match.mjs","names":[],"sources":["../../../../src/core/i18n/match.ts"],"sourcesContent":["import { getSupportedLocales } from \"./define\";\nimport { UnsupportedLocaleError } from \"./errors\";\nimport { getLocaleHostname, getLocalePathSegment, isSameLocale } from \"./locale\";\nimport type {\n ShopifyDomainLocale,\n ShopifyI18n,\n ShopifyLocale,\n ShopifyMatchedLocale,\n ShopifyPathnameLocale,\n} from \"./types\";\n\nconst RELATIVE_URL_BASE = \"https://shopify.local\";\n\n/** Anything the locale can be matched from: a `Request`, a `URL`, or a URL or pathname string. */\nexport type LocaleMatchSource = Pick<Request, \"url\"> | URL | string;\n\n/**\n * Resolves the locale of a request URL according to the definition's routing.\n *\n * Pure and synchronous: the URL is the only input, so the same URL always resolves to the same\n * locale. Unknown prefixes or hostnames, and a missing URL, resolve to `defaultLocale`. Pathnames\n * such as `/fr-ca/products/x` are matched under pathname routing; domain routing needs an\n * absolute URL to read the hostname from.\n *\n * @throws when the source is a string that is neither a URL nor a pathname.\n *\n * Only the first path segment (pathname routing) or the hostname (domain routing) is read. The\n * URL is matched as-is: framework-specific rewrites of that segment must be normalized by the\n * caller. React Router single-fetch requests the locale root as `/fr-ca.data`, for example, so\n * strip the suffix before matching or pass the resolved locale to `createShopifyRequestContext`.\n */\nexport function matchLocale<const TI18n extends ShopifyI18n>(\n source: LocaleMatchSource | undefined,\n i18n: TI18n,\n): ShopifyMatchedLocale<TI18n>;\nexport function matchLocale(\n source: LocaleMatchSource | undefined,\n i18n: ShopifyI18n,\n): ShopifyMatchedLocale {\n const routing = i18n.routing;\n if (!routing) return toDefaultMatchedLocale(i18n);\n\n const url = toUrl(source);\n if (!url) return toDefaultMatchedLocale(i18n);\n\n switch (routing.type) {\n case \"pathname\": {\n const segment = getFirstPathSegment(url.pathname);\n const locale =\n segment === \"\"\n ? undefined\n : routing.locales.find((candidate) => getLocalePathSegment(candidate) === segment);\n return locale ? toPathnameMatchedLocale(locale) : toDefaultMatchedLocale(i18n);\n }\n case \"domain\": {\n const hostname = url.hostname.toLowerCase();\n const locale = routing.locales.find((candidate) => getLocaleHostname(candidate) === hostname);\n return locale ? toDomainMatchedLocale(locale) : toDefaultMatchedLocale(i18n);\n }\n default:\n routing satisfies never;\n return toDefaultMatchedLocale(i18n);\n }\n}\n\n/**\n * Resolves an explicitly chosen locale against the definition, for callers that already know the\n * locale and must not derive it from a URL: static rendering, locale switchers, or a `[locale]`\n * route param.\n *\n * Accepts either a `{language, country}` pair or the locale's path segment as returned by\n * `getLocalePathSegment` (case-insensitive), so a route param can be resolved directly.\n *\n * @throws {UnsupportedLocaleError} when the locale or segment is not one of the definition's\n * supported locales, so a stale or mistyped choice fails fast instead of silently producing an\n * unroutable URL or request context. Catch it to turn an unknown route param into a 404.\n */\nexport function resolveSupportedLocale<const TI18n extends ShopifyI18n>(\n locale: ShopifyLocale | string,\n i18n: TI18n,\n): ShopifyMatchedLocale<TI18n>;\nexport function resolveSupportedLocale(\n locale: ShopifyLocale | string,\n i18n: ShopifyI18n,\n): ShopifyMatchedLocale {\n const matches =\n typeof locale === \"string\"\n ? (candidate: ShopifyLocale) => getLocalePathSegment(candidate) === locale.toLowerCase()\n : (candidate: ShopifyLocale) => isSameLocale(candidate, locale);\n\n if (matches(i18n.defaultLocale)) return toDefaultMatchedLocale(i18n);\n\n const routing = i18n.routing;\n switch (routing?.type) {\n case \"pathname\": {\n const match = routing.locales.find(matches);\n if (match) return toPathnameMatchedLocale(match);\n break;\n }\n case \"domain\": {\n const match = routing.locales.find(matches);\n if (match) return toDomainMatchedLocale(match);\n break;\n }\n case undefined:\n break;\n default:\n routing satisfies never;\n }\n\n throw new UnsupportedLocaleError(locale, getSupportedLocales(i18n));\n}\n\nfunction toDefaultMatchedLocale(i18n: ShopifyI18n): ShopifyMatchedLocale {\n // Under domain routing the default locale's entry carries its hostname; prefer it.\n const entry =\n i18n.routing?.type === \"domain\"\n ? (i18n.routing.locales.find((candidate) => isSameLocale(candidate, i18n.defaultLocale)) ??\n i18n.defaultLocale)\n : i18n.defaultLocale;\n\n return { ...entry, pathPrefix: \"\" };\n}\n\nfunction toPathnameMatchedLocale(locale: ShopifyPathnameLocale): ShopifyMatchedLocale {\n const { pathSegment: _pathSegment, ...rest } = locale;\n return { ...rest, pathPrefix: `/${getLocalePathSegment(locale)}` };\n}\n\nfunction toDomainMatchedLocale(locale: ShopifyDomainLocale): ShopifyMatchedLocale {\n return { ...locale, pathPrefix: \"\" };\n}\n\nfunction getFirstPathSegment(pathname: string): string {\n const [segment = \"\"] = pathname.replace(/^\\/+/, \"\").split(\"/\", 1);\n return segment.toLowerCase();\n}\n\nfunction toUrl(source: LocaleMatchSource | undefined): URL | null {\n if (source === undefined) return null;\n if (source instanceof URL) return source;\n\n const url = typeof source === \"string\" ? source : source.url;\n try {\n return new URL(url, RELATIVE_URL_BASE);\n } catch {\n throw new Error(`matchLocale: ${JSON.stringify(url)} is not a URL or pathname.`);\n }\n}\n"],"mappings":";;;;;AAWA,MAAM,oBAAoB;AAwB1B,SAAgB,YACd,QACA,MACsB;CACtB,MAAM,UAAU,KAAK;AACrB,KAAI,CAAC,QAAS,QAAO,uBAAuB,KAAK;CAEjD,MAAM,MAAM,MAAM,OAAO;AACzB,KAAI,CAAC,IAAK,QAAO,uBAAuB,KAAK;AAE7C,SAAQ,QAAQ,MAAhB;EACE,KAAK,YAAY;GACf,MAAM,UAAU,oBAAoB,IAAI,SAAS;GACjD,MAAM,SACJ,YAAY,KACR,SACA,QAAQ,QAAQ,MAAM,cAAc,qBAAqB,UAAU,KAAK,QAAQ;AACtF,UAAO,SAAS,wBAAwB,OAAO,GAAG,uBAAuB,KAAK;;EAEhF,KAAK,UAAU;GACb,MAAM,WAAW,IAAI,SAAS,aAAa;GAC3C,MAAM,SAAS,QAAQ,QAAQ,MAAM,cAAc,kBAAkB,UAAU,KAAK,SAAS;AAC7F,UAAO,SAAS,sBAAsB,OAAO,GAAG,uBAAuB,KAAK;;EAE9E,QAEE,QAAO,uBAAuB,KAAK;;;AAoBzC,SAAgB,uBACd,QACA,MACsB;CACtB,MAAM,UACJ,OAAO,WAAW,YACb,cAA6B,qBAAqB,UAAU,KAAK,OAAO,aAAa,IACrF,cAA6B,aAAa,WAAW,OAAO;AAEnE,KAAI,QAAQ,KAAK,cAAc,CAAE,QAAO,uBAAuB,KAAK;CAEpE,MAAM,UAAU,KAAK;AACrB,SAAQ,SAAS,MAAjB;EACE,KAAK,YAAY;GACf,MAAM,QAAQ,QAAQ,QAAQ,KAAK,QAAQ;AAC3C,OAAI,MAAO,QAAO,wBAAwB,MAAM;AAChD;;EAEF,KAAK,UAAU;GACb,MAAM,QAAQ,QAAQ,QAAQ,KAAK,QAAQ;AAC3C,OAAI,MAAO,QAAO,sBAAsB,MAAM;AAC9C;;EAEF,KAAK,OACH;EACF;;AAIF,OAAM,IAAI,uBAAuB,QAAQ,oBAAoB,KAAK,CAAC;;AAGrE,SAAS,uBAAuB,MAAyC;AAQvE,QAAO;EAAE,GALP,KAAK,SAAS,SAAS,WAClB,KAAK,QAAQ,QAAQ,MAAM,cAAc,aAAa,WAAW,KAAK,cAAc,CAAC,IACtF,KAAK,gBACL,KAAK;EAEQ,YAAY;EAAI;;AAGrC,SAAS,wBAAwB,QAAqD;CACpF,MAAM,EAAE,aAAa,cAAc,GAAG,SAAS;AAC/C,QAAO;EAAE,GAAG;EAAM,YAAY,IAAI,qBAAqB,OAAO;EAAI;;AAGpE,SAAS,sBAAsB,QAAmD;AAChF,QAAO;EAAE,GAAG;EAAQ,YAAY;EAAI;;AAGtC,SAAS,oBAAoB,UAA0B;CACrD,MAAM,CAAC,UAAU,MAAM,SAAS,QAAQ,QAAQ,GAAG,CAAC,MAAM,KAAK,EAAE;AACjE,QAAO,QAAQ,aAAa;;AAG9B,SAAS,MAAM,QAAmD;AAChE,KAAI,WAAW,OAAW,QAAO;AACjC,KAAI,kBAAkB,IAAK,QAAO;CAElC,MAAM,MAAM,OAAO,WAAW,WAAW,SAAS,OAAO;AACzD,KAAI;AACF,SAAO,IAAI,IAAI,KAAK,kBAAkB;SAChC;AACN,QAAM,IAAI,MAAM,gBAAgB,KAAK,UAAU,IAAI,CAAC,4BAA4B"}
|