Markets in Hydrogen
This recipe shows how to add support for Shopify Markets to your Hydrogen app. Markets let you segment your audience based on location and serve different content to each market.
You can use Markets in a variety of ways. In this recipe, you'll set up basic localization support for your Hydrogen store, learn what options are available for routing, add a country selector component to your app, and set up links that work across localized versions of your store.
There are several ways to implement localization in your Shopify Hydrogen store, and the approach you take will depend on your project's requirements. This recipe uses URL-based localization, which makes market information visible in the URL. This provides two key benefits:
-
It's transparent to search engine crawlers.
-
It allows each localized version of your store to be properly indexed.
This approach is typically implemented in two ways:
- Path-based localization (recommended)
- Example:
example.com/fr-ca/products
- Implementation: Requires adding a locale parameter to your routes
- Rename
routes/_index.tsx
toroutes/($locale)._index.tsx
- Rename
- Advantages: No infrastructure changes needed
- Considerations: Requires additional code to handle link formatting throughout your application
- Example:
- Subdomain or top-level domain localization
- Example:
fr-ca.example.com/products
(orexample.fr/products
) - Implementation: Requires infrastructure configuration
- Advantages: Maintains consistent URL structure across localized stores
- Considerations: More complex setup at the infrastructure level
- Example:
Although you can use other methods for localization (like cookies or HTTP headers), these approaches have one significant disadvantage: they're not visible to search engine crawlers. This can negatively impact your SEO for different markets.
In this recipe, we'll implement path-based localization.
This recipe is particularly useful for existing Hydrogen projects. If you need to set up a brand new Hydrogen app, you can get a solid foundation by selecting the localization options when setting up your new project using the Shopify CLI. You can also use h2 setup markets
to add localization support to your new Hydrogen app.
Anchor to RequirementsRequirements
- Set up your store's regions and languages using Shopify Markets.
- Configure your products appropriately for each market.
- Make sure your Hydrogen app is configured to use a default
language
andcountry code
. They will be used as the fallback when no market is explicitly selected.
Anchor to IngredientsIngredients
New files added to the template by this recipe.
File | Description |
---|---|
app/components/CountrySelector.tsx | A component that displays a country selector inside the Header. |
app/components/Link.tsx | A wrapper around the Link component that uses the selected locale path prefix. |
app/lib/i18n.ts | A helper function to get locale information from the context, a hook to retrieve the selected locale, and a list of available locales. |
app/routes/($locale)._index.tsx | A route that renders a localized version of the home page. |
app/routes/($locale).cart.tsx | A localized cart route. |
app/routes/($locale).products.$handle.tsx | A route that renders a localized version of the product page. |
app/routes/($locale).tsx | A utility route that makes sure the locale is valid. |
Anchor to Step 1: Add localization utilities and update core componentsStep 1: Add localization utilities and update core components
In this section, we'll create utilities to handle localization and country selection, and update the core components to use these utilities.
Create a new CountrySelector
component that allows users to select the locale from a dropdown of the supported locales.
To handle redirects, use a Form
that updates the cart buyer identity,
which eventually redirects to the localized root of the app.
File
Anchor to Step 1.2: Create a Link wrapper componentStep 1. 2: Create a Link wrapper component
Create a wrapper component around the Link
component that prepends the selected locale path prefix (if any) to the actual links.
File
Anchor to Step 1.3: Create i18n helpersStep 1. 3: Create i18n helpers
- Create a helper function to get locale information from the context, and a hook to retrieve the selected locale.
- Define a set of supported locales for the app.
- Add a utility function to validate the locale from the route param against the supported locales.
File
Anchor to Step 1.4: Use the new Link component in the ProductItem componentStep 1. 4: Use the new Link component in the Product Item component
Update the ProductItem
component to use the Link
component from the
app/components/Link.tsx
file.
Anchor to Step 1.5: Add the selected locale to the contextStep 1. 5: Add the selected locale to the context
Detect the locale from the URL path, and add it to the HydrogenContext.
Anchor to Step 1.6: Add the CountrySelector component to the HeaderStep 1. 6: Add the Country Selector component to the Header
This adds a country selector component to the navigation.
Anchor to Step 1.7: Add the selected locale to the root routeStep 1. 7: Add the selected locale to the root route
- Include the selected locale in the root route's loader data.
- Make sure to redirect to the 404 page if the requested locale is not supported.
- Add a key prop to the
PageLayout
component to make sure it re-renders when the locale changes.
Anchor to Step 2: Localizing the individual routesStep 2: Localizing the individual routes
In this section, we'll add localization to the individual routes using the language dynamic segment.
Anchor to Step 2.1: Add language dynamic segment to the desired routesStep 2. 1: Add language dynamic segment to the desired routes
To implement path-based localization, add a language
dynamic segment to your localized routes (for example, renaming routes/_index.tsx
to routes/($locale)._index.tsx
).
For brevity, we'll focus on the home page, the cart page, and the product page in this example. In your app, you should do this for all the app routes.
Anchor to Step 2.2: Add localization to the home pageStep 2. 2: Add localization to the home page
- Add the dynamic segment to the home page route.
- Use the new
Link
component as a drop-in replacement.
Rename app/routes/_index.tsx
to app/routes/($locale)._index.tsx
.
File
Anchor to Step 2.3: Add localization to the cart pageStep 2. 3: Add localization to the cart page
Add the dynamic segment to the cart page route.
Rename app/routes/cart.tsx
to app/routes/($locale).cart.tsx
.
File
Anchor to Step 2.4: Add localization to the product pageStep 2. 4: Add localization to the product page
- Add the dynamic segment to the product page route.
- Update the
meta
function to also update the canonical URL to use the localized prefix.
Rename app/routes/products.$handle.tsx
to app/routes/($locale).products.$handle.tsx
.
File
Anchor to Step 2.5: Add a utility route to validate the locale.Step 2. 5: Add a utility route to validate the locale.
Add a utility route in $(locale).tsx
that will use localeMatchesPrefix
to validate the locale from the URL params. If the locale is invalid,
the route will throw a 404 error.
File
Anchor to Step 2.6: Update the sitemap route's locales.Step 2. 6: Update the sitemap route's locales.
Update the sitemap route to use the locales included in SUPPORTED_LOCALES
.
Remove the pathname
parameter from the useVariantUrl
function, and the logic that prepends the locale to the path.
Anchor to Next stepsNext steps
- Test your implementation by going to your store and selecting a different market from the country selector.
- Refer to the Shopify Help Center for more information on how to optimize and manage your international markets.