Skip to content

Commit 5a360a4

Browse files
authored
Merge pull request #506 from contentstack/develop_v4
Develop v4
2 parents 14b1f37 + b365b99 commit 5a360a4

File tree

14 files changed

+249
-87
lines changed

14 files changed

+249
-87
lines changed

.github/workflows/secrets-scan.yml

Lines changed: 0 additions & 29 deletions
This file was deleted.

.talismanrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ fileignoreconfig:
1010
checksum: d0ef271ee5381d9feab06bda6e7e89bd0a882fee87495627bd811c1f0a5459c7
1111
- filename: package-lock.json
1212
checksum: fd06363871d0ee16ebfb5d9d0cc479e0922a615bb76584b80bb6933ee6c3e237
13+
- filename: src/visualBuilder/utils/__test__/handleFieldMouseDown.test.ts
14+
checksum: dc20802eab76834de7aadb797b14076f1f1a9c0662b32493563fe68fd5cd6e16
1315
- filename: CHANGELOG.md
1416
checksum: 873106e25dafe0355c55724936cfe0ecc9d0192a2a82c98eddaf5648f23d5ee7
1517
version: "1.0"

CODEOWNERS

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
* @contentstack/security-admin
1+
* @contentstack/ghost-pr-reviewers
2+
3+
.github/workflows/sca-scan.yml @contentstack/security-admin
4+
5+
.github/workflows/codeql-anaylsis.yml @contentstack/security-admin
6+
7+
**/.snyk @contentstack/security-admin
8+
9+
.github/workflows/policy-scan.yml @contentstack/security-admin
10+
11+
.github/workflows/issues-jira.yml @contentstack/security-admin

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ npm install @contentstack/live-preview-utils
1515
Alternatively, if you want to include the package directly in your website HTML code, use the following command:
1616

1717
```html
18-
<script type='module' integrity='sha384-b6G+ggU20rGxqCqsgaS6zludFgj5N11xsuXhMEIARMuQY2PtyDS04TU0H5goP+32' crossorigin="anonymous">
18+
<script type='module' crossorigin="anonymous">
1919
import ContentstackLivePreview from 'https://esm.sh/@contentstack/live-preview-utils@4.0.1';
2020
2121
ContentstackLivePreview.init({
@@ -51,7 +51,7 @@ ContentstackLivePreview.init({
5151

5252
MIT License
5353

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

5656
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:
5757

main.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ npm install @contentstack/live-preview-utils
1515
Alternatively, if you want to include the package directly in your website HTML code, use the following command:
1616

1717
```html
18-
<script type='module' integrity='{{integrity}}' crossorigin="anonymous">
18+
<script type='module' crossorigin="anonymous">
1919
import ContentstackLivePreview from 'https://esm.sh/@contentstack/live-preview-utils@{{packageVersion}}';
2020
2121
ContentstackLivePreview.init({
@@ -51,7 +51,7 @@ ContentstackLivePreview.init({
5151

5252
MIT License
5353

54-
Copyright © 2024 [Contentstack](https://www.contentstack.com/). All Rights Reserved
54+
Copyright © 2021-{{currentYear}} [Contentstack](https://www.contentstack.com/). All Rights Reserved
5555

5656
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:
5757

mustache.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
// index.js
22
// import { render } from 'mustache';
33
import * as mustache from 'mustache';
4-
import { readFile, readFileSync, writeFileSync } from 'fs';
4+
import { readFile, writeFileSync } from 'fs';
55
import packageJson from "./package.json" assert { type: "json" };
6-
import ssri from 'ssri';
76
const MUSTACHE_MAIN_DIR = './main.mustache';
8-
const fileContent = readFileSync('./dist/modern/index.js');
9-
const integrity = ssri.fromData(fileContent, { algorithms: ['sha384'] });
107
/**
118
* DATA is the object that contains all
129
* the data to be provided to Mustache
1310
* Notice the "name" and "date" property.
1411
*/
1512
const DATA = {
1613
packageVersion: packageJson.version,
17-
integrity: integrity.toString(),
14+
currentYear: new Date().getFullYear(),
1815
};
1916
function generateReadMe() {
2017

package-lock.json

Lines changed: 0 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
"@types/mustache": "^4.2.2",
5757
"@types/react": "^18.2.57",
5858
"@types/react-dom": "^18.2.19",
59-
"@types/ssri": "^7.1.5",
6059
"@types/uuid": "^8.3.1",
6160
"@vitest/coverage-v8": "^2.1.2",
6261
"@vitest/ui": "^2.1.2",
@@ -71,7 +70,6 @@
7170
"jsdom": "^25.0.0",
7271
"prettier": "^3.3.3",
7372
"prettier-eslint": "^15.0.1",
74-
"ssri": "^11.0.0",
7573
"ts-node": "^10.9.2",
7674
"tsc": "^2.0.4",
7775
"tsup": "^8.0.1",

src/livePreview/eventManager/__test__/postMessageEvent.hooks.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ describe("postMessageEvent.hooks", () => {
159159
(Config.get as any).mockReturnValue(mockConfig);
160160
});
161161

162-
it("should reload window when ssr is true and no event_type", () => {
163-
// Set URL to include live_preview parameter so reload path is taken
164-
mockWindow.location.href = "https://example.com?live_preview=old-hash";
162+
it("should reload window when ssr is true and no event_type and all params present", () => {
163+
// Set URL to include all required params so reload path is taken
164+
mockWindow.location.href = "https://example.com?live_preview=old-hash&content_type_uid=blog&entry_uid=entry-123";
165165

166166
const eventData: OnChangeLivePreviewPostMessageEventData = {
167167
hash: "test-hash",

src/livePreview/eventManager/postMessageEvent.hooks.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function useOnEntryUpdatePostMessageEvent(): void {
5050
LIVE_PREVIEW_POST_MESSAGE_EVENTS.ON_CHANGE,
5151
(event) => {
5252
try {
53-
const { ssr, onChange } = Config.get();
53+
const { ssr, onChange, stackDetails } = Config.get();
5454
const event_type = event.data._metadata?.event_type;
5555
setConfigFromParams({
5656
live_preview: event.data.hash,
@@ -59,41 +59,57 @@ export function useOnEntryUpdatePostMessageEvent(): void {
5959
// This section will run when there is a change in the entry and the website is CSR
6060
if (!ssr && !event_type) {
6161
onChange();
62-
}
62+
}
6363

64-
if(isOpeningInNewTab()) {
65-
if(!window) {
64+
if (isOpeningInNewTab()) {
65+
if (!window) {
6666
PublicLogger.error("window is not defined");
6767
return;
6868
};
69-
70-
// This section will run when there is a change in the entry and the website is SSR
71-
if(ssr && !event_type) {
72-
if(window.location.href.includes("live_preview")) {
69+
70+
if (ssr && !event_type) {
71+
const url = new URL(window.location.href);
72+
let live_preview = url.searchParams.get("live_preview");
73+
let content_type_uid = url.searchParams.get("content_type_uid");
74+
let entry_uid = url.searchParams.get("entry_uid");
75+
76+
if (live_preview && content_type_uid && entry_uid) {
77+
// All required params are present, just reload
7378
window.location.reload();
7479
} else {
75-
const url = new URL(window.location.href);
76-
url.searchParams.set("live_preview", event.data.hash);
77-
url.searchParams.set("content_type_uid", Config.get().stackDetails.contentTypeUid || "");
78-
url.searchParams.set("entry_uid", Config.get().stackDetails.entryUid || "");
80+
live_preview = event.data.hash;
81+
content_type_uid = event.data.content_type_uid || stackDetails.$contentTypeUid?.toString() || "";
82+
entry_uid = event.data.entry_uid || stackDetails.$entryUid?.toString() || "";
83+
// Set missing params and redirect
84+
url.searchParams.set("live_preview", live_preview);
85+
if (content_type_uid) {
86+
url.searchParams.set(
87+
"content_type_uid",
88+
content_type_uid
89+
);
90+
}
91+
if (entry_uid) {
92+
url.searchParams.set(
93+
"entry_uid",
94+
entry_uid
95+
);
96+
}
7997
window.location.href = url.toString();
8098
}
8199
}
82-
100+
83101
// This section will run when the hash changes and the website is SSR or CSR
84-
if(event_type === OnChangeLivePreviewPostMessageEventTypes.HASH_CHANGE){
102+
if (event_type === OnChangeLivePreviewPostMessageEventTypes.HASH_CHANGE) {
85103
const newUrl = new URL(window.location.href);
86104
newUrl.searchParams.set("live_preview", event.data.hash);
87105
window.history.pushState({}, "", newUrl.toString());
88106
}
89-
107+
90108
// This section will run when the URL of the page changes
91-
if(event_type === OnChangeLivePreviewPostMessageEventTypes.URL_CHANGE && event.data.url){
109+
if (event_type === OnChangeLivePreviewPostMessageEventTypes.URL_CHANGE && event.data.url) {
92110
window.location.href = event.data.url;
93111
}
94112
}
95-
96-
97113
} catch (error) {
98114
PublicLogger.error("Error handling live preview update:", error);
99115
return;

0 commit comments

Comments
 (0)