Skip to content

Latest commit

 

History

History
127 lines (95 loc) · 4.88 KB

File metadata and controls

127 lines (95 loc) · 4.88 KB

Next.js Headless Shopify Starter

A production-ready starter template for building headless Shopify storefronts with Next.js. Includes a complete Shopify Storefront API integration with GraphQL queries, mutations, cart management, product/page fetching, and webhook-based revalidation — all fully typed with TypeScript.

Tech Stack

What's Included

├── lib/shopify/
│   ├── client.ts              # Storefront API client (server-only)
│   ├── products.ts            # getAllProducts(), getProductByHandle()
│   ├── pages.ts               # getPages(), getPage()
│   ├── cart.ts                # getCart(), createCart(), addToCart(), updateCartItems(), removeFromCart()
│   ├── fragments/             # GraphQL fragments (image, seo, product, page, cart)
│   ├── queries/               # GraphQL queries (product, page, cart)
│   └── mutations/             # GraphQL mutations (cart CRUD)
├── components/
│   ├── cart/
│   │   ├── cart-provider.tsx  # CartProvider context + useCart() hook
│   │   └── actions.ts         # Server actions with cookie-based cart persistence
│   └── product/
│       └── product-provider.tsx # ProductProvider context + useProduct() hook
├── types/
│   └── shopify.ts             # TypeScript types (Product, Cart, Page, Variant, etc.)
├── app/api/revalidate/
│   └── route.ts               # Shopify webhook endpoint for ISR revalidation
├── lib/
│   ├── utils.ts               # cn(), transformEdgeNode(), handleGraphQLResponse(), formatPrice()
│   └── constants.ts           # Shared constants
├── env.ts                     # Zod env validation + global type augmentation
├── .env.example               # Environment variable template
└── .graphqlrc.ts              # Shopify GraphQL codegen config

Getting Started

1. Install dependencies

bun install

2. Configure environment variables

cp .env.example .env.local

Fill in your Shopify credentials:

Variable Description
SHOPIFY_STORE_DOMAIN Your .myshopify.com domain
SHOPIFY_STOREFRONT_ACCESS_TOKEN Public Storefront API access token
SHOPIFY_REVALIDATION_SECRET Secret for webhook revalidation

You can generate a Storefront API token in your Shopify Admin under Settings > Apps and sales channels > Develop apps > Storefront API.

3. Run the development server

bun run dev

Open http://localhost:3000 to see the result.

4. Generate GraphQL types (optional)

bun run graphql-codegen

Shopify Webhook Setup

To enable on-demand revalidation when products or collections change in Shopify:

  1. Go to Shopify Admin > Settings > Notifications > Webhooks
  2. Add webhooks for products/create, products/update, products/delete
  3. Set the URL to: https://your-domain.com/api/revalidate?secret=YOUR_REVALIDATION_SECRET

Deploy

Deploy to Vercel with one click:

Deploy with Vercel

Or build for production:

bun run build
bun run start

License

MIT License

Copyright (c) 2025

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.