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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Alternatively, if you want to include the package directly in your website HTML

```html
<script type='module' crossorigin="anonymous">
import ContentstackLivePreview from 'https://esm.sh/@contentstack/live-preview-utils@4.1.2';
import ContentstackLivePreview from 'https://esm.sh/@contentstack/live-preview-utils@4.3.0';

ContentstackLivePreview.init({
stackDetails: {
Expand Down Expand Up @@ -51,7 +51,7 @@ ContentstackLivePreview.init({

MIT License

Copyright © 2021-2025 [Contentstack](https://www.contentstack.com/). All Rights Reserved
Copyright © 2021-2026 [Contentstack](https://www.contentstack.com/). All Rights Reserved

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/live-preview-utils",
"version": "4.1.2",
"version": "4.3.0",
"description": "Contentstack provides the Live Preview SDK to establish a communication channel between the various Contentstack SDKs and your website, transmitting live changes to the preview pane.",
"type": "module",
"types": "dist/legacy/index.d.ts",
Expand Down
8 changes: 4 additions & 4 deletions src/configManager/__test__/configManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ describe("Config", () => {
});

describe("config default flags", () => {
test("enableLivePreviewOutsideIframe defaults to false in getDefaultConfig", () => {
test("enableLivePreviewOutsideIframe defaults to undefined in getDefaultConfig", () => {
const defaultConfig = getDefaultConfig();
expect(defaultConfig.enableLivePreviewOutsideIframe).toBe(false);
expect(defaultConfig.enableLivePreviewOutsideIframe).toBeUndefined();
});

test("enableLivePreviewOutsideIframe defaults to false in getUserInitData", () => {
test("enableLivePreviewOutsideIframe defaults to undefined in getUserInitData", () => {
const initData = getUserInitData();
expect(initData.enableLivePreviewOutsideIframe).toBe(false);
expect(initData.enableLivePreviewOutsideIframe).toBeUndefined();
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/configManager/__test__/handleUserConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,10 @@ describe("handleInitData() - enableLivePreviewOutsideIframe", () => {
Config.reset();
});

test("should default to false when not provided", () => {
test("should default to undefined when not provided", () => {
const initData: Partial<IInitData> = {};
handleInitData(initData);
expect(config.enableLivePreviewOutsideIframe).toBe(false);
expect(config.enableLivePreviewOutsideIframe).toBeUndefined();
});

test("should set to true when provided as true", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/configManager/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function getUserInitData(): IInitData {
environment: "",
},
runScriptsOnUpdate: false,
enableLivePreviewOutsideIframe: false,
enableLivePreviewOutsideIframe: undefined,
};
}

Expand Down Expand Up @@ -111,6 +111,6 @@ export function getDefaultConfig(): IConfig {
},
payload: [],
},
enableLivePreviewOutsideIframe: false,
enableLivePreviewOutsideIframe: undefined,
};
}
6 changes: 5 additions & 1 deletion src/configManager/handleUserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ export const handleInitData = (initData: Partial<IInitData>): void => {
"bottom-right",
})

Config.set("enableLivePreviewOutsideIframe", initData.enableLivePreviewOutsideIframe ?? config.enableLivePreviewOutsideIframe ?? false);
Config.set(
"enableLivePreviewOutsideIframe",
initData.enableLivePreviewOutsideIframe ??
config.enableLivePreviewOutsideIframe
);

// client URL params
handleClientUrlParams(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,23 @@ describe("postMessageEvent.hooks", () => {
});
});

it("should omit enableLivePreviewOutsideIframe in INIT payload when config is unset", async () => {
mockConfig = {
ssr: true,
mode: 1,
enableLivePreviewOutsideIframe: undefined,
};
(Config.get as any).mockReturnValue(mockConfig);

await sendInitializeLivePreviewPostMessageEvent();
await Promise.resolve();

const initPayload = (livePreviewPostMessage?.send as any).mock.calls.at(-1)?.[1];
expect(initPayload.config).not.toHaveProperty(
"enableLivePreviewOutsideIframe"
);
});

it("should include enableLivePreviewOutsideIframe=false in INIT payload", async () => {
mockConfig = {
ssr: true, // avoid timers
Expand Down
26 changes: 19 additions & 7 deletions src/livePreview/eventManager/postMessageEvent.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,29 @@ export function useOnEntryUpdatePostMessageEvent(): void {
}

export function sendInitializeLivePreviewPostMessageEvent(): void {
const config = Config.get();
const initConfig: {
shouldReload: boolean;
href: string;
sdkVersion: string | undefined;
mode: number;
enableLivePreviewOutsideIframe?: boolean;
} = {
shouldReload: config.ssr,
href: window.location.href,
sdkVersion: process?.env?.PACKAGE_VERSION,
mode: config.mode,
};

if (config.enableLivePreviewOutsideIframe !== undefined) {
initConfig.enableLivePreviewOutsideIframe = config.enableLivePreviewOutsideIframe;
}

livePreviewPostMessage
?.send<LivePreviewInitEventResponse>(
LIVE_PREVIEW_POST_MESSAGE_EVENTS.INIT,
{
config: {
shouldReload: Config.get().ssr,
href: window.location.href,
sdkVersion: process?.env?.PACKAGE_VERSION,
mode: Config.get().mode,
enableLivePreviewOutsideIframe: Config.get().enableLivePreviewOutsideIframe,
},
config: initConfig,
}
)
.then((data) => {
Expand Down
4 changes: 2 additions & 2 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export declare interface IConfig {
highlightedElement: HTMLElement | null;
};
collab: ICollabConfig["collab"];
enableLivePreviewOutsideIframe: boolean;
enableLivePreviewOutsideIframe: boolean | undefined;
}


Expand Down Expand Up @@ -133,7 +133,7 @@ export declare interface IInitData {
editButton: IConfigEditButton;
editInVisualBuilderButton: IConfigEditInVisualBuilderButton;
mode: ILivePreviewMode;
enableLivePreviewOutsideIframe: boolean; // default: false
enableLivePreviewOutsideIframe: boolean | undefined; // default: undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not this?

Suggested change
enableLivePreviewOutsideIframe: boolean | undefined; // default: undefined
enableLivePreviewOutsideIframe?: boolean;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially considered enableLivePreviewOutsideIframe?: boolean, but with exactOptionalPropertyTypes: true that type does not allow explicitly assigning undefined when the key is present in object literals.
In our defaults, we intentionally keep the key present as undefined so the config shape stays consistent and key validation via Config.set continues to work.

}

// type PickPartial<T, K extends keyof T> = Partial<Pick<T, K>> & Omit<T, K>;
Expand Down
Loading