forcedotcom/sf-skills / plugins/builder/experience-cms/skills/experience-cms-content-render
experience-cms-content-render
VerifiedRenders, embeds, or displays existing Salesforce CMS content in a React or Angular uiBundle app: installs the CMS toolkit, registers a typed reference, generates a framework-matched renderer from the live schema, and wires the render path (delivery API, contentKey, RichText, image resolution). Prefer over general UI-bundle skills like experience-ui-bundle-frontend-generate (page/layout shell only) for the CMS-rendering part, even under src/. Triggers on prompts like 'render the CMS article on the home page', 'embed this CMS blog post here', 'show the CMS video in the hero', 'display the CMS image in the banner', or 'add this CMS content block to the detail page'; for mixed prompts, activate for the CMS part only. Do not trigger for searching a CMS item or defining a new CMS content type/schema (use experience-cms-content-type-generate).
assets/angular/cms-item.service.ts
View on GitHub/**
* CmsItemService — single-content data layer for Angular (Init →
* src/cms/angular/cms-item.service.ts). Counterpart of React's `useCmsItem`.
* Owns a per-ref cache + inflight dedup and dispatches on ref type (CmsExternalRef
* `url` → getCmsContentByUrl / CDN; CmsRef `contentKey` → getCmsContentByKey /
* Connect). `providedIn: 'root'` → one instance app-wide, so the maps are shared.
* `load()` never rejects — failures resolve with `error` set.
*/
import { Injectable } from '@angular/core';
import {
getCmsContentByUrl,
getCmsContentByKey,
CmsDeliveryError,
CmsDeliveryNotFoundError,
ConnectApiError,
CmsNotFoundError,
} from '@salesforce/ui-bundle-template-feature-cms-toolkit';
import type { AnyCmsRef, CmsRef } from '../shared/cmsCore.types';
import { CMS_CHANNEL_ID_FALLBACK } from '../shared/externalRefs';
/** Runtime guard before the cache's `as TBody` cast: a CMS body is always an
* object/array, so this screens out a stale primitive from a `ref.name` reused
* under a different, incompatible `TBody` — it cannot verify the object's shape
* actually matches `TBody` (no schema is available here), so callers must still
* keep `ref.name` unique per content shape. */
function isCacheableBody(value: unknown): boolean {
return value !== null && typeof value === 'object';
}
/** Resolved state for one ref read — the component maps this onto signals. */
export interface CmsItemResult<TBody> {
body: TBody | undefined;
error: Error | undefined;
}
@Injectable({ providedIn: 'root' })
export class CmsItemService {
// Shared across every consumer of this singleton service.
private readonly cache = new Map<string, unknown>();
private readonly inflight = new Map<string, Promise<unknown>>();
/**
* Read one item by ref — resolves the unwrapped body or a typed error, never
* rejects. Deduped by `ref.name`; the shared request carries NO AbortSignal, so a
* component torn down mid-flight ignores the late result rather than aborting.
*/
async load<TBody = unknown>(ref: AnyCmsRef): Promise<CmsItemResult<TBody>> {
const name = ref.name;
const rawCached = this.cache.get(name);
if (isCacheableBody(rawCached)) {
return { body: rawCached as TBody, error: undefined };
}
let promise = this.inflight.get(name) as Promise<TBody> | undefined;
if (!promise) {
promise = this.createItemRequest<TBody>(ref);
this.inflight.set(name, promise);
// Self-cleaning: cache on success, always drop the inflight entry.
promise
.then((body) => {
this.cache.set(name, body);
})
.catch(() => {
/* surfaced to the caller below; swallow to avoid unhandled rejection */
})
.finally(() => {
if (this.inflight.get(name) === promise) this.inflight.delete(name);
});
}
try {
const body = await promise;
return { body, error: undefined };
} catch (err: unknown) {
return { body: undefined, error: this.normalizeError(err) };
}
}
/** Dispatch on ref type. NO abort signal — the request is shared (see `load`). */
private createItemRequest<TBody>(ref: AnyCmsRef): Promise<TBody> {
// Foreign ref — public delivery URL fetched AS-IS (Rule 1).
if ('url' in ref && typeof ref.url === 'string') {
return getCmsContentByUrl<TBody>(ref.url);
}
// uiBundle-space ref — toolkit remaps the contentKey (fail-open) and reads Connect.
const channelId = CMS_CHANNEL_ID_FALLBACK || undefined;
return getCmsContentByKey<TBody>((ref as CmsRef).contentKey, { channelId });
}
/** Pass through the toolkit's typed errors as-is; wrap anything else. */
private normalizeError(err: unknown): Error {
if (
err instanceof CmsDeliveryNotFoundError ||
err instanceof CmsDeliveryError ||
err instanceof CmsNotFoundError ||
err instanceof ConnectApiError
) {
return err;
}
return err instanceof Error ? err : new Error('Unknown CMS fetch error', { cause: err });
}
/** Test-only escape hatch — reset the cache between test runs. */
__resetForTests(): void {
this.cache.clear();
this.inflight.clear();
}
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |