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
33 changes: 23 additions & 10 deletions samples/fx-integration/src/app/en/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { getVariation } from '@/lib/fx';
import { GraphClient } from '@optimizely/cms-sdk';
import { OptimizelyComponent } from '@optimizely/cms-sdk/react/server';
import { notFound } from 'next/navigation';

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

function returnFirst(content: any[]) {
if (content.length === 0) {
notFound();
}

return content[0];
}

export default async function Page({ params }: Props) {
const { slug } = await params;
const path = `/en/${slug.join('/')}/`;
Expand All @@ -20,22 +29,26 @@ export default async function Page({ params }: Props) {
if (!variation) {
console.log('Showing original');

const content = await client.getContentByPath(path);
const content = await client.getContentByPath(path).then(returnFirst);

return <OptimizelyComponent opti={content[0]} />;
return <OptimizelyComponent opti={content} />;
}

const content = await client
.getContentByPath(path, {
variation: { include: 'SOME', value: [variation] },
})
.catch(
// If fetching variations result in an error,
// we try to fetch the original content
() => client.getContentByPath(path)
);

console.log('Showing variation', variation);
.then((content) => {
// If no variations are found, try to fetch the original
if (content.length === 0) {
console.log('Variation not found. Fetching original');
return client.getContentByPath(path);
}

console.log('Showing variation', variation);
return content;
})
.then(returnFirst);

return <OptimizelyComponent opti={content[0]} />;
return <OptimizelyComponent opti={content} />;
}
5 changes: 5 additions & 0 deletions samples/hello-world/src/app/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GraphClient } from '@optimizely/cms-sdk';
import { OptimizelyComponent } from '@optimizely/cms-sdk/react/server';
import { notFound } from 'next/navigation';
import React from 'react';

type Props = {
Expand All @@ -16,5 +17,9 @@ export default async function Page({ params }: Props) {
});
const content = await client.getContentByPath(`/${slug.join('/')}/`);

if (content.length === 0) {
notFound();
}

return <OptimizelyComponent opti={content[0]} />;
}