From e83e718dbebcaee59227c370373407f72deab36b Mon Sep 17 00:00:00 2001 From: Yuming Hsieh Date: Thu, 29 Jan 2026 09:28:46 -0500 Subject: [PATCH 1/5] dev guidelines tool --- packages/b2c-dx-mcp/README.md | 16 +- .../src/tools/storefrontnext/content/auth.md | 62 ++++ .../storefrontnext/content/components.md | 110 ++++++ .../tools/storefrontnext/content/config.md | 105 ++++++ .../storefrontnext/content/data-fetching.md | 323 ++++++++++++++++++ .../storefrontnext/content/extensions.md | 80 +++++ .../src/tools/storefrontnext/content/i18n.md | 121 +++++++ .../storefrontnext/content/page-designer.md | 86 +++++ .../storefrontnext/content/performance.md | 80 +++++ .../tools/storefrontnext/content/pitfalls.md | 141 ++++++++ .../storefrontnext/content/quick-reference.md | 288 ++++++++++++++++ .../content/state-management.md | 75 ++++ .../storefrontnext/developer-guidelines.ts | 133 ++++++++ .../src/tools/storefrontnext/index.ts | 32 +- packages/b2c-dx-mcp/test/registry.test.ts | 8 +- 15 files changed, 1634 insertions(+), 26 deletions(-) create mode 100644 packages/b2c-dx-mcp/src/tools/storefrontnext/content/auth.md create mode 100644 packages/b2c-dx-mcp/src/tools/storefrontnext/content/components.md create mode 100644 packages/b2c-dx-mcp/src/tools/storefrontnext/content/config.md create mode 100644 packages/b2c-dx-mcp/src/tools/storefrontnext/content/data-fetching.md create mode 100644 packages/b2c-dx-mcp/src/tools/storefrontnext/content/extensions.md create mode 100644 packages/b2c-dx-mcp/src/tools/storefrontnext/content/i18n.md create mode 100644 packages/b2c-dx-mcp/src/tools/storefrontnext/content/page-designer.md create mode 100644 packages/b2c-dx-mcp/src/tools/storefrontnext/content/performance.md create mode 100644 packages/b2c-dx-mcp/src/tools/storefrontnext/content/pitfalls.md create mode 100644 packages/b2c-dx-mcp/src/tools/storefrontnext/content/quick-reference.md create mode 100644 packages/b2c-dx-mcp/src/tools/storefrontnext/content/state-management.md create mode 100644 packages/b2c-dx-mcp/src/tools/storefrontnext/developer-guidelines.ts diff --git a/packages/b2c-dx-mcp/README.md b/packages/b2c-dx-mcp/README.md index d810bf37..7b12d67b 100644 --- a/packages/b2c-dx-mcp/README.md +++ b/packages/b2c-dx-mcp/README.md @@ -214,13 +214,13 @@ Storefront Next development tools for building modern storefronts. | Tool | Description | |------|-------------| -| `sfnext_development_guidelines` | Get Storefront Next development guidelines and best practices | -| `sfnext_site_theming` | Configure and manage site theming for Storefront Next | -| `sfnext_figma_to_component_workflow` | Convert Figma designs to Storefront Next components | -| `sfnext_generate_component` | Generate a new Storefront Next component | -| `sfnext_map_tokens_to_theme` | Map design tokens to Storefront Next theme configuration | -| `sfnext_design_decorator` | Apply design decorators to Storefront Next components | -| `sfnext_generate_page_designer_metadata` | Generate Page Designer metadata for Storefront Next components | +| `storefront_next_development_guidelines` | Get Storefront Next development guidelines and best practices | +| `storefront_next_site_theming` | Configure and manage site theming for Storefront Next | +| `storefront_next_figma_to_component_workflow` | Convert Figma designs to Storefront Next components | +| `storefront_next_generate_component` | Generate a new Storefront Next component | +| `storefront_next_map_tokens_to_theme` | Map design tokens to Storefront Next theme configuration | +| `storefront_next_design_decorator` | Apply design decorators to Storefront Next components | +| `storefront_next_generate_page_designer_metadata` | Generate Page Designer metadata for Storefront Next components | | `scapi_discovery` | Discover available SCAPI endpoints and capabilities | | `scapi_custom_api_discovery` | Discover custom SCAPI API endpoints | | `mrt_bundle_push` | Build, push bundle (optionally deploy) | @@ -296,7 +296,7 @@ npx mcp-inspector --cli node bin/dev.js --toolsets all --allow-non-ga-tools --me # Call a specific tool npx mcp-inspector --cli node bin/dev.js --toolsets all --allow-non-ga-tools \ --method tools/call \ - --tool-name sfnext_design_decorator + --tool-name storefront_next_design_decorator ``` #### 2. IDE Integration diff --git a/packages/b2c-dx-mcp/src/tools/storefrontnext/content/auth.md b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/auth.md new file mode 100644 index 00000000..b962df2f --- /dev/null +++ b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/auth.md @@ -0,0 +1,62 @@ +# Authentication & Session Management + +## Architecture + +Split-cookie architecture with server/client contexts: + +- **Server middleware** (`auth.server.ts`): Manages SLAS tokens, writes cookies +- **Client middleware** (`auth.client.ts`): Reads cookies, maintains cache +- **React Context** (`AuthProvider`): Provides auth state to components + +## Cookie Design + +| Cookie Name | Purpose | User Type | Expiry | HttpOnly | +|-------------|---------|-----------|--------|----------| +| `cc-nx-g` | Guest refresh token | Guest | 30 days | No | +| `cc-nx` | Registered refresh token | Registered | 90 days | No | +| `cc-at` | Access token | Both | 30 min | No | +| `usid` | User session ID | Both | Matches refresh | No | +| `customerId` | Customer ID | Registered | Matches refresh | No | + +**Key Points**: + +- Only ONE refresh token exists (guest OR registered, never both) +- User type derived from which refresh token exists +- Cookies auto-namespaced with `siteId` +- Tokens auto-refresh when expired + +## Usage in Loaders/Actions + +```typescript +import { getAuth } from '@/middlewares/auth.server'; + +export function loader({ context }: LoaderFunctionArgs) { + const auth = getAuth(context); + + // Access auth properties + const accessToken = auth.access_token; + const customerId = auth.customer_id; + const isGuest = auth.userType === 'guest'; + const isRegistered = auth.userType === 'registered'; + + return { isGuest, customerId }; +} +``` + +## Usage in Components + +```typescript +import { useAuth } from '@/providers/auth'; + +export function MyComponent() { + const auth = useAuth(); + + if (auth?.userType === 'guest') { + return ; + } + + return
Welcome, customer {auth?.customer_id}
; +} +``` + +**Reference:** See README-AUTH.md for complete authentication documentation. diff --git a/packages/b2c-dx-mcp/src/tools/storefrontnext/content/components.md b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/components.md new file mode 100644 index 00000000..3592300a --- /dev/null +++ b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/components.md @@ -0,0 +1,110 @@ +# Component Patterns + +## Use the `createPage` HOC + +The `createPage` higher-order component standardizes page patterns with built-in Suspense and page key handling: + +```typescript +import { use } from 'react'; +import { createPage } from '@/components/create-page'; + +// Define your view component +function ProductView({ + product, + category +}: { + product: Promise; + category?: Promise +}) { + const productData = use(product); + const categoryData = category ? use(category) : null; + + return ( +
+

{productData.name}

+ {categoryData &&

Category: {categoryData.name}

} +
+ ); +} + +// Create page with fallback +const ProductPage = createPage({ + component: ProductView, + fallback: +}); + +export default ProductPage; +``` + +**Benefits:** + +- Eliminates repetitive Suspense/Await boilerplate +- Consistent loading states across pages +- Built-in page key management for navigation transitions +- Type-safe with full TypeScript support + +## shadcn/ui Components + +**RULES**: + +- ✅ Add via: `npx shadcn@latest add ` +- ❌ DO NOT modify `src/components/ui/` directly +- ✅ Create custom components elsewhere + +## Suspense Boundaries + +Use granular Suspense boundaries for better UX: + +```typescript +// ✅ RECOMMENDED - Multiple Suspense boundaries +export default function ProductPage({ loaderData: { product, reviews } }) { + return ( +
+ }> + + {(data) => } + + + + }> + + {(data) => } + + +
+ ); +} + +// ⚠️ OK - Single Suspense boundary (less granular) +export default createPage({ + component: ProductView, + fallback: +}); +``` + +## File Organization + +``` +src/components/product-tile/ +├── index.tsx # Component +├── index.test.tsx # Tests +└── stories/ + ├── index.stories.tsx # Storybook stories + └── __snapshots__/ # Storybook snapshots (optional) + └── product-tile-snapshot.tsx.snap + +# Skeleton components are separate components +src/components/product-skeleton/ +├── index.tsx +├── index.test.tsx +└── stories/ + └── index.stories.tsx +``` + +## Best Practices + +1. **Extract view components** - Separate data handling from presentation +2. **Type safety** - Define proper TypeScript interfaces +3. **Consistent fallbacks** - Reusable skeleton components +4. **Colocate tests** - Keep tests next to components +5. **Story coverage** - Create stories for all reusable components diff --git a/packages/b2c-dx-mcp/src/tools/storefrontnext/content/config.md b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/config.md new file mode 100644 index 00000000..da5b9e4d --- /dev/null +++ b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/config.md @@ -0,0 +1,105 @@ +# Configuration Management + +## Overview + +All configuration in `config.server.ts` with environment variable overrides. + +## Adding Configuration + +1. **Define type in `src/config/schema.ts`**: + +```typescript +export type Config = { + app: { + myFeature: { + enabled: boolean; + maxItems: number; + }; + }; +}; +``` + +2. **Add defaults in `config.server.ts`**: + +```typescript +export default defineConfig({ + app: { + myFeature: { + enabled: false, + maxItems: 10, + }, + }, +}); +``` + +3. **Override via environment variables**: + +```bash +PUBLIC__app__myFeature__enabled=true +PUBLIC__app__myFeature__maxItems=20 +``` + +## Usage Patterns + +**In React Components**: + +```typescript +import { useConfig } from '@/config'; + +export function MyComponent() { + const config = useConfig(); + + if (config.app.myFeature.enabled) { + const maxItems = config.app.myFeature.maxItems; + // Your feature code + } +} +``` + +**In Loaders/Actions**: + +```typescript +import { getConfig } from '@/config'; + +export function loader({ context }: LoaderFunctionArgs) { + const config = getConfig(context); + + if (config.app.myFeature.enabled) { + // Your loader code + } +} +``` + +## Environment Variable Rules + +Use the `PUBLIC__` prefix with double underscores (`__`) to set any config path: + +```bash +# Environment variable → Config path +PUBLIC__app__site__locale=en-US → config.app.site.locale +PUBLIC__app__site__currency=EUR → config.app.site.currency +``` + +Values are automatically parsed (numbers, booleans, JSON arrays/objects). + +Rules: +1. **`PUBLIC__` prefix**: Exposed to browser (client-safe values) +2. **No prefix**: Server-only (secrets, never exposed) +3. **`__` separator**: Navigate nested paths (`PUBLIC__app__site__locale`) +4. **Case-insensitive**: All casings work +5. **Auto-parsing**: Strings, numbers, booleans, JSON +6. **Validation**: Paths must exist in `config.server.ts` + +## Security + +```bash +# ✅ Safe for client (PUBLIC__ prefix) +PUBLIC__app__commerce__api__clientId=abc123 + +# ✅ Server-only (no prefix) +COMMERCE_API_SLAS_SECRET=your-secret +``` + +Read server-only secrets directly from `process.env` - never add to config. + +**Reference:** See src/config/README.md for complete configuration documentation. diff --git a/packages/b2c-dx-mcp/src/tools/storefrontnext/content/data-fetching.md b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/data-fetching.md new file mode 100644 index 00000000..111e5889 --- /dev/null +++ b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/data-fetching.md @@ -0,0 +1,323 @@ +# Data Fetching Patterns + +## Loader Functions + +**IMPORTANT**: This project **mandates server-only data loading**. Every UI route must only export a `loader` function. + +### Critical Rule: Synchronous Loaders for Streaming + +**IMPORTANT**: Loaders should be **synchronous functions that return objects containing promises**, NOT async functions. This enables non-blocking page transitions and streaming SSR. + +```typescript +// ✅ CORRECT - Synchronous loader returning promises +export function loader({ context }: LoaderFunctionArgs): ProductPageData { + const clients = createApiClients(context); + return { + product: clients.shopperProducts.getProduct({...}), // Promise - streams + reviews: clients.shopperProducts.getReviews({...}), // Promise - streams + }; +} + +// ❌ AVOID - Async loader blocks page transitions +export async function loader({ context }: LoaderFunctionArgs): Promise { + const product = await clients.shopperProducts.getProduct({...}); // Blocks! + return { product }; +} +``` + +**Why this matters:** +- Async loaders with `await` **block the entire page transition** until all data resolves +- Synchronous loaders returning promises allow React to **stream data progressively** +- Each promise resolves independently, enabling granular Suspense boundaries +- Users see content as it becomes available, not all at once + +**Behavior**: +- Initial load: Runs on server (SSR) +- Navigation: Runs on server (XHR/fetch to server) +- SCAPI requests always on MRT + +## Data Loading Strategies + +### Pattern 1: Awaited Data (Blocking) + +```typescript +// ⚠️ BLOCKS rendering until all data is ready +export async function loader({ params, context }: LoaderFunctionArgs) { + const clients = createApiClients(context); + return { + product: await clients.shopperProducts.getProduct({ + params: { path: { id: params.productId } } + }).then(({ data }) => data) + }; +} +``` + +**Use when:** Critical data must be available before rendering (SEO, above-the-fold content) + +### Pattern 2: Deferred Data (Streaming) + +```typescript +// ✅ RECOMMENDED - Streams data progressively +export function loader({ params, context }: LoaderFunctionArgs) { + const clients = createApiClients(context); + return { + // Return promises directly - they'll stream to client + product: clients.shopperProducts.getProduct({ + params: { path: { id: params.productId } } + }).then(({ data }) => data), + + reviews: clients.shopperProducts.getReviews({ + params: { path: { id: params.productId } } + }).then(({ data }) => data) + }; +} +``` + +**Use when:** Non-critical data can load after initial render + +### Pattern 3: Mixed Strategy + +```typescript +// ✅ BEST OF BOTH - Critical data awaited, rest streamed +export async function loader({ params, context }: LoaderFunctionArgs) { + const clients = createApiClients(context); + + // Await critical data + const product = await clients.shopperProducts.getProduct({ + params: { path: { id: params.productId } } + }).then(({ data }) => data); + + return { + product, // Resolved + reviews: clients.shopperProducts.getReviews({ + params: { path: { id: params.productId } } + }).then(({ data }) => data), // Streamed + recommendations: clients.shopperProducts.getRecommendations({ + params: { path: { id: params.productId } } + }).then(({ data }) => data) // Streamed + }; +} +``` + +## Action Functions + +Handle mutations (form submissions, cart updates): + +```typescript +import {data, redirect} from 'react-router'; + +export async function action({request, context}: ActionFunctionArgs) { + const formData = await request.formData(); + const productId = formData.get('productId') as string; + + const clients = createApiClients(context); + + try { + await clients.shopperBasketsV2.addItemToBasket({ + params: { + path: {basketId}, + body: {productId, quantity: 1}, + }, + }); + + return data({success: true}); + } catch (error) { + return data({success: false, error: error.message}, {status: 400}); + } +} +``` + +## Interactive Data Fetching: useScapiFetcher + +For on-demand, user-triggered data fetching (after page load), use the `useScapiFetcher` hook instead of loaders. + +### `loader` vs `useScapiFetcher` + +| Aspect | `loader` | `useScapiFetcher` | +|--------|----------|-------------------| +| **When it runs** | Route navigation (page load) | On-demand (user interaction) | +| **Triggered by** | URL change | Component code (useEffect, button click) | +| **Data availability** | Before/during component render (streamed) | After component mounts | +| **Execution context** | Server (MRT) | Triggers server route | +| **Use case** | Initial page data | Dynamic, interactive fetching | + +### How `useScapiFetcher` Works + +```text +Component calls useScapiFetcher() + ↓ +Hook builds URL: /resource/api/client/{encoded-params} + ↓ +fetcher.load() or fetcher.submit() + ↓ +resource.api.client.$resource.ts loader/action runs ON SERVER + ↓ +createApiClients(context) makes SCAPI call (server-side) + ↓ +JSON response returned to component +``` + +**Important:** Even though you call `useScapiFetcher` from the browser, the actual SCAPI requests still happen **on the server** through the resource route, keeping credentials secure. + +### Example: Search Suggestions + +```typescript +import { useScapiFetcher } from '@/hooks/use-scapi-fetcher'; +import { useMemo, useCallback } from 'react'; + +export function useSearchSuggestions({ q, limit, currency }) { + // Prepare SCAPI parameters + const parameters = useMemo( + () => ({ + params: { + query: { q, limit, currency } + } + }), + [q, limit, currency] + ); + + // Hook automatically routes to server + const fetcher = useScapiFetcher( + 'shopperSearch', // SCAPI client + 'getSearchSuggestions', // Method name + parameters // Parameters + ); + + const refetch = useCallback(async () => { + await fetcher.load(); // Triggers server request + }, [fetcher]); + + return { + data: fetcher.data, + isLoading: fetcher.state === 'loading', + refetch + }; +} +``` + +### When to Use Each Approach + +| Scenario | Use | +|----------|-----| +| Load product data when visiting `/product/123` | `loader` | +| Load checkout data | `loader` | +| Search suggestions as user types | `useScapiFetcher` | +| Update customer profile in modal | `useScapiFetcher` | +| Load recommendations after page loads | `useScapiFetcher` | +| Fetch bonus products when modal opens | `useScapiFetcher` | +| Infinite scroll / Load more | `useScapiFetcher` | + +### Timeline Comparison + +```text +┌─────────────────────────────────────────────────────────────────┐ +│ loader (Server) │ +├─────────────────────────────────────────────────────────────────┤ +│ User clicks link → Server loader() → Stream data → Page render │ +│ │ +│ Timeline: [navigate] → [server fetch] → [stream to client] │ +│ │ +│ Data available: Streamed during render via Suspense │ +└─────────────────────────────────────────────────────────────────┘ + +┌─────────────────────────────────────────────────────────────────┐ +│ useScapiFetcher │ +├─────────────────────────────────────────────────────────────────┤ +│ Page loads → Component mounts → User types → fetcher.load() │ +│ │ +│ Timeline: [render] → [user action] → [fetch] → [re-render] │ +│ │ +│ Data available: AFTER user action, component re-renders │ +└─────────────────────────────────────────────────────────────────┘ +``` + +## API Client Usage + +Always use `createApiClients(context)`: + +```typescript +import { createApiClients } from '@/lib/api-clients'; + +export function loader({ context }: LoaderFunctionArgs) { + const clients = createApiClients(context); + + // All SCAPI clients with full type safety: + clients.shopperProducts.getProduct({...}); + clients.shopperCustomers.getCustomer({...}); + clients.shopperBasketsV2.getBasket({...}); + clients.shopperSearch.productSearch({...}); + clients.shopperOrders.getOrder({...}); +} +``` + +## Parallel vs Sequential + +```typescript +// ✅ GOOD - Parallel requests +export function loader({ context }: LoaderFunctionArgs) { + const clients = createApiClients(context); + + return { + product: clients.shopperProducts.getProduct({...}), + reviews: clients.shopperProducts.getReviews({...}), + recommendations: clients.shopperProducts.getRecommendations({...}) + }; + // All three requests start simultaneously +} + +// ❌ BAD - Sequential requests +export async function loader({ context }: LoaderFunctionArgs) { + const clients = createApiClients(context); + + const product = await clients.shopperProducts.getProduct({...}); + const reviews = await clients.shopperProducts.getReviews({...}); + const recommendations = await clients.shopperProducts.getRecommendations({...}); + + return { product, reviews, recommendations }; + // Each request waits for the previous to complete +} +``` + +## Understanding Data Flow + +### Initial Page Load (SSR) + +```text +Browser → MRT Server + ↓ + loader() runs on server + ↓ + SCAPI requests on MRT + ↓ + HTML response → Browser +``` + +**Key characteristics:** +- The `loader()` runs on the server +- SCAPI requests happen server-side (direct in production, proxied in dev) +- Full HTML is returned to browser +- Client hydrates the HTML + +### Subsequent Navigation (SPA) + +All routes use server `loader` for both SSR and SPA navigation: + +```text +User clicks link → React Router intercepts + ↓ + Browser makes fetch() to server + ↓ + MRT Server receives request + ↓ + Same loader() runs on server + ↓ + SCAPI requests on MRT + ↓ + JSON response → Browser + ↓ + React updates DOM +``` + +**Key Point:** The loader function code is identical for both SSR and SPA navigation. The only difference is the response format (HTML vs JSON). This is why SCAPI credentials stay secure and MRT orchestration works consistently. + +**Reference:** See README-DATA.md for complete data fetching documentation. diff --git a/packages/b2c-dx-mcp/src/tools/storefrontnext/content/extensions.md b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/extensions.md new file mode 100644 index 00000000..f656d31c --- /dev/null +++ b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/extensions.md @@ -0,0 +1,80 @@ +# Extension Development + +## Structure + +``` +src/extensions/my-extension/ +├── plugin-config.json # Plugin configuration +├── components/ # Extension components +├── routes/ # Extension routes +├── locales/ # Extension translations +└── providers/ # Extension providers +``` + +## Plugin Configuration + +**Insert component into plugin point**: + +```json +{ + "components": [ + { + "pluginId": "header.before.cart", + "path": "extensions/my-extension/components/badge.tsx", + "order": 0 + } + ], + "contextProviders": [ + { + "path": "extensions/my-extension/providers/my-provider.tsx", + "order": 0 + } + ] +} +``` + +## Extension Routes + +Files in `routes/` auto-register: + +```typescript +// src/extensions/my-extension/routes/my-route.tsx +export function loader() { + return { message: 'Hello' }; +} + +export default function MyRoute() { + const { message } = useLoaderData(); + return
{message}
; +} +``` + +## Extension Translations + +Auto-namespaced as `extPascalCase`: + +``` +src/extensions/my-extension/locales/ +├── en-US/translations.json +└── it-IT/translations.json +``` + +```typescript +const {t} = useTranslation('extMyExtension'); +t('welcome'); +``` + +## Integration Markers + +```typescript +// Single line +/** @sfdc-extension-line SFDC_EXT_MY_FEATURE */ +import myFeature from '@extensions/my-feature'; + +// Block +{/* @sfdc-extension-block-start SFDC_EXT_MY_FEATURE */} +My Feature +{/* @sfdc-extension-block-end SFDC_EXT_MY_FEATURE */} +``` + +For full documentation, read: src/extensions/README.md diff --git a/packages/b2c-dx-mcp/src/tools/storefrontnext/content/i18n.md b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/i18n.md new file mode 100644 index 00000000..ec0f4253 --- /dev/null +++ b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/i18n.md @@ -0,0 +1,121 @@ +# Internationalization (i18n) + +## Overview + +- **Server instance**: Has access to all translations for all languages +- **Client instance**: Dynamically imports translations as JavaScript chunks +- **Dual API**: `useTranslation()` for components, `getTranslation()` for everything else + +## Adding Translations + +**In `src/locales/{language}/translations.json`**: + +```json +{ + "product": { + "title": "Product Details", + "addToCart": "Add to Cart", + "greeting": "Hello, {{name}}!", + "itemCount": { + "zero": "No items", + "one": "{{count}} item", + "other": "{{count}} items" + } + } +} +``` + +## Usage + +**2. Use in React components:** + +```typescript +import { useTranslation } from 'react-i18next'; + +export function ProductCard() { + const { t } = useTranslation('product'); + + return ( +
+

{t('title')}

+ +

{t('greeting', { name: 'John' })}

+

{t('itemCount', { count: 5 })}

+
+ ); +} +``` + +**3. Use in non-component code:** + +```typescript +import { getTranslation } from '@/lib/i18next'; + +// Client-side or utilities +const { t } = getTranslation(); +const message = t('product:addToCart'); + +// Server-side (loaders/actions) +export function loader(args: LoaderFunctionArgs) { + const { t } = getTranslation(args.context); + return { title: t('product:title') }; +} +``` + +## Validation Schemas with Translations + +**CRITICAL**: Use factory pattern for Zod schemas to avoid race conditions: + +```typescript +// ❌ WRONG - Module-level schema (race condition) +export const schema = z.object({ + email: z.string().email(t('validation:emailInvalid')) +}); + +// ✅ CORRECT - Factory function +import type { TFunction } from 'i18next'; + +export const createSchema = (t: TFunction) => { + return z.object({ + email: z.string().email(t('validation:emailInvalid')) + }); +}; + +// Usage in component +import { useMemo } from 'react'; +import { useTranslation } from 'react-i18next'; + +function MyForm() { + const { t } = useTranslation(); + const schema = useMemo(() => createSchema(t), [t]); + + const form = useForm({ resolver: zodResolver(schema) }); +} +``` + +## Language Switching + +```typescript +import LocaleSwitcher from '@/components/locale-switcher'; + +export function Footer() { + return
; +} +``` + +## Extension Translations + +Extensions use `extPascalCase` namespace: + +``` +src/extensions/my-extension/locales/ +├── en-US/translations.json +└── it-IT/translations.json +``` + +```typescript +const {t} = useTranslation('extMyExtension'); +t('welcome'); +``` + +**Reference:** See README-I18N.md for complete internationalization documentation. diff --git a/packages/b2c-dx-mcp/src/tools/storefrontnext/content/page-designer.md b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/page-designer.md new file mode 100644 index 00000000..022f523a --- /dev/null +++ b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/page-designer.md @@ -0,0 +1,86 @@ +# Page Designer Integration + +## Overview + +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 ``. + +## Concepts + +| Concept | Role | +|--------|------| +| **Page** | Fetched in route loaders via `fetchPageFromLoader(args, { pageId })`. | +| **Region** | Named area on a page; rendered with ``. | +| **Component** | Content block (hero, grid, carousel, etc.) with a `typeId` and attributes; registered in `@/lib/registry`. | +| **Registry** | Static registry in `@/lib/static-registry.ts` is **auto-generated** by the staticRegistry Vite plugin — do not edit by hand. | + +## Which pages use Page Designer + +Only **content pages** that merchants edit in Business Manager use Page Designer; cart, checkout, account, and auth do not. + +| Uses Page Designer | Does not | +|--------------------|----------| +| Home (`pageId: 'homepage'`), Category/PLP (`plp`), Search (`search`), Product/PDP (`pdp`) | Cart, Checkout, Account, Order confirmation, Auth | + +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 `` for each region. + +## Getting started + +### Route (new Page Designer page) + +- **In the loader**: call `fetchPageFromLoader(args, { pageId: '...' })` and `collectComponentDataPromises(args, pagePromise)`; return `page` and `componentData` (keep loaders synchronous; return promises for streaming). +- **In the layout**: for each region, render `` with optional `fallbackElement` and `errorElement` for Suspense/error boundaries. +- **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`). + +### Component (new Page Designer component) + +- **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). +- **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. +- **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`. + +### After changes + +- **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. + +### Design mode + +- **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. + + +## MCP tools (recommended) + +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. + +### 1. `storefront_next_design_decorator` (STOREFRONTNEXT toolset) + +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). + +- **Auto mode** (fast): Ask in your IDE: *"Add Page Designer support to [ComponentName] with autoMode"*. The tool runs in one turn with no prompts. +- **Interactive mode** (control): Ask *"Add Page Designer support to [ComponentName]"* and answer questions about typeId, which props to expose, types, and optional nested regions. + +### 2. `storefront_next_generate_page_designer_metadata` (STOREFRONTNEXT toolset) + +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. + +- **Full scan**: Run with no file list to process the whole project. +- **Incremental**: Pass specific file paths to regenerate only those components. +- **Dry run**: Use `dryRun: true` to see what would be generated without writing files. + +### 3. `cartridge_deploy` (CARTRIDGES toolset) + +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. + +### Typical workflow + +1. **`storefront_next_design_decorator`** — Add decorators to the component (use autoMode for a quick first pass). +2. **`storefront_next_generate_page_designer_metadata`** — Generate metadata JSON so the component and regions appear in Page Designer. +3. **`cartridge_deploy`** — Deploy to Commerce Cloud so merchants can use the component in Business Manager. + +## Best Practices + +1. **Keep loaders synchronous**: Return promises for Page Designer pages to enable streaming +2. **Use registry for components**: Register all Page Designer components with proper `typeId` +3. **Handle design mode**: Adapt UI when `pageDesignerMode` is `'EDIT'` or `'PREVIEW'` +4. **Rebuild after registry changes**: Static registry is generated at build time +5. **Use MCP tools**: Leverage `storefront_next_design_decorator` and `storefront_next_generate_page_designer_metadata` for faster development + +**Reference:** See README.md for complete Page Designer documentation and MCP tool setup. diff --git a/packages/b2c-dx-mcp/src/tools/storefrontnext/content/performance.md b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/performance.md new file mode 100644 index 00000000..bc5e378d --- /dev/null +++ b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/performance.md @@ -0,0 +1,80 @@ +# Performance Optimization + +## Bundle Size Limits + +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. + +**Check bundle size:** + +```bash +pnpm bundlesize:test # Verify limits +pnpm bundlesize:analyze # Analyze composition +``` + +## Built-in Metrics + +Enable in `config.server.ts`: + +```typescript +{ + performance: { + metrics: { + serverPerformanceMetricsEnabled: true, + clientPerformanceMetricsEnabled: true, + serverTimingHeaderEnabled: false // Debug only + } + } +} +``` + +Tracks: + +- SSR operations and rendering time +- SCAPI API calls with parallelization +- Authentication operations +- Client-side navigations + +## Best Practices + +### 1. Parallel Data Fetching + +**Key principle:** Return all promises simultaneously in loaders to enable parallel requests. Avoid sequential `await` calls. + +**Reference:** See `data-fetching` section for detailed parallel vs sequential patterns and code examples. + +### 2. Image Optimization + +Use the `DynamicImage` component with WebP format: + +```typescript +import { DynamicImage } from '@/components/dynamic-image'; + + +``` + +### 3. Progressive Streaming + +**Key principle:** Use synchronous loaders returning promises to enable progressive streaming. Await only critical data, stream the rest. + +**Reference:** See `data-fetching` section for detailed streaming patterns including mixed strategies (awaited + streamed). + +### 4. Lighthouse Audits + +Monitor performance metrics: + +- Preload critical CSS +- Use WebP images by default +- Lazy load below-the-fold content +- Optimize font loading + +```bash +pnpm lighthouse:ci # Run Lighthouse CI +``` + +**Reference:** See README-PERFORMANCE.md for complete performance optimization documentation. diff --git a/packages/b2c-dx-mcp/src/tools/storefrontnext/content/pitfalls.md b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/pitfalls.md new file mode 100644 index 00000000..916a45be --- /dev/null +++ b/packages/b2c-dx-mcp/src/tools/storefrontnext/content/pitfalls.md @@ -0,0 +1,141 @@ +# Common Pitfalls + +## 1. Using Client Loaders/Actions + +```typescript +// ❌ NEVER USE - Client loaders are not permitted +export function clientLoader() { ... } + +// ❌ NEVER USE - Client actions are not permitted +export function clientAction() { ... } + +// ✅ REQUIRED - Server-only data loading +export function loader({ context }: LoaderFunctionArgs) { + const clients = createApiClients(context); + return { product: clients.shopperProducts.getProduct({...}) }; +} + +// ✅ REQUIRED - Server-only actions +export async function action({ request, context }: ActionFunctionArgs) { + const clients = createApiClients(context); + // Handle mutation on server +} +``` + +**Decision tree:** + +```text +Need data for page render? +└─ Use server `loader` + +Need to handle mutations (form submissions, cart updates)? +└─ Use server `action` + +Need on-demand fetching after page load? +└─ Use `useScapiFetcher` (search, modals, infinite scroll) +``` + +**Key Point:** ALL SCAPI requests happen on the server: +- `loader`: Runs on server, SCAPI direct (prod) or proxied (dev) +- `action`: Runs on server, handles mutations securely +- `useScapiFetcher`: Triggers server route that calls SCAPI + +## 2. Module-Level i18n in Schemas + +```typescript +// ❌ RACE CONDITION +const schema = z.object({ + email: z.string().email(t('error')), +}); + +// ✅ FACTORY PATTERN +export const createSchema = (t: TFunction) => { + return z.object({ + email: z.string().email(t('error')), + }); +}; +``` + +## 3. Using Async Loaders (Blocks Page Transitions) + +```typescript +// ❌ BLOCKS PAGE TRANSITIONS - Async loader with await +export async function loader({ context }: LoaderFunctionArgs) { + const product = await fetchProduct(); // Blocks! + const reviews = await fetchReviews(); // Blocks! + return { product, reviews }; +} + +// ✅ NON-BLOCKING - Synchronous loader returning promises +export function loader({ context }: LoaderFunctionArgs): PageData { + return { + product: fetchProduct(), // Streams progressively + reviews: fetchReviews(), // Streams progressively + }; +} +``` + +**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. + +**Reference:** See `data-fetching` section for comprehensive loader patterns including mixed strategies (awaited + streamed). + +## 4. Modifying shadcn/ui + +```typescript +// ❌ NEVER modify src/components/ui/ + +// ✅ Create wrapper +import { Button } from '@/components/ui/button'; +export function MyButton(props) { + return + + +// shadcn/ui: Add via npx shadcn@latest add +import { Button } from '@/components/ui/button'; +import { Card } from '@/components/ui/card'; +``` + +**See `styling` section for:** Tailwind CSS 4 rules, Shadcn/ui components, dark mode, responsive design + --- ## 🔍 Get Detailed Guidelines @@ -171,6 +207,7 @@ Use the `storefront_next_development_guidelines` MCP tool with specific sections **Available sections:** - `data-fetching` - Loaders, actions, useScapiFetcher, data flow - `components` - createPage HOC, Suspense, file organization +- `styling` - Tailwind CSS 4, Shadcn/ui, styling guidelines - `testing` - Vitest, Storybook, coverage requirements - `auth` - Authentication and session management - `config` - Configuration system diff --git a/packages/b2c-dx-mcp/content/styling.md b/packages/b2c-dx-mcp/content/styling.md new file mode 100644 index 00000000..2e8c06ed --- /dev/null +++ b/packages/b2c-dx-mcp/content/styling.md @@ -0,0 +1,51 @@ +# Styling Guidelines + +## Rules + +- ✅ **Use Tailwind utility classes** in component JSX +- ✅ **Use `cn()` utility** for conditional classes (`import { cn } from '@/lib/utils'`) +- ✅ **Follow mobile-first** responsive patterns (`md:`, `lg:` breakpoints) +- ❌ **NO inline styles** (`style={{...}}`) +- ❌ **NO CSS modules** (`.module.css` files) +- ❌ **NO separate CSS files** for component styles +- ✅ **Custom CSS** only in `src/app.css` for global styles and theme configuration + +## Shadcn/ui Components + +**Adding components:** + +```bash +npx shadcn@latest add +``` + +**Rules:** + +- ✅ **DO** customize components by editing files in `src/components/ui/` +- ❌ **DON'T** create custom components inside `src/components/ui/` +- ❌ **DON'T** manually copy components (use CLI instead) + +## Dark Mode + +Dark mode is supported via CSS variables and the `.dark` class. Theme variables automatically adapt: + +```typescript +
+ +
+``` + +## Responsive Design + +Follow mobile-first responsive design: + +```typescript +
+ {/* Content */} +
+``` + +--- + +**Reference:** See [README-UI-STYLING.md](README-UI-STYLING.md) for complete UI and styling documentation. diff --git a/packages/b2c-dx-mcp/src/tools/storefrontnext/README.md b/packages/b2c-dx-mcp/src/tools/storefrontnext/README.md index ff561758..710a59bd 100644 --- a/packages/b2c-dx-mcp/src/tools/storefrontnext/README.md +++ b/packages/b2c-dx-mcp/src/tools/storefrontnext/README.md @@ -33,6 +33,7 @@ MCP tools for Storefront Next development with React Server Components. - `config` - Configuration system - `i18n` - Internationalization patterns - `components` - Component best practices + - `styling` - Tailwind CSS 4, Shadcn/ui, styling guidelines - `page-designer` - Page Designer integration and component registry - `performance` - Optimization techniques - `testing` - Testing strategy @@ -74,7 +75,7 @@ MCP tools for Storefront Next development with React Server Components. { "name": "storefront_next_development_guidelines", "arguments": { - "sections": ["quick-reference", "data-fetching", "state-management", "auth", "config", "i18n", "components", "page-designer", "performance", "testing", "extensions", "pitfalls"] + "sections": ["quick-reference", "data-fetching", "state-management", "auth", "config", "i18n", "components", "styling", "page-designer", "performance", "testing", "extensions", "pitfalls"] } } ``` diff --git a/packages/b2c-dx-mcp/src/tools/storefrontnext/developer-guidelines.ts b/packages/b2c-dx-mcp/src/tools/storefrontnext/developer-guidelines.ts index ff48a6a8..28381387 100644 --- a/packages/b2c-dx-mcp/src/tools/storefrontnext/developer-guidelines.ts +++ b/packages/b2c-dx-mcp/src/tools/storefrontnext/developer-guidelines.ts @@ -44,6 +44,7 @@ const SECTIONS_METADATA = [ {key: 'config', description: 'configuration'}, {key: 'i18n', description: 'i18n patterns and internationalization'}, {key: 'components', description: 'component best practices'}, + {key: 'styling', description: 'Tailwind CSS 4, Shadcn/ui, styling guidelines'}, {key: 'page-designer', description: 'Page Designer integration'}, {key: 'performance', description: 'performance optimization'}, {key: 'testing', description: 'testing strategies'},