diff --git a/__test__/test-website/src/app/multi-host/[...slug]/page.tsx b/__test__/test-website/src/app/multi-host/[...slug]/page.tsx new file mode 100644 index 00000000..e20afcda --- /dev/null +++ b/__test__/test-website/src/app/multi-host/[...slug]/page.tsx @@ -0,0 +1,27 @@ +import { GraphClient } from '@optimizely/cms-sdk'; +import { OptimizelyComponent } from '@optimizely/cms-sdk/react/server'; +import React from 'react'; + +type Props = { + params: Promise<{ + slug: string[]; + }>; + // Assume that search params are correct: + searchParams: Promise<{ variation?: string }>; +}; + +export default async function Page({ params }: Props) { + const { slug } = await params; + + const client = new GraphClient(process.env.OPTIMIZELY_GRAPH_SINGLE_KEY!, { + graphUrl: process.env.OPTIMIZELY_GRAPH_URL, + }); + const host = + process.env.NEXTJS_HOST ?? `https://localhost:${process.env.PORT ?? 3000}`; + + const content = await client.getContentByPath(`/${slug.join('/')}/`, { + host, + }); + + return ; +} diff --git a/__test__/test-website/src/app/multi-host/layout.tsx b/__test__/test-website/src/app/multi-host/layout.tsx new file mode 100644 index 00000000..a14e64fc --- /dev/null +++ b/__test__/test-website/src/app/multi-host/layout.tsx @@ -0,0 +1,16 @@ +export const metadata = { + title: 'Next.js', + description: 'Generated by Next.js', +} + +export default function RootLayout({ + children, +}: { + children: React.ReactNode +}) { + return ( + + {children} + + ) +} diff --git a/packages/optimizely-cms-sdk/src/graph/filters.ts b/packages/optimizely-cms-sdk/src/graph/filters.ts index 380f0f0b..6e85a274 100644 --- a/packages/optimizely-cms-sdk/src/graph/filters.ts +++ b/packages/optimizely-cms-sdk/src/graph/filters.ts @@ -12,7 +12,7 @@ * @param path - The URL path to filter by. * @returns A `GraphQueryArguments` object with a `where` clause that matches the given path. */ -export function pathFilter(path: string): ContentInput { +export function pathFilter(path: string, host?: string): ContentInput { return { where: { _metadata: { @@ -20,6 +20,7 @@ export function pathFilter(path: string): ContentInput { default: { eq: path, }, + base: host ? { eq: host } : undefined, }, }, }, diff --git a/packages/optimizely-cms-sdk/src/graph/index.ts b/packages/optimizely-cms-sdk/src/graph/index.ts index c7efac81..1b8e4ebe 100644 --- a/packages/optimizely-cms-sdk/src/graph/index.ts +++ b/packages/optimizely-cms-sdk/src/graph/index.ts @@ -31,6 +31,7 @@ export type PreviewParams = { export type GraphGetContentOptions = { variation?: GraphVariationInput; + host?: string; }; export { GraphVariationInput }; @@ -185,7 +186,7 @@ export class GraphClient { options?: GraphGetContentOptions ) { const input: GraphVariables = { - ...pathFilter(path), + ...pathFilter(path, options?.host), variation: options?.variation, }; const contentTypeName = await this.getContentType(input);