nextjs-starter-medusa
A performant frontend ecommerce starter template with Next.js 14 and Medusa.
Top Related Projects
Next.js Commerce
Saleor Storefront built with React 18, Next.js 14, App Router, TypeScript, GraphQL, and Tailwind CSS.
Hydrogen lets you build faster headless storefronts in less time, on Shopify.
Quick Overview
The medusajs/nextjs-starter-medusa repository is a starter template for building e-commerce applications using Medusa, an open-source headless commerce engine, and Next.js, a popular React framework. This project provides a foundation for creating custom, high-performance online stores with a modern tech stack.
Pros
- Seamless integration of Medusa's powerful e-commerce features with Next.js's performance and developer experience
- Pre-configured setup for rapid development of custom online stores
- Built-in responsive design and customizable components for easy UI modifications
- Supports both server-side rendering (SSR) and static site generation (SSG) for optimal performance
Cons
- Requires familiarity with both Medusa and Next.js ecosystems
- Limited out-of-the-box design customization options compared to traditional e-commerce platforms
- May have a steeper learning curve for developers new to headless commerce architectures
- Potential challenges in integrating with certain third-party services not natively supported by Medusa
Code Examples
- Fetching products using Medusa's API:
import { medusaClient } from "@lib/config"
const { products } = await medusaClient.products.list()
- Creating a custom checkout flow:
import { useCart } from "medusa-react"
const { cart, createPaymentSessions } = useCart()
const handleCheckout = async () => {
await createPaymentSessions()
// Redirect to payment page
}
- Implementing a product search feature:
import { useState } from "react"
import { medusaClient } from "@lib/config"
const ProductSearch = () => {
const [searchResults, setSearchResults] = useState([])
const handleSearch = async (query) => {
const { products } = await medusaClient.products.list({ q: query })
setSearchResults(products)
}
// Render search results
}
Getting Started
To get started with the medusajs/nextjs-starter-medusa project:
-
Clone the repository:
git clone https://github.com/medusajs/nextjs-starter-medusa.git
-
Install dependencies:
cd nextjs-starter-medusa npm install
-
Set up environment variables:
cp .env.template .env.local
Edit
.env.local
with your Medusa backend URL. -
Run the development server:
npm run dev
-
Open
http://localhost:3000
in your browser to see the starter template in action.
Competitor Comparisons
Next.js Commerce
Pros of Commerce
- More comprehensive e-commerce features, including cart, checkout, and product management
- Better integration with Vercel's ecosystem and deployment platform
- Larger community and more frequent updates
Cons of Commerce
- Steeper learning curve due to more complex architecture
- Less flexibility for customization compared to Medusa's modular approach
- Potentially overkill for simpler e-commerce projects
Code Comparison
Commerce:
export const getStaticProps: GetStaticProps = async () => {
const config = getConfig()
const products = await getAllProducts(config)
return {
props: { products },
revalidate: 60,
}
}
Nextjs-starter-medusa:
export const getStaticProps = async () => {
const products = await medusaClient.products.list()
return {
props: { products },
}
}
The Commerce example showcases a more complex setup with configuration and revalidation, while Nextjs-starter-medusa offers a simpler approach using the Medusa client directly.
Both repositories provide solid foundations for building e-commerce applications with Next.js. Commerce offers a more feature-rich solution with tighter Vercel integration, while Nextjs-starter-medusa provides a simpler starting point with greater flexibility for customization. The choice between them depends on project requirements and developer preferences.
Saleor Storefront built with React 18, Next.js 14, App Router, TypeScript, GraphQL, and Tailwind CSS.
Pros of Storefront
- More comprehensive e-commerce features out-of-the-box
- Better internationalization support
- Stronger focus on PWA capabilities
Cons of Storefront
- Steeper learning curve due to complex architecture
- Less flexibility for customization compared to Nextjs-starter-medusa
- Potentially slower performance for smaller-scale projects
Code Comparison
Storefront (React with Apollo Client):
const ProductList = () => {
const { loading, error, data } = useQuery(GET_PRODUCTS);
if (loading) return <p>Loading...</p>;
if (error) return <p>Error :(</p>;
return data.products.map(({ id, name, price }) => (
<div key={id}>{name} - {price}</div>
));
};
Nextjs-starter-medusa (Next.js with REST API):
export async function getServerSideProps() {
const products = await medusaClient.products.list();
return { props: { products } };
}
const ProductList = ({ products }) => {
return products.map(({ id, title, variant }) => (
<div key={id}>{title} - {variant.prices[0].amount}</div>
));
};
This comparison highlights the different approaches to data fetching and rendering between the two starters. Storefront uses GraphQL with Apollo Client, while Nextjs-starter-medusa utilizes Next.js server-side rendering with a REST API.
Hydrogen lets you build faster headless storefronts in less time, on Shopify.
Pros of Hydrogen
- Seamless integration with Shopify's ecosystem and features
- Built-in performance optimizations for e-commerce
- Extensive documentation and community support
Cons of Hydrogen
- Limited flexibility outside of Shopify's ecosystem
- Potential vendor lock-in
- Steeper learning curve for developers new to Shopify
Code Comparison
Hydrogen (React Server Components):
export default function Product({ params }) {
const { handle } = params;
const { product } = useShopQuery({
query: PRODUCT_QUERY,
variables: { handle },
});
return <ProductDetails product={product} />;
}
Nextjs-starter-medusa (Next.js):
export async function getStaticProps({ params }) {
const { product } = await medusaClient.products.retrieve(params.id);
return { props: { product } };
}
export default function Product({ product }) {
return <ProductDetails product={product} />;
}
Both repositories offer starter templates for e-commerce applications, but they cater to different ecosystems. Hydrogen is tightly integrated with Shopify, providing a streamlined development experience for Shopify stores. It leverages React Server Components for improved performance.
Nextjs-starter-medusa, on the other hand, offers more flexibility and is not tied to a specific e-commerce platform. It uses Next.js, which provides a familiar development experience for React developers. The code comparison shows how both handle product retrieval, with Hydrogen using a custom hook and Nextjs-starter-medusa utilizing Next.js's data fetching methods.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Medusa Next.js Starter Template
Combine Medusa's modules for your commerce backend with the newest Next.js 15 features for a performant storefront.
Prerequisites
To use the Next.js Starter Template, you should have a Medusa server running locally on port 9000. For a quick setup, run:
npx create-medusa-app@latest
Check out create-medusa-app docs for more details and troubleshooting.
Overview
The Medusa Next.js Starter is built with:
Features include:
- Full ecommerce support:
- Product Detail Page
- Product Overview Page
- Product Collections
- Cart
- Checkout with Stripe
- User Accounts
- Order Details
- Full Next.js 15 support:
- App Router
- Next fetching/caching
- Server Components
- Server Actions
- Streaming
- Static Pre-Rendering
Quickstart
Setting up the environment variables
Navigate into your projects directory and get your environment variables ready:
cd nextjs-starter-medusa/
mv .env.template .env.local
Install dependencies
Use Yarn to install all dependencies.
yarn
Start developing
You are now ready to start up your project.
yarn dev
Open the code and start customizing
Your site is now running at http://localhost:8000!
Payment integrations
By default this starter supports the following payment integrations
To enable the integrations you need to add the following to your .env.local
file:
NEXT_PUBLIC_STRIPE_KEY=<your-stripe-public-key>
You'll also need to setup the integrations in your Medusa server. See the Medusa documentation for more information on how to configure Stripe.
Resources
Learn more about Medusa
Learn more about Next.js
Top Related Projects
Next.js Commerce
Saleor Storefront built with React 18, Next.js 14, App Router, TypeScript, GraphQL, and Tailwind CSS.
Hydrogen lets you build faster headless storefronts in less time, on Shopify.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot