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
27 changes: 27 additions & 0 deletions __test__/test-website/src/app/multi-host/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -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 <OptimizelyComponent opti={content[0]} />;
}
16 changes: 16 additions & 0 deletions __test__/test-website/src/app/multi-host/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
}

export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
3 changes: 2 additions & 1 deletion packages/optimizely-cms-sdk/src/graph/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
* @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: {
url: {
default: {
eq: path,
},
base: host ? { eq: host } : undefined,
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion packages/optimizely-cms-sdk/src/graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type PreviewParams = {

export type GraphGetContentOptions = {
variation?: GraphVariationInput;
host?: string;
};

export { GraphVariationInput };
Expand Down Expand Up @@ -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);
Expand Down