@salesforce/b2c-dx-mcp 0.0.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +422 -29
- package/bin/run.cmd +3 -0
- package/bin/run.js +27 -0
- package/content/auth.md +62 -0
- package/content/components.md +123 -0
- package/content/config.md +180 -0
- package/content/data-fetching.md +323 -0
- package/content/extensions.md +80 -0
- package/content/i18n.md +121 -0
- package/content/page-designer.md +86 -0
- package/content/performance.md +80 -0
- package/content/pitfalls.md +141 -0
- package/content/quick-reference.md +226 -0
- package/content/state-management.md +75 -0
- package/content/styling.md +51 -0
- package/content/testing.md +232 -0
- package/dist/commands/mcp.d.ts +110 -0
- package/dist/commands/mcp.js +333 -0
- package/dist/registry.d.ts +37 -0
- package/dist/registry.js +212 -0
- package/dist/server.d.ts +46 -0
- package/dist/server.js +98 -0
- package/dist/services.d.ts +168 -0
- package/dist/services.js +191 -0
- package/dist/tools/adapter.d.ts +201 -0
- package/dist/tools/adapter.js +220 -0
- package/dist/tools/cartridges/index.d.ts +20 -0
- package/dist/tools/cartridges/index.js +101 -0
- package/dist/tools/index.d.ts +17 -0
- package/dist/tools/index.js +25 -0
- package/dist/tools/mrt/index.d.ts +20 -0
- package/dist/tools/mrt/index.js +101 -0
- package/dist/tools/pwav3/index.d.ts +13 -0
- package/dist/tools/pwav3/index.js +78 -0
- package/dist/tools/scapi/index.d.ts +9 -0
- package/dist/tools/scapi/index.js +68 -0
- package/dist/tools/storefrontnext/developer-guidelines.d.ts +9 -0
- package/dist/tools/storefrontnext/developer-guidelines.js +140 -0
- package/dist/tools/storefrontnext/index.d.ts +13 -0
- package/dist/tools/storefrontnext/index.js +83 -0
- package/dist/utils/constants.d.ts +16 -0
- package/dist/utils/constants.js +18 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.js +16 -0
- package/dist/utils/types.d.ts +45 -0
- package/dist/utils/types.js +7 -0
- package/oclif.manifest.json +377 -0
- package/package.json +123 -7
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Page Designer Integration
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Page Designer is Commerce Cloud's visual editor in Business Manager: merchants build and edit storefront pages (home, category, etc.) without code. The app gets page structure (regions, components, attributes) from the **Shopper Experience API** and renders it via a **component registry** and `<Region>`.
|
|
6
|
+
|
|
7
|
+
## Concepts
|
|
8
|
+
|
|
9
|
+
| Concept | Role |
|
|
10
|
+
|--------|------|
|
|
11
|
+
| **Page** | Fetched in route loaders via `fetchPageFromLoader(args, { pageId })`. |
|
|
12
|
+
| **Region** | Named area on a page; rendered with `<Region page={...} regionId="..." componentData={...} />`. |
|
|
13
|
+
| **Component** | Content block (hero, grid, carousel, etc.) with a `typeId` and attributes; registered in `@/lib/registry`. |
|
|
14
|
+
| **Registry** | Static registry in `@/lib/static-registry.ts` is **auto-generated** by the staticRegistry Vite plugin — do not edit by hand. |
|
|
15
|
+
|
|
16
|
+
## Which pages use Page Designer
|
|
17
|
+
|
|
18
|
+
Only **content pages** that merchants edit in Business Manager use Page Designer; cart, checkout, account, and auth do not.
|
|
19
|
+
|
|
20
|
+
| Uses Page Designer | Does not |
|
|
21
|
+
|--------------------|----------|
|
|
22
|
+
| Home (`pageId: 'homepage'`), Category/PLP (`plp`), Search (`search`), Product/PDP (`pdp`) | Cart, Checkout, Account, Order confirmation, Auth |
|
|
23
|
+
|
|
24
|
+
To add a new content page: define a page type and ID in Commerce Cloud, then in your route use `fetchPageFromLoader(args, { pageId })` and `collectComponentDataPromises(args, pagePromise)`, and render `<Region>` for each region.
|
|
25
|
+
|
|
26
|
+
## Getting started
|
|
27
|
+
|
|
28
|
+
### Route (new Page Designer page)
|
|
29
|
+
|
|
30
|
+
- **In the loader**: call `fetchPageFromLoader(args, { pageId: '...' })` and `collectComponentDataPromises(args, pagePromise)`; return `page` and `componentData` (keep loaders synchronous; return promises for streaming).
|
|
31
|
+
- **In the layout**: for each region, render `<Region page={loaderData.page} regionId="..." componentData={loaderData.componentData} />` with optional `fallbackElement` and `errorElement` for Suspense/error boundaries.
|
|
32
|
+
- **On the route module**: add `@PageType({ name, description, supportedAspectTypes })` and `@RegionDefinition([{ id, name, description, maxComponents }])` so Business Manager knows the page type and regions. Example routes: home (`_app._index.tsx`), category/PLP (`_app.category.$categoryId.tsx`).
|
|
33
|
+
|
|
34
|
+
### Component (new Page Designer component)
|
|
35
|
+
|
|
36
|
+
- **Add a metadata class** with `@Component('typeId', { name, description })` and `@AttributeDefinition()` (and optionally `@AttributeDefinition({ type: 'image' })`, `type: 'url'`, etc.) for each prop you want editable in Page Designer. Use `@RegionDefinition([...])` if the component has nested regions (e.g. a grid with slots).
|
|
37
|
+
- **Implement the React component** so it accepts those props (and strips Page Designer–only props like `component`, `page`, `componentData`, `designMetadata` before spreading to the DOM). If the component needs server data (e.g. products for a carousel), export a `loader({ componentData, context })` and optionally a `fallback` component; the registry calls the loader during `collectComponentDataPromises` and passes resolved data as the `data` prop.
|
|
38
|
+
- **Use the MCP tool `storefront_next_design_decorator`** to generate decorators instead of writing them by hand. Example components: `components/hero/index.tsx`, `components/content-card/index.tsx`, `components/product-carousel/index.tsx`.
|
|
39
|
+
|
|
40
|
+
### After changes
|
|
41
|
+
|
|
42
|
+
- **Rebuild the app** so the static registry (`lib/static-registry.ts`) is regenerated by the staticRegistry Vite plugin. Do not edit the static registry by hand.
|
|
43
|
+
|
|
44
|
+
### Design mode
|
|
45
|
+
|
|
46
|
+
- **Edit** and **Preview** mode are detected from the request via `isDesignModeActive(request)` and `isPreviewModeActive(request)` from `@salesforce/storefront-next-runtime/design/mode`. The root layout exposes `pageDesignerMode` in loader data (`'EDIT' | 'PREVIEW' | undefined`) so the tree can adapt (e.g. show outlines, disable interactions) when running inside Page Designer.
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
## MCP tools (recommended)
|
|
50
|
+
|
|
51
|
+
Use the **B2C DX MCP server** for Page Designer work instead of hand-writing decorators and metadata. Configure the B2C DX MCP server in your IDE (e.g. in MCP settings) so these tools are available.
|
|
52
|
+
|
|
53
|
+
### 1. `storefront_next_design_decorator` (STOREFRONTNEXT toolset)
|
|
54
|
+
|
|
55
|
+
Adds Page Designer decorators to an existing React component so it can be used in Business Manager. The tool analyzes the component, picks suitable props, infers types (e.g. `*Url`/`*Link` → url, `*Image` → image, `is*`/`show*` → boolean), and generates `@Component('typeId', { name, description })`, `@AttributeDefinition()` on a metadata class, and optionally `@RegionDefinition([...])` for nested regions. It skips complex or UI-only props (e.g. className, style, callbacks).
|
|
56
|
+
|
|
57
|
+
- **Auto mode** (fast): Ask in your IDE: *"Add Page Designer support to [ComponentName] with autoMode"*. The tool runs in one turn with no prompts.
|
|
58
|
+
- **Interactive mode** (control): Ask *"Add Page Designer support to [ComponentName]"* and answer questions about typeId, which props to expose, types, and optional nested regions.
|
|
59
|
+
|
|
60
|
+
### 2. `storefront_next_generate_page_designer_metadata` (STOREFRONTNEXT toolset)
|
|
61
|
+
|
|
62
|
+
Generates Page Designer metadata JSON from decorated components, page types, and aspects. Writes files under the cartridge experience folder (e.g. `cartridges/app_storefrontnext_base/cartridge/experience/`). Use after adding or changing decorators so Business Manager has up-to-date component and page-type definitions.
|
|
63
|
+
|
|
64
|
+
- **Full scan**: Run with no file list to process the whole project.
|
|
65
|
+
- **Incremental**: Pass specific file paths to regenerate only those components.
|
|
66
|
+
- **Dry run**: Use `dryRun: true` to see what would be generated without writing files.
|
|
67
|
+
|
|
68
|
+
### 3. `cartridge_deploy` (CARTRIDGES toolset)
|
|
69
|
+
|
|
70
|
+
Packages the cartridge, uploads it to Commerce Cloud via WebDAV, and unpacks it on the server. Requires Commerce Cloud credentials (e.g. `dw.json` or explicit config). Use after generating metadata so the new/updated metadata is available in Business Manager.
|
|
71
|
+
|
|
72
|
+
### Typical workflow
|
|
73
|
+
|
|
74
|
+
1. **`storefront_next_design_decorator`** — Add decorators to the component (use autoMode for a quick first pass).
|
|
75
|
+
2. **`storefront_next_generate_page_designer_metadata`** — Generate metadata JSON so the component and regions appear in Page Designer.
|
|
76
|
+
3. **`cartridge_deploy`** — Deploy to Commerce Cloud so merchants can use the component in Business Manager.
|
|
77
|
+
|
|
78
|
+
## Best Practices
|
|
79
|
+
|
|
80
|
+
1. **Keep loaders synchronous**: Return promises for Page Designer pages to enable streaming
|
|
81
|
+
2. **Use registry for components**: Register all Page Designer components with proper `typeId`
|
|
82
|
+
3. **Handle design mode**: Adapt UI when `pageDesignerMode` is `'EDIT'` or `'PREVIEW'`
|
|
83
|
+
4. **Rebuild after registry changes**: Static registry is generated at build time
|
|
84
|
+
5. **Use MCP tools**: Leverage `storefront_next_design_decorator` and `storefront_next_generate_page_designer_metadata` for faster development
|
|
85
|
+
|
|
86
|
+
**Reference:** See README.md for complete Page Designer documentation and MCP tool setup.
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Performance Optimization
|
|
2
|
+
|
|
3
|
+
## Bundle Size Limits
|
|
4
|
+
|
|
5
|
+
The application enforces strict bundle size limits defined in `package.json` under the `bundlesize` configuration. Refer to `package.json` for the complete list of limits.
|
|
6
|
+
|
|
7
|
+
**Check bundle size:**
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm bundlesize:test # Verify limits
|
|
11
|
+
pnpm bundlesize:analyze # Analyze composition
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Built-in Metrics
|
|
15
|
+
|
|
16
|
+
Enable in `config.server.ts`:
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
{
|
|
20
|
+
performance: {
|
|
21
|
+
metrics: {
|
|
22
|
+
serverPerformanceMetricsEnabled: true,
|
|
23
|
+
clientPerformanceMetricsEnabled: true,
|
|
24
|
+
serverTimingHeaderEnabled: false // Debug only
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Tracks:
|
|
31
|
+
|
|
32
|
+
- SSR operations and rendering time
|
|
33
|
+
- SCAPI API calls with parallelization
|
|
34
|
+
- Authentication operations
|
|
35
|
+
- Client-side navigations
|
|
36
|
+
|
|
37
|
+
## Best Practices
|
|
38
|
+
|
|
39
|
+
### 1. Parallel Data Fetching
|
|
40
|
+
|
|
41
|
+
**Key principle:** Return all promises simultaneously in loaders to enable parallel requests. Avoid sequential `await` calls.
|
|
42
|
+
|
|
43
|
+
**Reference:** See `data-fetching` section for detailed parallel vs sequential patterns and code examples.
|
|
44
|
+
|
|
45
|
+
### 2. Image Optimization
|
|
46
|
+
|
|
47
|
+
Use the `DynamicImage` component with WebP format:
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { DynamicImage } from '@/components/dynamic-image';
|
|
51
|
+
|
|
52
|
+
<DynamicImage
|
|
53
|
+
src={product.image.link}
|
|
54
|
+
alt={product.image.alt}
|
|
55
|
+
width={400}
|
|
56
|
+
height={400}
|
|
57
|
+
format="webp" // Default format
|
|
58
|
+
/>
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 3. Progressive Streaming
|
|
62
|
+
|
|
63
|
+
**Key principle:** Use synchronous loaders returning promises to enable progressive streaming. Await only critical data, stream the rest.
|
|
64
|
+
|
|
65
|
+
**Reference:** See `data-fetching` section for detailed streaming patterns including mixed strategies (awaited + streamed).
|
|
66
|
+
|
|
67
|
+
### 4. Lighthouse Audits
|
|
68
|
+
|
|
69
|
+
Monitor performance metrics:
|
|
70
|
+
|
|
71
|
+
- Preload critical CSS
|
|
72
|
+
- Use WebP images by default
|
|
73
|
+
- Lazy load below-the-fold content
|
|
74
|
+
- Optimize font loading
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pnpm lighthouse:ci # Run Lighthouse CI
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Reference:** See README-PERFORMANCE.md for complete performance optimization documentation.
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# Common Pitfalls
|
|
2
|
+
|
|
3
|
+
## 1. Using Client Loaders/Actions
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
// ❌ NEVER USE - Client loaders are not permitted
|
|
7
|
+
export function clientLoader() { ... }
|
|
8
|
+
|
|
9
|
+
// ❌ NEVER USE - Client actions are not permitted
|
|
10
|
+
export function clientAction() { ... }
|
|
11
|
+
|
|
12
|
+
// ✅ REQUIRED - Server-only data loading
|
|
13
|
+
export function loader({ context }: LoaderFunctionArgs) {
|
|
14
|
+
const clients = createApiClients(context);
|
|
15
|
+
return { product: clients.shopperProducts.getProduct({...}) };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// ✅ REQUIRED - Server-only actions
|
|
19
|
+
export async function action({ request, context }: ActionFunctionArgs) {
|
|
20
|
+
const clients = createApiClients(context);
|
|
21
|
+
// Handle mutation on server
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Decision tree:**
|
|
26
|
+
|
|
27
|
+
```text
|
|
28
|
+
Need data for page render?
|
|
29
|
+
└─ Use server `loader`
|
|
30
|
+
|
|
31
|
+
Need to handle mutations (form submissions, cart updates)?
|
|
32
|
+
└─ Use server `action`
|
|
33
|
+
|
|
34
|
+
Need on-demand fetching after page load?
|
|
35
|
+
└─ Use `useScapiFetcher` (search, modals, infinite scroll)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**Key Point:** ALL SCAPI requests happen on the server:
|
|
39
|
+
- `loader`: Runs on server, SCAPI direct (prod) or proxied (dev)
|
|
40
|
+
- `action`: Runs on server, handles mutations securely
|
|
41
|
+
- `useScapiFetcher`: Triggers server route that calls SCAPI
|
|
42
|
+
|
|
43
|
+
## 2. Module-Level i18n in Schemas
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
// ❌ RACE CONDITION
|
|
47
|
+
const schema = z.object({
|
|
48
|
+
email: z.string().email(t('error')),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// ✅ FACTORY PATTERN
|
|
52
|
+
export const createSchema = (t: TFunction) => {
|
|
53
|
+
return z.object({
|
|
54
|
+
email: z.string().email(t('error')),
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## 3. Using Async Loaders (Blocks Page Transitions)
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
// ❌ BLOCKS PAGE TRANSITIONS - Async loader with await
|
|
63
|
+
export async function loader({ context }: LoaderFunctionArgs) {
|
|
64
|
+
const product = await fetchProduct(); // Blocks!
|
|
65
|
+
const reviews = await fetchReviews(); // Blocks!
|
|
66
|
+
return { product, reviews };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ✅ NON-BLOCKING - Synchronous loader returning promises
|
|
70
|
+
export function loader({ context }: LoaderFunctionArgs): PageData {
|
|
71
|
+
return {
|
|
72
|
+
product: fetchProduct(), // Streams progressively
|
|
73
|
+
reviews: fetchReviews(), // Streams progressively
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Key insight:** Defining loaders as `async` and using `await` causes the entire page transition to block until all data resolves. Use synchronous loaders returning promises for streaming.
|
|
79
|
+
|
|
80
|
+
**Reference:** See `data-fetching` section for comprehensive loader patterns including mixed strategies (awaited + streamed).
|
|
81
|
+
|
|
82
|
+
## 4. Modifying shadcn/ui
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
// ❌ NEVER modify src/components/ui/
|
|
86
|
+
|
|
87
|
+
// ✅ Create wrapper
|
|
88
|
+
import { Button } from '@/components/ui/button';
|
|
89
|
+
export function MyButton(props) {
|
|
90
|
+
return <Button {...props} className="custom" />;
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## 5. Missing Namespace in i18n
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
// ❌ MISSING NAMESPACE
|
|
98
|
+
const {t} = useTranslation();
|
|
99
|
+
t('title'); // Won't work
|
|
100
|
+
|
|
101
|
+
// ✅ USE NAMESPACE
|
|
102
|
+
const {t} = useTranslation('product');
|
|
103
|
+
t('title'); // Works
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## 6. Not Using Context in Server Loaders
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
// ❌ MISSING CONTEXT
|
|
110
|
+
export function loader() {
|
|
111
|
+
const config = getConfig(); // Wrong!
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ✅ PASS CONTEXT
|
|
115
|
+
export function loader({ context }: LoaderFunctionArgs) {
|
|
116
|
+
const config = getConfig(context); // Correct
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## 7. Forgetting to Namespace i18n Keys
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
// ❌ MISSING NAMESPACE
|
|
124
|
+
const { t } = useTranslation();
|
|
125
|
+
t('title'); // Won't work without namespace
|
|
126
|
+
|
|
127
|
+
// ✅ USE NAMESPACE
|
|
128
|
+
const { t } = useTranslation('product');
|
|
129
|
+
t('title'); // Works
|
|
130
|
+
|
|
131
|
+
// OR
|
|
132
|
+
const { t } = getTranslation();
|
|
133
|
+
t('product:title'); // Works
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## 8. Using JavaScript Files
|
|
137
|
+
|
|
138
|
+
```text
|
|
139
|
+
❌ .js, .jsx, .mjs, .cjs files are BLOCKED
|
|
140
|
+
✅ Use .ts, .tsx files only
|
|
141
|
+
```
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# Storefront Next - Development Guidelines (Quick Reference)
|
|
2
|
+
|
|
3
|
+
⚠️ **READ THESE CRITICAL RULES BEFORE WRITING ANY CODE** ⚠️
|
|
4
|
+
|
|
5
|
+
## 🏗️ Architecture Overview
|
|
6
|
+
|
|
7
|
+
**Server-rendered SPA** built on React Server Components:
|
|
8
|
+
- **React Router 7** in framework mode
|
|
9
|
+
- **Managed Runtime (MRT)** as data orchestration layer
|
|
10
|
+
- **All SCAPI requests execute on MRT server** (both SSR and client-side navigation)
|
|
11
|
+
|
|
12
|
+
**Key Point**: Client-side routing does NOT mean client-side data fetching. Loaders always run on the server.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## 🚨 Non-Negotiable Rules
|
|
17
|
+
|
|
18
|
+
### 1. Server-Only Data Loading
|
|
19
|
+
|
|
20
|
+
✅ **REQUIRED**: Use server `loader` for all SCAPI data fetching
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
export function loader({ context }: LoaderFunctionArgs): PageData {
|
|
24
|
+
const clients = createApiClients(context);
|
|
25
|
+
return {
|
|
26
|
+
product: clients.shopperProducts.getProduct({...}), // Promise - streams
|
|
27
|
+
reviews: clients.shopperProducts.getReviews({...}), // Promise - streams
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
**Why?** Keeps SCAPI requests on MRT server for security, performance, and bundle size.
|
|
33
|
+
|
|
34
|
+
### 2. Synchronous Loaders (Not Async)
|
|
35
|
+
|
|
36
|
+
✅ **CRITICAL**: Loaders must be **synchronous functions that return promises**, NOT async functions.
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
// ✅ CORRECT - Enables streaming
|
|
40
|
+
export function loader({ context }: LoaderFunctionArgs): PageData {
|
|
41
|
+
const clients = createApiClients(context);
|
|
42
|
+
return {
|
|
43
|
+
product: clients.shopperProducts.getProduct({...}), // Promise - streams
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ❌ AVOID - Blocks page transitions
|
|
48
|
+
export async function loader({ context }: LoaderFunctionArgs) {
|
|
49
|
+
const product = await clients.shopperProducts.getProduct({...}); // Blocks!
|
|
50
|
+
return { product };
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**Why?** Async loaders block page transitions. Synchronous loaders enable progressive streaming.
|
|
55
|
+
|
|
56
|
+
### 3. TypeScript-Only
|
|
57
|
+
|
|
58
|
+
✅ **REQUIRED**: Use `.ts` and `.tsx` file extensions
|
|
59
|
+
❌ **BLOCKED**: `.js`, `.jsx`, `.mjs`, `.cjs` files are forbidden by ESLint
|
|
60
|
+
|
|
61
|
+
### 4. Use createPage() HOC
|
|
62
|
+
|
|
63
|
+
✅ **RECOMMENDED**: Use `createPage()` for standardized page patterns
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { createPage } from '@/components/create-page';
|
|
67
|
+
|
|
68
|
+
const ProductPage = createPage({
|
|
69
|
+
component: ProductView,
|
|
70
|
+
fallback: <ProductSkeleton />
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
export default ProductPage;
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 5. Tailwind CSS 4 Only
|
|
77
|
+
|
|
78
|
+
✅ **REQUIRED**: Use Tailwind utility classes only
|
|
79
|
+
❌ **BLOCKED**: No inline styles (`style={{...}}`), no CSS modules, no separate CSS files
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
// ✅ CORRECT - Tailwind utilities
|
|
83
|
+
<div className="rounded-lg border border-border bg-card p-4">
|
|
84
|
+
<h2 className="text-lg font-semibold text-card-foreground">
|
|
85
|
+
{product.name}
|
|
86
|
+
</h2>
|
|
87
|
+
</div>
|
|
88
|
+
|
|
89
|
+
// ❌ AVOID - Inline styles
|
|
90
|
+
<div style={{ padding: '1rem' }}>
|
|
91
|
+
|
|
92
|
+
// ❌ AVOID - CSS modules
|
|
93
|
+
import styles from './product-card.module.css';
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
**Why?** Consistent styling approach, better performance, automatic dark mode support via theme variables.
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## 📋 Quick Patterns
|
|
101
|
+
|
|
102
|
+
### Data Fetching
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import { createApiClients } from '@/lib/api-clients';
|
|
106
|
+
|
|
107
|
+
export function loader({ context }: LoaderFunctionArgs): PageData {
|
|
108
|
+
const clients = createApiClients(context);
|
|
109
|
+
return {
|
|
110
|
+
product: clients.shopperProducts.getProduct({...}),
|
|
111
|
+
reviews: clients.shopperProducts.getReviews({...}),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
**See `data-fetching` section for:** loaders, actions, useScapiFetcher, parallel requests, data flow
|
|
117
|
+
|
|
118
|
+
### Authentication
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { getAuth } from '@/middlewares/auth.server';
|
|
122
|
+
|
|
123
|
+
export function loader({ context }: LoaderFunctionArgs) {
|
|
124
|
+
const auth = getAuth(context);
|
|
125
|
+
return {
|
|
126
|
+
isGuest: auth.userType === 'guest',
|
|
127
|
+
customerId: auth.customer_id
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**See `auth` section for:** cookie architecture, client usage, token management
|
|
133
|
+
|
|
134
|
+
### Configuration
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
// Components
|
|
138
|
+
import { useConfig } from '@/config';
|
|
139
|
+
const config = useConfig();
|
|
140
|
+
|
|
141
|
+
// Loaders/Actions
|
|
142
|
+
import { getConfig } from '@/config';
|
|
143
|
+
const config = getConfig(context);
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
**See `config` section for:** adding config, environment variables, security
|
|
147
|
+
|
|
148
|
+
### Internationalization
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
// Components
|
|
152
|
+
import { useTranslation } from 'react-i18next';
|
|
153
|
+
const { t } = useTranslation('product');
|
|
154
|
+
|
|
155
|
+
// Loaders/Actions
|
|
156
|
+
import { getTranslation } from '@/lib/i18next';
|
|
157
|
+
const { t } = getTranslation(context);
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
**See `i18n` section for:** validation schemas (factory pattern), language switching, extensions
|
|
161
|
+
|
|
162
|
+
### Components
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
// Suspense boundaries
|
|
166
|
+
import { Suspense } from 'react';
|
|
167
|
+
import { Await } from 'react-router';
|
|
168
|
+
|
|
169
|
+
<Suspense fallback={<ProductSkeleton />}>
|
|
170
|
+
<Await resolve={product}>
|
|
171
|
+
{(data) => <ProductHeader product={data} />}
|
|
172
|
+
</Await>
|
|
173
|
+
</Suspense>
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
**See `components` section for:** createPage HOC, file organization, best practices
|
|
177
|
+
|
|
178
|
+
### Styling
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
// Tailwind utility classes
|
|
182
|
+
<div className="bg-background text-foreground border-border">
|
|
183
|
+
<button className="bg-primary text-primary-foreground rounded-md px-4 py-2">
|
|
184
|
+
Click me
|
|
185
|
+
</button>
|
|
186
|
+
</div>
|
|
187
|
+
|
|
188
|
+
// shadcn/ui: Add via npx shadcn@latest add <component-name>
|
|
189
|
+
import { Button } from '@/components/ui/button';
|
|
190
|
+
import { Card } from '@/components/ui/card';
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
**See `styling` section for:** Tailwind CSS 4 rules, Shadcn/ui components, dark mode, responsive design
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## 🔍 Get Detailed Guidelines
|
|
198
|
+
|
|
199
|
+
Use the `storefront_next_development_guidelines` MCP tool with specific sections:
|
|
200
|
+
|
|
201
|
+
```json
|
|
202
|
+
{
|
|
203
|
+
"sections": ["data-fetching", "components", "testing"]
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
**Available sections:**
|
|
208
|
+
- `data-fetching` - Loaders, actions, useScapiFetcher, data flow
|
|
209
|
+
- `components` - createPage HOC, Suspense, file organization
|
|
210
|
+
- `styling` - Tailwind CSS 4, Shadcn/ui, styling guidelines
|
|
211
|
+
- `testing` - Vitest, Storybook, coverage requirements
|
|
212
|
+
- `auth` - Authentication and session management
|
|
213
|
+
- `config` - Configuration system
|
|
214
|
+
- `i18n` - Internationalization patterns
|
|
215
|
+
- `state-management` - Client-side state with Zustand
|
|
216
|
+
- `page-designer` - Page Designer integration
|
|
217
|
+
- `performance` - Optimization techniques
|
|
218
|
+
- `extensions` - Extension development
|
|
219
|
+
- `pitfalls` - Common mistakes to avoid
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
**When in doubt:**
|
|
224
|
+
1. Check existing code for similar examples
|
|
225
|
+
2. Use the MCP tool to get detailed section guidance
|
|
226
|
+
3. Follow architectural principles: server-only, streaming, TypeScript
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Client-Side State Management
|
|
2
|
+
|
|
3
|
+
## Zustand Store Pattern
|
|
4
|
+
|
|
5
|
+
Storefront Next uses Zustand for client-side state (basket, wishlist):
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
// src/middlewares/basket.client.ts
|
|
9
|
+
import {create} from 'zustand';
|
|
10
|
+
|
|
11
|
+
interface BasketStore {
|
|
12
|
+
basket: Basket | null;
|
|
13
|
+
setBasket: (basket: Basket | null) => void;
|
|
14
|
+
clearBasket: () => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const useBasketStore = create<BasketStore>((set) => ({
|
|
18
|
+
basket: null,
|
|
19
|
+
setBasket: (basket) => set({basket}),
|
|
20
|
+
clearBasket: () => set({basket: null}),
|
|
21
|
+
}));
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Context Integration
|
|
25
|
+
|
|
26
|
+
Access Zustand state via context helpers:
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { getBasket, updateBasket } from '@/middlewares/basket.client';
|
|
30
|
+
|
|
31
|
+
// In clientLoader
|
|
32
|
+
export const clientLoader: ClientLoaderFunction = ({ context }) => {
|
|
33
|
+
const basket = getBasket(context);
|
|
34
|
+
return { basket, itemCount: basket?.productItems?.length ?? 0 };
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// In components
|
|
38
|
+
function CartIcon() {
|
|
39
|
+
const basket = getBasket(context);
|
|
40
|
+
return <Badge count={basket?.productItems?.length ?? 0} />;
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Update Pattern
|
|
45
|
+
|
|
46
|
+
After mutations, update the store:
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
export async function clientAction({request, context}: ActionFunctionArgs) {
|
|
50
|
+
const formData = await request.formData();
|
|
51
|
+
const productId = formData.get('productId') as string;
|
|
52
|
+
|
|
53
|
+
const basket = getBasket(context);
|
|
54
|
+
const clients = createApiClients(context);
|
|
55
|
+
|
|
56
|
+
const {data: updatedBasket} = await clients.shopperBasketsV2.addItemToBasket({
|
|
57
|
+
params: {path: {basketId: basket.basketId}},
|
|
58
|
+
body: [{productId, quantity: 1}],
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Update Zustand store
|
|
62
|
+
updateBasket(context, updatedBasket);
|
|
63
|
+
|
|
64
|
+
return Response.json({success: true, basket: updatedBasket});
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Best Practices
|
|
69
|
+
|
|
70
|
+
1. **Use for ephemeral client state**: Shopping cart, UI state, temporary selections
|
|
71
|
+
2. **Don't duplicate server state**: Prefer React Router loaders for server data
|
|
72
|
+
3. **Keep stores focused**: Separate stores for basket, wishlist, etc.
|
|
73
|
+
4. **Sync with server**: Update store after successful mutations
|
|
74
|
+
|
|
75
|
+
For full documentation on client-side state management patterns, see the Zustand documentation and React Router state management patterns.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Styling Guidelines
|
|
2
|
+
|
|
3
|
+
## Rules
|
|
4
|
+
|
|
5
|
+
- ✅ **Use Tailwind utility classes** in component JSX
|
|
6
|
+
- ✅ **Use `cn()` utility** for conditional classes (`import { cn } from '@/lib/utils'`)
|
|
7
|
+
- ✅ **Follow mobile-first** responsive patterns (`md:`, `lg:` breakpoints)
|
|
8
|
+
- ❌ **NO inline styles** (`style={{...}}`)
|
|
9
|
+
- ❌ **NO CSS modules** (`.module.css` files)
|
|
10
|
+
- ❌ **NO separate CSS files** for component styles
|
|
11
|
+
- ✅ **Custom CSS** only in `src/app.css` for global styles and theme configuration
|
|
12
|
+
|
|
13
|
+
## Shadcn/ui Components
|
|
14
|
+
|
|
15
|
+
**Adding components:**
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npx shadcn@latest add <component-name>
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Rules:**
|
|
22
|
+
|
|
23
|
+
- ✅ **DO** customize components by editing files in `src/components/ui/`
|
|
24
|
+
- ❌ **DON'T** create custom components inside `src/components/ui/`
|
|
25
|
+
- ❌ **DON'T** manually copy components (use CLI instead)
|
|
26
|
+
|
|
27
|
+
## Dark Mode
|
|
28
|
+
|
|
29
|
+
Dark mode is supported via CSS variables and the `.dark` class. Theme variables automatically adapt:
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
<div className="bg-background text-foreground border-border">
|
|
33
|
+
<button className="bg-primary text-primary-foreground">
|
|
34
|
+
Click me
|
|
35
|
+
</button>
|
|
36
|
+
</div>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Responsive Design
|
|
40
|
+
|
|
41
|
+
Follow mobile-first responsive design:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
|
45
|
+
{/* Content */}
|
|
46
|
+
</div>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
**Reference:** See [README-UI-STYLING.md](README-UI-STYLING.md) for complete UI and styling documentation.
|