-
Notifications
You must be signed in to change notification settings - Fork 1
Live preview new tab ssr issue #502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
52f91a4
1b89e20
b78db51
f3c6abc
e65d15c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,11 @@ | ||
| * @contentstack/security-admin | ||
| * @contentstack/ghost-pr-reviewers | ||
|
|
||
| .github/workflows/sca-scan.yml @contentstack/security-admin | ||
|
|
||
| .github/workflows/codeql-anaylsis.yml @contentstack/security-admin | ||
|
|
||
| **/.snyk @contentstack/security-admin | ||
|
|
||
| .github/workflows/policy-scan.yml @contentstack/security-admin | ||
|
|
||
| .github/workflows/issues-jira.yml @contentstack/security-admin | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -159,9 +159,9 @@ describe("postMessageEvent.hooks", () => { | |
| (Config.get as any).mockReturnValue(mockConfig); | ||
| }); | ||
|
|
||
| it("should reload window when ssr is true and no event_type", () => { | ||
| // Set URL to include live_preview parameter so reload path is taken | ||
| mockWindow.location.href = "https://example.com?live_preview=old-hash"; | ||
| it("should reload window when ssr is true and no event_type and all params present", () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @contentstackMridul Just want to know that whether this test was newly added by us? or the live preview used to work like this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @KANE-99, earlier the test was checking for the live preview hash only, but ideally we should check for all the params in the SSR case |
||
| // Set URL to include all required params so reload path is taken | ||
| mockWindow.location.href = "https://example.com?live_preview=old-hash&content_type_uid=blog&entry_uid=entry-123"; | ||
|
|
||
| const eventData: OnChangeLivePreviewPostMessageEventData = { | ||
| hash: "test-hash", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,7 +50,7 @@ export function useOnEntryUpdatePostMessageEvent(): void { | |
| LIVE_PREVIEW_POST_MESSAGE_EVENTS.ON_CHANGE, | ||
| (event) => { | ||
| try { | ||
| const { ssr, onChange } = Config.get(); | ||
| const { ssr, onChange, stackDetails } = Config.get(); | ||
| const event_type = event.data._metadata?.event_type; | ||
| setConfigFromParams({ | ||
| live_preview: event.data.hash, | ||
|
|
@@ -59,41 +59,57 @@ export function useOnEntryUpdatePostMessageEvent(): void { | |
| // This section will run when there is a change in the entry and the website is CSR | ||
| if (!ssr && !event_type) { | ||
| onChange(); | ||
| } | ||
| } | ||
|
|
||
| if(isOpeningInNewTab()) { | ||
| if(!window) { | ||
| if (isOpeningInNewTab()) { | ||
| if (!window) { | ||
| PublicLogger.error("window is not defined"); | ||
| return; | ||
| }; | ||
|
|
||
| // This section will run when there is a change in the entry and the website is SSR | ||
| if(ssr && !event_type) { | ||
| if(window.location.href.includes("live_preview")) { | ||
|
|
||
| if (ssr && !event_type) { | ||
| const url = new URL(window.location.href); | ||
| let live_preview = url.searchParams.get("live_preview"); | ||
| let content_type_uid = url.searchParams.get("content_type_uid"); | ||
| let entry_uid = url.searchParams.get("entry_uid"); | ||
|
|
||
| if (live_preview && content_type_uid && entry_uid) { | ||
| // All required params are present, just reload | ||
| window.location.reload(); | ||
| } else { | ||
| const url = new URL(window.location.href); | ||
| url.searchParams.set("live_preview", event.data.hash); | ||
| url.searchParams.set("content_type_uid", Config.get().stackDetails.contentTypeUid || ""); | ||
| url.searchParams.set("entry_uid", Config.get().stackDetails.entryUid || ""); | ||
| live_preview = event.data.hash; | ||
| content_type_uid = event.data.content_type_uid || stackDetails.$contentTypeUid?.toString() || ""; | ||
| entry_uid = event.data.entry_uid || stackDetails.$entryUid?.toString() || ""; | ||
| // Set missing params and redirect | ||
| url.searchParams.set("live_preview", live_preview); | ||
| if (content_type_uid) { | ||
| url.searchParams.set( | ||
| "content_type_uid", | ||
| content_type_uid | ||
| ); | ||
| } | ||
| if (entry_uid) { | ||
| url.searchParams.set( | ||
| "entry_uid", | ||
| entry_uid | ||
| ); | ||
| } | ||
| window.location.href = url.toString(); | ||
| } | ||
| } | ||
|
|
||
| // This section will run when the hash changes and the website is SSR or CSR | ||
| if(event_type === OnChangeLivePreviewPostMessageEventTypes.HASH_CHANGE){ | ||
| if (event_type === OnChangeLivePreviewPostMessageEventTypes.HASH_CHANGE) { | ||
| const newUrl = new URL(window.location.href); | ||
| newUrl.searchParams.set("live_preview", event.data.hash); | ||
| window.history.pushState({}, "", newUrl.toString()); | ||
| } | ||
|
|
||
| // This section will run when the URL of the page changes | ||
| if(event_type === OnChangeLivePreviewPostMessageEventTypes.URL_CHANGE && event.data.url){ | ||
| if (event_type === OnChangeLivePreviewPostMessageEventTypes.URL_CHANGE && event.data.url) { | ||
| window.location.href = event.data.url; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } catch (error) { | ||
| PublicLogger.error("Error handling live preview update:", error); | ||
| return; | ||
|
|
@@ -130,6 +146,8 @@ export function sendInitializeLivePreviewPostMessageEvent(): void { | |
|
|
||
| if (contentTypeUid && entryUid) { | ||
| // TODO: we should not use this function. Instead we should have sideEffect run automatically when we set the config. | ||
| console.log("setConfigFromParams", contentTypeUid, entryUid); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this console
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| // setConfigFromParams(`?content_type_uid=${contentTypeUid}&entry_uid=${entryUid}`); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this unwanted comment
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| setConfigFromParams({ | ||
| content_type_uid: contentTypeUid, | ||
| entry_uid: entryUid, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are this changes from main branch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes