Shopify Hydrogen
repository·main·Indexed 24 days ago
https://github.com/shopify/hydrogenShopify's headless commerce stack providing tools and utilities to build performant commerce applications. It is primarily designed for React Router but includes a portable React library. The documentation includes the Hydrogen Cookbook, a collection of reproducible recipes for the Hydrogen skeleton template, featuring a CLI for managing, validating, and applying recipes, as well as LLM prompts for AI-assisted coding.
What's inside Hydrogen
- Hydrogen is Shopify’s stack for headless commerce. It is designed to work seamlessly with Remix, but it also provides a React library that is portable to other supporting frameworks. It provides tools, utilities, and examples for building dynamic and performant commerce applications.
Overview of Hydrogen packages
mainHydrogen is a monorepo containing several specialized packages for building headless commerce applications:
@shopify/hydrogen: Opinionated tools and utilities designed for building commerce applications specifically with React Router.@shopify/hydrogen-react: An unopinionated library of Shopify-specific commerce components, hooks, and utilities that can be used with other frameworks.@shopify/cli-hydrogen: An extension for the Shopify CLI.@shopify/create-hydrogen: The CLI tool used to generate new Hydrogen projects.@shopify/hydrogen-codegen: A tool to automatically generate types for Storefront API and Customer Account API queries.@shopify/mini-oxygen: A local runtime that simulates the Oxygen production environment for local development.
Use @shopify/cli-hydrogen
main@shopify/cli-hydrogen is the Hydrogen extension for the Shopify CLI. It provides tools, utilities, and examples for building commerce applications using Remix. You can use it to initialize new Hydrogen projects or manage existing ones via the Shopify CLI.Build a CMS using Shopify metaobjects in Hydrogen
mainThis recipe demonstrates how to create a custom Content Management System (CMS) using Shopify metaobjects. It allows you to manage dynamic content sections (like Hero banners, Featured Products, or Store profiles) directly through the Shopify admin.
Key capabilities include:
- Dynamic Route Rendering: Content is rendered based on metaobject data.
- Modular Components: Uses reusable React components that map to specific metaobject types.
- Direct Editing: Includes an
EditRoutecomponent to provide direct links to the Shopify admin for content management in development mode. - Rich Text Support: Integrates with the Slate editor for rich text fields.
Note: You must create the corresponding metaobject definitions in your Shopify admin before implementing this recipe. Each section component in the recipe has a one-to-one relationship with a specific metaobject definition.
What is Hydrogen Codegen
mainHydrogen Codegen is a codegen plugin and preset designed to generate TypeScript types from GraphQL queries into a
.d.tsfile. It wraps@shopify/graphql-codegenand provides specialized utilities for Hydrogen.Key characteristics:
- Zero Runtime Overhead: It adds 0 bytes to your bundle as it only generates type definitions.
- No Wrapper Required: It does not require specific function wrappers to work.
- Type Safety: It enables type safety for GraphQL queries by allowing the GraphQL client to use TypeScript interfaces that are extended in the generated
.d.tsfile.
What is the Hydrogen Cookbook and how do recipes work?
mainThe Hydrogen Cookbook is a collection of reproducible recipes designed to be applied to the Hydrogen skeleton template.
A recipe is a structured set of instructions that automates specific scenarios or use cases. Each recipe is organized into a folder containing:
recipe.yaml: A machine-readable definition of the entire recipe.ingredients/: A directory of new files to be introduced to the skeleton template (copied as-is).patches/: A directory of patches to be applied to existing files in the skeleton template. The mapping between files and patches is defined inrecipe.yamlunder theingredientskey.README.md: A human-readable Markdown version of the recipe generated fromrecipe.yaml.
Add language dynamic segments to routes
mainTo implement path-based localization, you must add an optional language dynamic segment to your route files. This allows the router to match paths both with and without a locale prefix.
Example Transformation:
- Original:
app/routes/_index.tsx - Localized:
app/routes/($locale)._index.tsx
This pattern should be applied to all application routes to ensure consistent localization across the site.
- Original:
Analyze bundle sizes with worker-bundle-analyzer
mainHydrogen includes tools to analyze bundle sizes to help keep worker bundles small, which improves cold startup times.
- Build Output: The build process generates
client-bundle-analyzer.htmlandworker-bundle-analyzer.htmlfiles in the build output directory. - Constraints: Hydrogen will fail the build if your worker bundle exceeds 10 MB, as Oxygen only supports bundles under this limit.
- Usage: Open the generated
.htmlfiles in a browser to view an interactive analysis. The CLI output typically provides direct links to these files.
- Build Output: The build process generates
Design a CMS architecture using Metaobjects
mainYou can build a custom Content Management System (CMS) in Hydrogen by structuring metaobjects into a hierarchy of Routes and Sections:
- Route Metaobjects: Act as containers. A single Route metaobject holds one or many references to
Sectionmetaobjects. - Section Metaobjects: Represent specific UI components (e.g.,
SectionHero,SectionFeaturedProducts). Each Section metaobject contains the specific fields (text, images, references) required to render that component.
This allows merchants to reorder sections within a route or create entirely new pages by simply composing different metaobject entries in the Shopify Admin.
- Route Metaobjects: Act as containers. A single Route metaobject holds one or many references to
Calculate subscription price adjustments
mainWhen a subscription plan is selected, the price may differ from the standard variant price. The Storefront API provides
priceAdjustmentswhich you must process manually in your UI.Supported adjustment types:
SellingPlanFixedAmountPriceAdjustment: Adds a specific amount to the base price.SellingPlanFixedPriceAdjustment: Sets the price to a specific fixed amount.SellingPlanPercentagePriceAdjustment: Applies a percentage discount to the base price.
Always use the
amountandcurrencyCodefrom the resulting calculation to render the final price using the<Money />component.Configure development and production bundles
mainHydrogen React provides separate development and production bundles. The development bundle includes extra warnings and messages to assist during development.
Most modern bundlers or runtimes will automatically select the correct bundle based on the
package.json#exportsfield. If your environment does not automatically select the correct bundle, you must configure your bundler or runtime to respect thedevelopmentandproductionconditions.Note: If your bundler/runtime does not understand export conditions, it will default to the production bundle.
Use LLM prompts from the Hydrogen Cookbook
mainRecipes in the cookbook are paired with LLM prompts. These prompts can be included in a Hydrogen project to enhance the AI-assisted coding experience by providing context-specific instructions related to the recipe being used. Prompts are located in the/cookbook/llmsdirectory.