Sfnext page designer

From SalesforceCommerceCloud/b2c-developer-tooling · View interactive version

Sfnext page designer is an AI agent skill from the GitHub repository SalesforceCommerceCloud/b2c-developer-tooling. Build merchant-editable Page Designer content in a Storefront Next project: components decorated with @Component/@AttributeDefinition/@RegionDefinition, page routes decorated with @PageType, Region component rendering, fetchPageWithComponentData, component loaders and fallbacks, the generated static

How to use this skill

Skill file

Page Designer in Storefront Next

In Storefront Next your React components are the Page Designer components. You annotate them with decorators, the build generates Business Manager metadata (JSON) from those decorators, and at runtime a route loader fetches the page structure from the Shopper Experience API and <Region> renders it.

The in-project guide is docs/README-PAGE-DESIGNER.md and the rules in AGENTS.md apply; this skill is the task-oriented path through them. Where the guide and the source disagree, trust the source.

Pieces and where they live

| Piece | Location in your project |

|-------|--------------------------|

| Decorators (Component, AttributeDefinition, RegionDefinition, PageType) | @/lib/decorators (src/lib/decorators/) |

| Page fetch with per-component loader promises | fetchPageWithComponentData in @/lib/page-designer/page-loader.server |

| Single-component fetch (preview, embedded) | fetchComponentWithComponentData in @/lib/page-designer/component-loader.server |

| Region renderer | Region from @/components/region |

| Generated registry (do not hand-edit) | src/lib/page-designer/static-registry.ts |

| Runtime hooks (usePageDesignerMode) | @salesforce/storefront-next-runtime/design/react/core |

| Design/preview mode detection in loaders | isDesignModeActive / isPreviewModeActive from @salesforce/storefront-next-runtime/design/mode |

| Generated Business Manager metadata | cartridges/app_storefrontnext_base/cartridge/experience/{components,pages,aspects} |

The runtime package's /design entry exports only the registry. Decorators come from your project, not from @salesforce/storefront-next-runtime.

Workflow

  1. Create or edit the component (metadata class, props, default export, fallback, optional loader) under src/components/<name>/index.tsx.
  2. Run pnpm dev (or pnpm build). The Vite plugin scans src/components for @Component and rewrites the registry between the STATIC_REGISTRY_START/END markers.
  3. pnpm cartridge:generate writes the metadata JSON (pnpm build runs it for you).
  4. pnpm cartridge:validate checks the generated JSON against the schemas. Generation also fails on an invalid attribute config (for example a bad searching combination).
  5. pnpm cartridge:deploy uploads the cartridge to the B2C instance (pnpm cartridge:deploy -- --delete removes old cartridge files first). The optional MCP tool cartridge_deploy does the same. See b2c-cli:b2c-code for credentials and code-version handling.
  6. In Business Manager, merchants build pages from the new palette entries. See Business Manager.

A component

// src/components/promo-banner/index.tsx
import { AttributeDefinition, Component, RegionDefinition } from '@/lib/decorators';
import { DynamicImage } from '@/components/dynamic-image';
import { type Image } from '@/types';
import { cn } from '@/lib/utils';

@Component('promoBanner', {
    name: 'Promo Banner',
    description: 'Headline and optional image. Headline text and alignment are editable.',
    group: 'Content',
})
@RegionDefinition([])
export class PromoBannerMetadata {
    @AttributeDefinition({ id: 'headline', name: 'Headline', type: 'string', required: true, defaultValue: 'Spring sale' })
    headline?: string;

    @AttributeDefinition({ id: 'image', name: 'Image', type: 'image' })
    image?: Image;

    @AttributeDefinition({ id: 'align', name: 'Alignment', type: 'enum', values: ['left', 'center'], defaultValue: 'left' })
    align?: 'left' | 'center';
}

interface PromoBannerProps {
    headline?: string;
    image?: Image;
    align?: 'left' | 'center';
    className?: string;
}

// Page Designer also injects component, data, designMetadata and regionId.
// Never spread them onto a DOM element; destructure them out first if you forward ...rest.
export default function PromoBanner({ headline = 'Spring sale', image, align = 'left', className }: PromoBannerProps) {
    return (
        <section className={cn('relative', align === 'center' && 'text-center', className)}>
            {image?.url && <DynamicImage src={image.url} alt="" />}
            <h2>{headline}</h2>
        </section>
    );
}

// REQUIRED. Rendered in a Suspense boundary; receives the same attribute props. Keep it light.
export function fallback() {
    return <div className="h-48 animate-pulse bg-muted" />;
}

Rules that matter:

A loader

Export a callable named loader when the component needs data (products, categories, your own API). It receives { componentData, context, request }; componentData is the whole SCAPI component object, so merchant-set attributes live at componentData.data.

// src/components/promo-products/loaders.ts
import type { LoaderFunctionArgs } from 'react-router';
import type { ShopperExperience } from '@/scapi';
import { fetchProductsByIds } from '@/lib/api/products.server';

export const loader = async (args: { componentData: unknown; context: LoaderFunctionArgs['context'] }) => {
    const comp = args.componentData as ShopperExperience.schemas['Component'];
    const { productIds } = (comp.data ?? {}) as { productIds?: string };
    if (!productIds) return null;
    return fetchProductsByIds(args.context, productIds.split(','));
};
// src/components/promo-products/index.tsx
export { loader } from './loaders';
export function fallback() { /* skeleton with reserved height */ }
export default function PromoProducts({ data }: { data?: Product[] | null }) { /* ... */ }

More on the registry shape, group-qualified ids, preload manifest and entry wiring: Registry and Loading.

A page route

Routes bind a URL to a Page Designer page template and render its regions. Routes with @PageType today: home (_app._index.tsx), PLP (_app.c.$.tsx), PDP (_app.p.$.tsx), search (_app.search.tsx), about-us (_app.about-us.tsx), and the component preview route. Cart, checkout, account and auth do not use Page Designer.

import { Region } from '@/components/region';
import { PageType } from '@/lib/decorators/page-type';
import { RegionDefinition } from '@/lib/decorators/region-definition';
import { fetchPageWithComponentData } from '@/lib/page-designer/page-loader.server';

@PageType({
    name: 'Landing Page',
    description: 'Campaign landing page with a banner and a main content area',
    supportedAspectTypes: [],
})
@RegionDefinition([
    { id: 'banner', name: 'Banner Region', maxComponents: 1 },
    { id: 'main', name: 'Main Region' },
])
export class LandingPageMetadata {}

export function loader(args: Route.LoaderArgs) {
    return { page: fetchPageWithComponentData(args, { pageId: 'landing' }) };
}

export default function Landing({ loaderData }: Route.ComponentProps) {
    return (
        <>
            <Region page={loaderData.page} regionId="banner" />
            <Region page={loaderData.page} regionId="main" />
        </>
    );
}

Design and preview mode

Business Manager loads your storefront in an iframe. In loaders use isDesignModeActive(request) / isPreviewModeActive(request) (the page loader already does and switches to the pageId/pdToken passed by Business Manager). In components use usePageDesignerMode() from @salesforce/storefront-next-runtime/design/react/core. PageDesignerInit (src/page-designer-init.tsx, rendered by root.tsx) blocks link navigation while editing and loads design-mode styles; do not remove it.

Verify before you finish

Reference Documentation

Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.