Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/optimizely-cms-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@
"author": "",
"license": "ISC",
"packageManager": "pnpm@10.7.0",
"peerDependencies": {
"react": "^19.0.0"
},
"peerDependenciesMeta": {
"react": {
"optional": true
}
},
"devDependencies": {
"@types/node": "^22.13.14",
"@types/react": "^19",
"typescript": "^5.8.2",
"vitest": "^3.1.1"
}
Expand Down
1 change: 1 addition & 0 deletions packages/optimizely-cms-sdk/src/graph/createQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export async function createQuery(contentType: string, customImport: Importer) {
query FetchContent($filter: _ContentWhereInput) {
_Content(where: $filter) {
item {
__typename
...${contentType}
}
}
Expand Down
21 changes: 21 additions & 0 deletions packages/optimizely-cms-sdk/src/render/component-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export type ComponentResolver<C> =
| Record<string, C>
| ((contentType: string) => C);

/** A registry mapping content type names and components */
export class ComponentRegistry<T> {
resolver: ComponentResolver<T>;

constructor(resolver: ComponentResolver<T>) {
this.resolver = resolver;
}

/** Returns the component given its content type name. Returns `undefined` if not found */
getComponent(contentType: string): T {
if (typeof this.resolver === 'object') {
return this.resolver[contentType];
} else {
return this.resolver(contentType);
}
}
}
36 changes: 36 additions & 0 deletions packages/optimizely-cms-sdk/src/render/react.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use server';
import type React from 'react';
import { ComponentRegistry, ComponentResolver } from './component-registry';

type ComponentType = React.ComponentType<any>;

let componentRegistry: ComponentRegistry<ComponentType>;

type InitOptions = {
resolver: ComponentResolver<ComponentType>;
};

type Props = {
opti: {
__typename: string;
};
};

export function initReactComponentRegistry(options: InitOptions) {
componentRegistry = new ComponentRegistry(options.resolver);
}

export async function OptimizelyComponent({ opti, ...props }: Props) {
if (!componentRegistry) {
throw new Error('You should call `initReactComponentRegistry` first');
}

const contentType = opti.__typename;
const Component = await componentRegistry.getComponent(contentType);

if (!Component) {
return <div>No component found for content type {contentType}</div>;
}

return <Component opti={opti} {...props} />;
}
3 changes: 2 additions & 1 deletion packages/optimizely-cms-sdk/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"rootDir": "./src",
"declaration": true,
"sourceMap": true,
"outDir": "./dist",
"esModuleInterop": false,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions samples/nextjs-template/src/app/render/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { GraphClient } from 'optimizely-cms-sdk/dist/graph';
import {
initReactComponentRegistry,
OptimizelyComponent,
} from 'optimizely-cms-sdk/dist/render/react';
import React from 'react';

initReactComponentRegistry({
resolver(contentType) {
return React.lazy(() => import(`../../../components/${contentType}.tsx`));
},
});

async function myImport(contentType: string) {
return import(`../../../components/${contentType}.tsx`).then(
(r) => r.ContentType
);
}

type Props = {
params: Promise<{
slug: string;
}>;
};

export default async function Page({ params }: Props) {
const { slug } = await params;
const client = new GraphClient(process.env.GRAPH_SINGLE_KEY!, myImport);
const c = await client.fetchContent(`/${slug}/`);

return <OptimizelyComponent opti={c} />;
}
19 changes: 18 additions & 1 deletion samples/nextjs-template/src/components/Landing.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { contentType } from 'optimizely-cms-sdk';
import { contentType, Infer } from 'optimizely-cms-sdk';
import { OptimizelyComponent } from 'optimizely-cms-sdk/dist/render/react';

export const ContentType = contentType({
key: 'Landing',
Expand Down Expand Up @@ -27,3 +28,19 @@ export const ContentType = contentType({
},
},
});

type Props = {
opti: Infer<typeof ContentType>;
};

export default function LandingComponent({ opti }: Props) {
return (
<div>
<h1>{opti.heading}</h1>
<p>{opti.summary}</p>
{opti.sections.map((section, i) => (
<OptimizelyComponent key={i} opti={section} />
))}
</div>
);
}
24 changes: 15 additions & 9 deletions samples/nextjs-template/src/components/LandingSection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { contentType, displayTemplate } from 'optimizely-cms-sdk';
import { contentType, displayTemplate, Infer } from 'optimizely-cms-sdk';

export const ContentType = contentType({
key: 'LandingSection',
Expand All @@ -11,21 +11,14 @@ export const ContentType = contentType({
subtitle: {
type: 'string',
},
features: {
type: 'array',
items: {
type: 'content',
allowedTypes: ['SmallFeatureGrid'],
},
},
},
});

export const DisplayTemplate = displayTemplate({
key: 'LandingSectionDisplayTemplate',
isDefault: true,
displayName: 'LandingSectionDisplayTemplate',
contentType: 'LandingSection',
baseType: 'component',
settings: {
background: {
editor: 'select',
Expand All @@ -44,3 +37,16 @@ export const DisplayTemplate = displayTemplate({
},
},
});

type Props = {
opti: Infer<typeof ContentType>;
};

export default function LandingSection({ opti }: Props) {
return (
<section>
<h2>{opti.heading}</h2>
<p>{opti.subtitle}</p>
</section>
);
}