Skip to main content

Configure route templates

Shopify storefronts have standard URLs, such as /products/{handle} and /collections/{handle}. Apps, Shopify browser modules, predictive search results, and old links all assume these URLs. If your storefront renders a resource at a different path, such as /p/{handle}, then Hydrogen needs to know so that it can build and redirect to your URLs instead.

Route templates are that shared configuration. You describe your app's URL shape once, in one manifest, and pass the same manifest to every Hydrogen primitive that builds or redirects storefront URLs. Route templates aren't page handlers. Your framework's router still renders every page.



Anchor to Step 1: Create the manifestStep 1: Create the manifest

Create one manifest with createShopifyRouteTemplates(). Add a key only when your app renders a Shopify route at a path that's different from Shopify's standard path, or when you want Hydrogen to redirect a standard URL to one of your pages.

If your app uses Shopify's standard paths, then create an empty manifest. It still needs to exist, so there's one place to update if your routes change later:

Standard paths

lib/route-templates.ts
import {createShopifyRouteTemplates} from '@shopify/hydrogen';

// Add entries here if the app changes to custom Shopify resource paths.
export const routeTemplates = createShopifyRouteTemplates({});

If your app uses custom paths, then add a template for each one:

Custom paths

lib/route-templates.ts
import {createShopifyRouteTemplates} from '@shopify/hydrogen';

export const routeTemplates = createShopifyRouteTemplates({
product: '/p/:productHandle',
collection: '/c/:collectionHandle',
article: '/journal/:blogHandle/:articleHandle',
cart: '/basket',
policy: '/legal/:policyHandle',
search: '/find',
});

The React Router template renders products at /products/{handle} and doesn't render collection-scoped product URLs. It adds only productInCollection, so collection-scoped product URLs redirect to the product page:

React Router template manifest

app/lib/route-templates.ts
import {createShopifyRouteTemplates} from '@shopify/hydrogen';

export const routeTemplates = createShopifyRouteTemplates({
productInCollection: '/products/:productHandle',
});

Vercel Shop, Vercel's Next.js template, uses Shopify's standard paths, so it calls createShopifyRouteTemplates() without any keys.

Each key is a Shopify standard route, and each value is your app's path template for that route. Templates start with / and include the placeholders that the key requires.

KeyShopify standard pathAdd this key when
product/products/:productHandleYour app renders product pages at a different path.
productInCollection/collections/:collectionHandle/products/:productHandleYou want collection-scoped product URLs to redirect to a product page that your app renders.
collection/collections/:collectionHandleYour app renders collection pages at a different path.
collectionList/collections and the legacy /productsYour app renders the collection list at a different path.
page/pages/:pageHandleYour app renders Online Store pages at a different path.
policy/policies/:policyHandleYour app renders store policies at a different path.
blog/blogs/:blogHandleYour app renders blog pages at a different path.
article/blogs/:blogHandle/:articleHandleYour app renders blog articles at a different path.
cart/cartYour app renders the cart page at a different path.
search/searchYour app renders search results at a different path.

The supported placeholders are :productHandle, :collectionHandle, :pageHandle, :policyHandle, :blogHandle, and :articleHandle. Hydrogen can't fill in any other placeholder.

Follow these rules when you write the manifest:

  • Leave a key out when your app doesn't render that route. For example, if your storefront has no blog, then leave out blog and article.
  • Don't add keys for standard paths, even to be explicit.
  • Use strings, not functions. The manifest must be serializable.
  • Don't include query strings or hashes. Hydrogen adds and keeps query parameters separately.
  • Don't include market prefixes such as /en-ca. Hydrogen adds the prefix from the request's i18n.pathPrefix. Refer to Markets.

Anchor to Step 2: Pass the manifest to every consumerStep 2: Pass the manifest to every consumer

Import the same routeTemplates object everywhere Hydrogen builds, redirects, or exposes storefront URLs. Don't create a separate manifest for each consumer. You pass it to the following APIs when you wire them:

APIWhat it uses the manifest forGuide
handleShopifyRoutes()Redirects Liquid-style ?variant={id} product URLs to your app's product path.Request handlers
handleShopifyRedirects()Redirects Shopify's standard paths to your app's paths after your router returns a 404. Hydrogen doesn't check that the resource exists, so your page route handles missing resources.Request handlers
The routes prop of ShopifyScripts, or the routes option of initializeShopifyScripts()Lets Shopify browser modules and agent tools build URLs that match your app.Analytics and consent
getPredictiveSearchItemUrl()Links predictive search results to your app's paths. Query suggestions use the search template.Add predictive search

To check that Shopify's standard URLs reach your pages, wire the request handlers and then follow Verify your wiring.



Was this page helpful?