Skip to content

Commit ed623ff

Browse files
committed
CMS-47490 Refactor getPreviewUtils to improve URL handling
1 parent 42fd6fb commit ed623ff

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

packages/optimizely-cms-sdk/src/react/server.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,17 @@ export function getPreviewUtils(opti: OptimizelyComponentProps['opti']) {
343343
src(input: InferredContentReference | string | null | undefined): string {
344344
const previewToken = opti.__context?.preview_token;
345345

346-
// if input is a ContentReference
347-
if (typeof input === 'object' && previewToken && input?.item?.Url) {
348-
return appendToken(input?.item?.Url, previewToken);
346+
// if input is an object with a URL
347+
if (typeof input === 'object' && input) {
348+
// if dam asset is selected the default URL is in input.url.default will be null
349+
const url = input.url?.default ?? input.item?.Url;
350+
if (url) {
351+
return appendToken(url, previewToken);
352+
}
349353
}
350354

351355
// if input is a string URL
352-
if (typeof input === 'string' && previewToken) {
356+
if (typeof input === 'string') {
353357
return appendToken(input, previewToken);
354358
}
355359

packages/optimizely-cms-sdk/src/util/preview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @returns The URL with the preview token appended as a query parameter.
88
*/
99
export const appendToken = (url: string, previewToken?: string): string => {
10-
if (!previewToken) return url;
10+
if (!previewToken || previewToken.trim() === '') return url;
1111
const separator = url.includes('?') ? '&' : '?';
1212
return `${url}${separator}preview_token=${previewToken}`;
1313
};

samples/nextjs-template/src/app/globals.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ a:hover span.animate {
414414
.about-us .about-us-image {
415415
position: relative;
416416
width: 100%;
417-
padding-top: 56.25%;
418417
}
419418

420419
.about-us .about-us-image img {

0 commit comments

Comments
 (0)