building-storefronts
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMedusa Storefront Development
Medusa 店面开发
Frontend integration guide for building storefronts with Medusa. Covers SDK usage, React Query patterns, and calling custom API routes.
使用Medusa构建店面的前端集成指南。涵盖SDK使用、React Query模式以及自定义API路由调用。
When to Apply
适用场景
Load this skill for ANY storefront development task, including:
- Calling custom Medusa API routes from the storefront
- Integrating Medusa SDK in frontend applications
- Using React Query for data fetching
- Implementing mutations with optimistic updates
- Error handling and cache invalidation
Also load building-with-medusa when: Building the backend API routes that the storefront calls
任何店面开发任务都需加载此技能,包括:
- 从店面调用自定义Medusa API路由
- 在前端应用中集成Medusa SDK
- 使用React Query进行数据获取
- 实现带乐观更新的突变操作
- 错误处理与缓存失效
同时,当构建店面调用的后端API路由时,请加载building-with-medusa技能
CRITICAL: Load Reference Files When Needed
重要提示:必要时加载参考文件
The quick reference below is NOT sufficient for implementation. You MUST load the reference file before writing storefront integration code.
Load this reference when implementing storefront features:
- Calling API routes? → MUST load first
references/frontend-integration.md - Using SDK? → MUST load first
references/frontend-integration.md - Implementing React Query? → MUST load first
references/frontend-integration.md
以下快速参考内容不足以支撑实现工作。在编写店面集成代码前,必须加载参考文件。
实现店面功能时,请加载以下参考文件:
- 调用API路由? → 必须先加载
references/frontend-integration.md - 使用SDK? → 必须先加载
references/frontend-integration.md - 实现React Query? → 必须先加载
references/frontend-integration.md
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | SDK Usage | CRITICAL | |
| 2 | React Query Patterns | HIGH | |
| 3 | Data Display | HIGH (includes CRITICAL price rule) | |
| 4 | Error Handling | MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | SDK使用 | 关键 | |
| 2 | React Query模式 | 高 | |
| 3 | 数据展示 | 高(包含关键价格规则) | |
| 4 | 错误处理 | 中 | |
Quick Reference
快速参考
1. SDK Usage (CRITICAL)
1. SDK使用(关键)
- - NEVER use JSON.stringify() on body - SDK handles serialization automatically
sdk-no-json-stringify - - Pass plain JavaScript objects to body, not strings
sdk-plain-objects - - Always locate where SDK is instantiated in the project before using it
sdk-locate-first - - Use
sdk-client-fetchfor custom API routessdk.client.fetch() - - SDK automatically adds Content-Type, auth headers, and API key
sdk-auto-headers
- - 绝对不要在请求体上使用JSON.stringify() - SDK会自动处理序列化
sdk-no-json-stringify - - 向请求体传入普通JavaScript对象,而非字符串
sdk-plain-objects - - 使用SDK前,务必先定位项目中SDK的实例化位置
sdk-locate-first - - 对自定义API路由使用
sdk-client-fetchsdk.client.fetch() - - SDK会自动添加Content-Type、认证头以及API密钥
sdk-auto-headers
2. React Query Patterns (HIGH)
2. React Query模式(高)
- - Use
query-use-queryfor GET requests (data fetching)useQuery - - Use
query-use-mutationfor POST/DELETE requests (mutations)useMutation - - Invalidate queries in
query-invalidateto refresh data after mutationsonSuccess - - Structure query keys hierarchically for effective cache management
query-keys-hierarchical - - Always handle
query-loading-states,isLoading,isPendingstatesisError
- - 对GET请求(数据获取)使用
query-use-queryuseQuery - - 对POST/DELETE请求(突变操作)使用
query-use-mutationuseMutation - - 在
query-invalidate中使查询失效,以便在突变后刷新数据onSuccess - - 按层级结构组织查询键,实现高效缓存管理
query-keys-hierarchical - - 务必处理
query-loading-states、isLoading、isPending状态isError
3. Data Display (HIGH)
3. 数据展示(高)
- - CRITICAL: Prices from Medusa are stored as-is ($49.99 = 49.99, NOT in cents). Display them directly - NEVER divide by 100
display-price-format
- - 关键:Medusa中的价格按原始值存储($49.99 = 49.99,而非以美分为单位)。直接展示即可——绝对不要除以100
display-price-format
4. Error Handling (MEDIUM)
4. 错误处理(中)
- - Implement
error-on-errorcallback in mutations to handle failuresonError - - Show error messages to users when mutations fail
error-display - - Use optimistic updates with rollback on error for better UX
error-rollback
- - 在突变操作中实现
error-on-error回调以处理失败情况onError - - 突变失败时向用户展示错误信息
error-display - - 结合乐观更新与错误回滚,提升用户体验
error-rollback
Critical SDK Pattern
关键SDK模式
ALWAYS pass plain objects to the SDK - NEVER use JSON.stringify():
typescript
// ✅ CORRECT - Plain object
await sdk.client.fetch("/store/reviews", {
method: "POST",
body: {
product_id: "prod_123",
rating: 5,
}
})
// ❌ WRONG - JSON.stringify breaks the request
await sdk.client.fetch("/store/reviews", {
method: "POST",
body: JSON.stringify({ // ❌ DON'T DO THIS!
product_id: "prod_123",
rating: 5,
})
})Why this matters:
- The SDK handles JSON serialization automatically
- Using JSON.stringify() will double-serialize and break the request
- The server won't be able to parse the body
务必向SDK传入普通对象——绝对不要使用JSON.stringify():
typescript
// ✅ 正确 - 普通对象
await sdk.client.fetch("/store/reviews", {
method: "POST",
body: {
product_id: "prod_123",
rating: 5,
}
})
// ❌ 错误 - JSON.stringify会破坏请求
await sdk.client.fetch("/store/reviews", {
method: "POST",
body: JSON.stringify({ // ❌ 不要这样做!
product_id: "prod_123",
rating: 5,
})
})为什么这很重要:
- SDK会自动处理JSON序列化
- 使用JSON.stringify()会导致双重序列化,破坏请求
- 服务器将无法解析请求体
Common Mistakes Checklist
常见错误检查清单
Before implementing, verify you're NOT doing these:
SDK Usage:
- Using JSON.stringify() on the body parameter
- Manually setting Content-Type headers (SDK adds them)
- Hardcoding SDK import paths (locate in project first)
- Not using sdk.client.fetch() for custom routes
React Query:
- Not invalidating queries after mutations
- Using flat query keys instead of hierarchical
- Not handling loading and error states
- Forgetting to disable buttons during mutations (isPending)
Data Display:
- CRITICAL: Dividing prices by 100 when displaying (prices are stored as-is: $49.99 = 49.99, NOT in cents)
Error Handling:
- Not implementing onError callbacks
- Not showing error messages to users
- Not handling network failures gracefully
实现前,请确认你没有以下操作:
SDK使用:
- 在body参数上使用JSON.stringify()
- 手动设置Content-Type请求头(SDK会自动添加)
- 硬编码SDK导入路径(先在项目中定位)
- 对自定义路由不使用sdk.client.fetch()
React Query:
- 突变后不使查询失效
- 使用扁平查询键而非层级结构
- 不处理加载和错误状态
- 突变期间忘记禁用按钮(isPending状态)
数据展示:
- 关键错误:展示价格时除以100(价格按原始值存储:$49.99 = 49.99,而非以美分为单位)
错误处理:
- 未实现onError回调
- 不向用户展示错误信息
- 未优雅处理网络故障
How to Use
使用方法
For detailed patterns and examples, load reference file:
references/frontend-integration.md - SDK usage, React Query patterns, API integrationThe reference file contains:
- Step-by-step SDK integration patterns
- Complete React Query examples
- Correct vs incorrect code examples
- Query key best practices
- Optimistic update patterns
- Error handling strategies
如需详细模式与示例,请加载参考文件:
references/frontend-integration.md - SDK使用、React Query模式、API集成参考文件包含:
- 分步SDK集成模式
- 完整的React Query示例
- 正确与错误的代码示例
- 查询键最佳实践
- 乐观更新模式
- 错误处理策略
When to Use MedusaDocs MCP Server
何时使用MedusaDocs MCP Server
Use this skill for (PRIMARY SOURCE):
- How to call custom API routes from storefront
- SDK usage patterns (sdk.client.fetch)
- React Query integration patterns
- Common mistakes and anti-patterns
Use MedusaDocs MCP server for (SECONDARY SOURCE):
- Built-in SDK methods (sdk.admin., sdk.store.)
- Official Medusa SDK API reference
- Framework-specific configuration options
Why skills come first:
- Skills contain critical patterns like "don't use JSON.stringify" that MCP doesn't emphasize
- Skills show correct vs incorrect patterns; MCP shows what's possible
- Planning requires understanding patterns, not just API reference
此技能适用于(主要来源):
- 如何从店面调用自定义API路由
- SDK使用模式(sdk.client.fetch)
- React Query集成模式
- 常见错误与反模式
MedusaDocs MCP Server适用于(次要来源):
- 内置SDK方法(sdk.admin., sdk.store.)
- 官方Medusa SDK API参考
- 框架特定配置选项
为什么优先使用技能:
- 技能包含像“不要使用JSON.stringify”这样的关键模式,而MCP并未强调这些
- 技能展示正确与错误的模式;MCP展示可用功能
- 规划工作需要理解模式,而非仅依赖API参考
Integration with Backend
与后端的集成
When building features that span backend and frontend:
- Backend (building-with-medusa skill): Module → Workflow → API Route
- Storefront (this skill): SDK → React Query → UI Components
- Connection: Storefront calls backend API routes via
sdk.client.fetch()
See for backend API route patterns.
building-with-medusa当构建跨后端与前端的功能时:
- 后端(building-with-medusa技能): 模块 → 工作流 → API路由
- 店面(本技能): SDK → React Query → UI组件
- 连接方式: 店面通过调用后端API路由
sdk.client.fetch()
有关后端API路由模式,请查看技能。
building-with-medusa