diff --git a/samples/fx-integration/src/app/en/[...slug]/page.tsx b/samples/fx-integration/src/app/en/[...slug]/page.tsx index 55c08a85..fb34eb22 100644 --- a/samples/fx-integration/src/app/en/[...slug]/page.tsx +++ b/samples/fx-integration/src/app/en/[...slug]/page.tsx @@ -1,6 +1,7 @@ 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<{ @@ -8,6 +9,14 @@ type Props = { }>; }; +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('/')}/`; @@ -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 ; + return ; } 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 ; + return ; } diff --git a/samples/hello-world/src/app/[...slug]/page.tsx b/samples/hello-world/src/app/[...slug]/page.tsx index e7b6bc00..80100d5f 100644 --- a/samples/hello-world/src/app/[...slug]/page.tsx +++ b/samples/hello-world/src/app/[...slug]/page.tsx @@ -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 = { @@ -16,5 +17,9 @@ export default async function Page({ params }: Props) { }); const content = await client.getContentByPath(`/${slug.join('/')}/`); + if (content.length === 0) { + notFound(); + } + return ; }