@coframe/sdk 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1204,10 +1204,10 @@ const executeJs = (jsCode, args = {}, opts = {}) => {
1204
1204
  w.Coframe.vars[elKey] = opts.elementRef;
1205
1205
  }
1206
1206
  const invocation = opts.isExpression
1207
- ? `(${jsCode})(__args)`
1207
+ ? `(\n${jsCode}\n)(__args)`
1208
1208
  : opts.elementRef
1209
- ? `(function(el){ ${jsCode} })(__args.el)`
1210
- : `(function(){ ${jsCode} })()`;
1209
+ ? `(function(el){\n${jsCode}\n})(__args.el)`
1210
+ : `(function(){\n${jsCode}\n})()`;
1211
1211
  const ranKey = `${key}_ran`;
1212
1212
  const script = document.createElement('script');
1213
1213
  script.id = `coframe-script-${key}`;
@@ -1243,6 +1243,7 @@ const executeJs = (jsCode, args = {}, opts = {}) => {
1243
1243
  return value;
1244
1244
  };
1245
1245
  const applyJavascriptModification = (element, args, logger, nonce, logMetadata = {}) => {
1246
+ var _a, _b;
1246
1247
  const javascript = args.js;
1247
1248
  try {
1248
1249
  if (!javascript) {
@@ -1264,9 +1265,11 @@ const applyJavascriptModification = (element, args, logger, nonce, logMetadata =
1264
1265
  if (logger === null || logger === void 0 ? void 0 : logger.error) {
1265
1266
  logger.error('Error executing JavaScript:', 'error_execute_js', {
1266
1267
  ...logMetadata,
1267
- error,
1268
+ error: error instanceof Error
1269
+ ? error.stack || error.message
1270
+ : String(error),
1268
1271
  javascript,
1269
- element,
1272
+ element: (_b = (_a = element.outerHTML) === null || _a === void 0 ? void 0 : _a.slice(0, 200)) !== null && _b !== void 0 ? _b : element.tagName,
1270
1273
  });
1271
1274
  }
1272
1275
  else {
@@ -2907,9 +2910,12 @@ function setupTracking(Coframe, projectResult, previewVariantId, executionStartT
2907
2910
  return analytics;
2908
2911
  }
2909
2912
 
2910
- async function awaitHydration(config, Coframe) {
2913
+ async function awaitHydration(config, Coframe, project) {
2914
+ var _a, _b;
2911
2915
  if (!config.waitForHydration)
2912
2916
  return true;
2917
+ if (!((_a = project.frames) === null || _a === void 0 ? void 0 : _a.length) && !((_b = project.tags) === null || _b === void 0 ? void 0 : _b.length))
2918
+ return true;
2913
2919
  const cmd = await waitForCmd((cmd) => cmd.pageHydrated === true || cmd.emit === 'pageHydrated', Coframe);
2914
2920
  if (!cmd) {
2915
2921
  Coframe.logger.warn('Failed to wait for page hydrated command', 'wait_for_cmd_timeout');
@@ -3020,8 +3026,10 @@ function persistLaneClaims(claims, logger) {
3020
3026
  }
3021
3027
  }
3022
3028
  function createApplyVariants(Coframe, config, previewVariantId, executionStartTime, analytics, personalizationHeaders, laneStore) {
3029
+ let isFirstApply = true;
3023
3030
  return async (pageUrl, pageProject) => {
3024
- const sessionId = generateUUID();
3031
+ const sessionId = isFirstApply ? Coframe.sessionId : generateUUID();
3032
+ isFirstApply = false;
3025
3033
  Coframe.sessionId = sessionId;
3026
3034
  const urlPreviewVariantId = getCurrentUrlSearchParams().get('coframe_variant_id') || null;
3027
3035
  const updatedPreviewVariantId = urlPreviewVariantId !== previewVariantId
@@ -3052,7 +3060,7 @@ function createApplyVariants(Coframe, config, previewVariantId, executionStartTi
3052
3060
  async function modifyPage(Coframe, configResult, projectResult, analytics) {
3053
3061
  const { config, timeoutMs, previewVariantId, integrationStartTime, executionStartTime, } = configResult;
3054
3062
  const { project: cdnProject, personalizationHeaders } = projectResult;
3055
- if (!(await awaitHydration(config, Coframe)))
3063
+ if (!(await awaitHydration(config, Coframe, cdnProject)))
3056
3064
  return;
3057
3065
  if (cdnProject.tags) {
3058
3066
  executeTags(cdnProject.tags, Coframe.logger, config.nonce);
@@ -1202,10 +1202,10 @@ const executeJs = (jsCode, args = {}, opts = {}) => {
1202
1202
  w.Coframe.vars[elKey] = opts.elementRef;
1203
1203
  }
1204
1204
  const invocation = opts.isExpression
1205
- ? `(${jsCode})(__args)`
1205
+ ? `(\n${jsCode}\n)(__args)`
1206
1206
  : opts.elementRef
1207
- ? `(function(el){ ${jsCode} })(__args.el)`
1208
- : `(function(){ ${jsCode} })()`;
1207
+ ? `(function(el){\n${jsCode}\n})(__args.el)`
1208
+ : `(function(){\n${jsCode}\n})()`;
1209
1209
  const ranKey = `${key}_ran`;
1210
1210
  const script = document.createElement('script');
1211
1211
  script.id = `coframe-script-${key}`;
@@ -1241,6 +1241,7 @@ const executeJs = (jsCode, args = {}, opts = {}) => {
1241
1241
  return value;
1242
1242
  };
1243
1243
  const applyJavascriptModification = (element, args, logger, nonce, logMetadata = {}) => {
1244
+ var _a, _b;
1244
1245
  const javascript = args.js;
1245
1246
  try {
1246
1247
  if (!javascript) {
@@ -1262,9 +1263,11 @@ const applyJavascriptModification = (element, args, logger, nonce, logMetadata =
1262
1263
  if (logger === null || logger === void 0 ? void 0 : logger.error) {
1263
1264
  logger.error('Error executing JavaScript:', 'error_execute_js', {
1264
1265
  ...logMetadata,
1265
- error,
1266
+ error: error instanceof Error
1267
+ ? error.stack || error.message
1268
+ : String(error),
1266
1269
  javascript,
1267
- element,
1270
+ element: (_b = (_a = element.outerHTML) === null || _a === void 0 ? void 0 : _a.slice(0, 200)) !== null && _b !== void 0 ? _b : element.tagName,
1268
1271
  });
1269
1272
  }
1270
1273
  else {
@@ -2905,9 +2908,12 @@ function setupTracking(Coframe, projectResult, previewVariantId, executionStartT
2905
2908
  return analytics;
2906
2909
  }
2907
2910
 
2908
- async function awaitHydration(config, Coframe) {
2911
+ async function awaitHydration(config, Coframe, project) {
2912
+ var _a, _b;
2909
2913
  if (!config.waitForHydration)
2910
2914
  return true;
2915
+ if (!((_a = project.frames) === null || _a === void 0 ? void 0 : _a.length) && !((_b = project.tags) === null || _b === void 0 ? void 0 : _b.length))
2916
+ return true;
2911
2917
  const cmd = await waitForCmd((cmd) => cmd.pageHydrated === true || cmd.emit === 'pageHydrated', Coframe);
2912
2918
  if (!cmd) {
2913
2919
  Coframe.logger.warn('Failed to wait for page hydrated command', 'wait_for_cmd_timeout');
@@ -3018,8 +3024,10 @@ function persistLaneClaims(claims, logger) {
3018
3024
  }
3019
3025
  }
3020
3026
  function createApplyVariants(Coframe, config, previewVariantId, executionStartTime, analytics, personalizationHeaders, laneStore) {
3027
+ let isFirstApply = true;
3021
3028
  return async (pageUrl, pageProject) => {
3022
- const sessionId = generateUUID();
3029
+ const sessionId = isFirstApply ? Coframe.sessionId : generateUUID();
3030
+ isFirstApply = false;
3023
3031
  Coframe.sessionId = sessionId;
3024
3032
  const urlPreviewVariantId = getCurrentUrlSearchParams().get('coframe_variant_id') || null;
3025
3033
  const updatedPreviewVariantId = urlPreviewVariantId !== previewVariantId
@@ -3050,7 +3058,7 @@ function createApplyVariants(Coframe, config, previewVariantId, executionStartTi
3050
3058
  async function modifyPage(Coframe, configResult, projectResult, analytics) {
3051
3059
  const { config, timeoutMs, previewVariantId, integrationStartTime, executionStartTime, } = configResult;
3052
3060
  const { project: cdnProject, personalizationHeaders } = projectResult;
3053
- if (!(await awaitHydration(config, Coframe)))
3061
+ if (!(await awaitHydration(config, Coframe, cdnProject)))
3054
3062
  return;
3055
3063
  if (cdnProject.tags) {
3056
3064
  executeTags(cdnProject.tags, Coframe.logger, config.nonce);
@@ -45,12 +45,15 @@ async function fetchProjectFromEdge(projectId, currentUrl, signal, userContext,
45
45
  }
46
46
  }
47
47
 
48
- async function fetchAntiflickerFromEdge(projectId, currentUrl, userContext, headers) {
48
+ async function fetchAntiflickerFromEdge(projectId, currentUrl, userContext, enableElementAntiFlicker, headers) {
49
49
  try {
50
50
  const payload = { projectId, currentUrl };
51
51
  if (userContext) {
52
52
  payload.userContext = userContext;
53
53
  }
54
+ if (enableElementAntiFlicker) {
55
+ payload.enableElementAntiFlicker = enableElementAntiFlicker;
56
+ }
54
57
  const config = encodeURIComponent(JSON.stringify(payload));
55
58
  const edgeUrl = new URL(`${"https://edge.cofra.me"}/cf.css`);
56
59
  edgeUrl.searchParams.set('config', config);
@@ -73,7 +76,7 @@ async function getProjectData(options) {
73
76
  ? fetchProjectFromEdge(options.projectId, options.currentUrl, undefined, options.userContext, options.headers)
74
77
  : Promise.resolve(null),
75
78
  includeCSS
76
- ? fetchAntiflickerFromEdge(options.projectId, options.currentUrl, options.userContext, options.headers)
79
+ ? fetchAntiflickerFromEdge(options.projectId, options.currentUrl, options.userContext, options.enableElementAntiFlicker, options.headers)
77
80
  : Promise.resolve(null),
78
81
  ]);
79
82
  const config = {};
@@ -43,12 +43,15 @@ async function fetchProjectFromEdge(projectId, currentUrl, signal, userContext,
43
43
  }
44
44
  }
45
45
 
46
- async function fetchAntiflickerFromEdge(projectId, currentUrl, userContext, headers) {
46
+ async function fetchAntiflickerFromEdge(projectId, currentUrl, userContext, enableElementAntiFlicker, headers) {
47
47
  try {
48
48
  const payload = { projectId, currentUrl };
49
49
  if (userContext) {
50
50
  payload.userContext = userContext;
51
51
  }
52
+ if (enableElementAntiFlicker) {
53
+ payload.enableElementAntiFlicker = enableElementAntiFlicker;
54
+ }
52
55
  const config = encodeURIComponent(JSON.stringify(payload));
53
56
  const edgeUrl = new URL(`${"https://edge.cofra.me"}/cf.css`);
54
57
  edgeUrl.searchParams.set('config', config);
@@ -71,7 +74,7 @@ async function getProjectData(options) {
71
74
  ? fetchProjectFromEdge(options.projectId, options.currentUrl, undefined, options.userContext, options.headers)
72
75
  : Promise.resolve(null),
73
76
  includeCSS
74
- ? fetchAntiflickerFromEdge(options.projectId, options.currentUrl, options.userContext, options.headers)
77
+ ? fetchAntiflickerFromEdge(options.projectId, options.currentUrl, options.userContext, options.enableElementAntiFlicker, options.headers)
75
78
  : Promise.resolve(null),
76
79
  ]);
77
80
  const config = {};
@@ -234,6 +234,7 @@ type CoframeConfig = {
234
234
  projectId: string;
235
235
  trackingOnly?: boolean;
236
236
  enableAntiFlickering?: boolean;
237
+ enableElementAntiFlicker?: boolean;
237
238
  waitForHydration?: boolean;
238
239
  waitForCookiesEnabled?: boolean;
239
240
  waitForFeatureFlag?: boolean;
@@ -237,6 +237,7 @@ type CoframeConfig = {
237
237
  projectId: string;
238
238
  trackingOnly?: boolean;
239
239
  enableAntiFlickering?: boolean;
240
+ enableElementAntiFlicker?: boolean;
240
241
  waitForHydration?: boolean;
241
242
  waitForCookiesEnabled?: boolean;
242
243
  waitForFeatureFlag?: boolean;
@@ -215,6 +215,7 @@ interface GetProjectDataOptions {
215
215
  currentUrl: string;
216
216
  userContext?: Record<string, unknown>;
217
217
  headers: Headers | Record<string, string>;
218
+ enableElementAntiFlicker?: boolean;
218
219
  includeProject?: boolean;
219
220
  includeCSS?: boolean;
220
221
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coframe/sdk",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Coframe optimizes your website using generative AI to deliver maximum impact to your audience and allow your site to improve itself as you sleep. Learn more at coframe.com.",
5
5
  "main": "dist/pkg/cf.cjs.js",
6
6
  "module": "dist/pkg/cf.esm.mjs",