Fashion Starter for Medusa 2.0
repository·master·Indexed 42 days ago
https://github.com/agilo/fashion-starterA modern, customizable e-commerce template for high-end fashion or furniture brands built with Medusa 2.0. It includes a Next.js storefront, dynamic product options, and Meilisearch integration. The starter provides guidance on creating custom API routes, Admin widgets, scheduled jobs, custom modules, and CLI scripts, and is compatible with @medusajs/medusa versions 1.8.0 and above.
What's inside Fashion Starter
- A module is a package of reusable functionalities. It is designed to be integrated into a Medusa application without affecting the overall system architecture. The core of a module is a Service (a TypeScript or JavaScript class containing business logic) and a Module Definition (which registers the service with the Medusa framework).
How to use a non-Postgres database for testing
masterThe current E2E suite assumes a Postgres backend and relies on dropping/recreating databases. If you need to use a different database engine (such as SQLite or MySQL), you must implement a custom
resetDatabasefunction.Refer to
seed/reset.tsfor the existing implementation pattern and adapt it to your database type to ensure it can be called between test runs.Extend the Medusa Admin with widgets and pages
masterThe Medusa Admin can be extended by adding custom widgets and new pages. These customizations allow you to provide merchants with custom functionalities by interacting with existing API routes.Understand the App Router structure and routing
masterThe storefront uses the Next.js App Router. The routing is organized as follows:
[countryCode]: The root directory is a dynamic route based on the country code (e.g.,/us/products/...). This allows fetching region-specific prices and languages.- Route Groups: The app uses
(checkout)and(main)groups. These do not affect the URL but allow for different layouts (e.g., the checkout flow uses a different layout than the rest of the site). - Nested Routes: Routes are nested within these groups. For example,
/account/orders/details/123maps tosrc/app/[countryCode]/(main)/account/orders/details/[id].
Accessing
countryCode:- In Server Components (
page.tsx): Access via theparamsobject. - In Client Components: Use the
useParamshook fromnext/navigation.
// Server Component usage export default async function Page({ params: { countryCode }, }: { params: { countryCode: string } }) { const region = await getRegion(countryCode) } // Client Component usage import { useParams } from "next/navigation" const Component = () => { const { countryCode } = useParams() }Understand the Middleware and Country Code redirection
masterThe
middleware.tsfile acts as an Edge function that enforces acountryCodein the URL. If a user visits a URL without acountryCode, the middleware redirects them to the most relevant region based on this priority:- Vercel Deployment: Uses the
x-vercel-ip-countryheader. - Environment Variable: Uses the
NEXT_PUBLIC_DEFAULT_REGIONvariable if defined. - Fallback: Redirects to the first region found on the Medusa server.
Note: The middleware also sets a cookie for Medusa Admin onboarding; this can be removed for production storefronts.
- Vercel Deployment: Uses the
Understand Medusa commerce modules
masterMedusa provides a set of open-source commerce modules and tools designed to build commerce applications (stores, marketplaces, etc.) without reinventing core logic. All modules are available on npm.
To understand how to build with these primitives, consult the Medusa Architecture overview and the Commerce Modules documentation.
Create associations between modules using Module Links
masterModule Links allow you to form associations between data models belonging to different modules while preserving module isolation. You define these links using the
defineLinkutility from@medusajs/framework/utils.To create a link, you pass the linkable entities from the respective modules to
defineLink. For example, to associate a Product from the Medusa Product Module with a custom entity in your own module, you reference their.linkableproperties.import HelloModule from "../modules/hello" import ProductModule from "@medusajs/medusa/product" import { defineLink } from "@medusajs/framework/utils" export default defineLink( ProductModule.linkable.product, HelloModule.linkable.myCustom )Quickstart: Set up the Storefront
masterThe storefront is built with Next.js 15 and Tailwind CSS. After setting up the Medusa backend, follow these steps to run the storefront:
- Navigate to the
storefrontdirectory. - Create the
.env.localfile from the template. - Install dependencies using
yarn. - Start the development server.
Important: You must paste the
NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY(obtained from the Medusa admin athttp://localhost:9000/app/settings/publishable-api-keys) intostorefront/.env.localfor the storefront to communicate with the backend.The storefront will be available at
http://localhost:8000.cd storefront # Create the .env.local file cp .env.template .env.local # Install dependencies yarn # Start the development server yarn dev- Navigate to the
Quickstart: Install and run the Medusa Next.js Starter
masterTo get started with the storefront, ensure you have a Medusa server running locally on port 9000. Follow these steps to set up the local environment:
- Prepare environment variables: Copy the template environment file to
.env.local. - Install dependencies: Use
yarnto install all required packages. - Start development server: Run the development command to start the site at
http://localhost:8000.
Prerequisites:
- A running Medusa server (use
npx create-medusa-app@latestto create one quickly). - Yarn installed.
# 1. Setup env cd nextjs-starter-medusa/ mv .env.template .env.local # 2. Install yarn # 3. Run yarn dev- Prepare environment variables: Copy the template environment file to
Quickstart: Set up the Medusa backend
masterTo set up the Medusa 2.0 backend, follow these steps in order. This requires Docker and Docker Compose to be running for the database and Redis services.
- Navigate to the
medusadirectory. - Create your environment file from the template.
- Install dependencies using
yarn. - Start the database and Redis via Docker.
- Build the project.
- Run database migrations.
- Seed the database with initial data.
- Create an admin user to access the dashboard.
- Start the development server.
After starting, access the admin at
http://localhost:9000/app. You will need to retrieve a publishable key from the admin settings to connect the storefront.cd medusa # Create the .env file cp .env.template .env # Install dependencies yarn # Spin up the database and Redis docker-compose up -d # Build the project yarn build # Run the migrations yarn medusa db:migrate # Seed the database yarn seed # Create an user yarn medusa user -e "admin@medusa.local" -p "supersecret" # Start the development server yarn dev- Navigate to the
How to create a custom CLI script
masterCustom CLI scripts allow you to execute custom Medusa tooling directly through the Medusa CLI.
To create a script:
- Create a TypeScript (
.ts) or JavaScript (.js) file inside thesrc/scriptsdirectory. - The file must provide a
default exportof anasyncfunction. - The function receives an
ExecArgsobject as its parameter, which contains acontainerproperty. Thiscontaineris an instance of the Medusa Container used to resolve application resources (e.g., module services).
import { ExecArgs, IProductModuleService } from "@medusajs/framework/types" import { Modules } from "@medusajs/framework/utils" export default async function myScript ({ container }: ExecArgs) { const productModuleService: IProductModuleService = container.resolve(Modules.PRODUCT) const [, count] = await productModuleService.listAndCount() console.log(`You have ${count} product(s)`) }- Create a TypeScript (
Configure MeiliSearch or switch to Algolia search
masterUsing MeiliSearch (Default)
To enable MeiliSearch, you must install the
medusa-search-meilisearchplugin on your Medusa server and enable the feature flag in./store.config.json:{ "features": { "search": true } }Switching to Algolia
If you prefer Algolia, follow these steps:
- Install the Algolia package:
yarn add algoliasearch. - Update
@lib/search-clientto use thealgoliasearch/liteclient with yourappIdandapiKey(provided via environment variables). - Update
src/app/(main)/search/actions.tsto use the newsearchClientinstead of the MeiliSearch implementation.
- Install the Algolia package: