@elementor/editor-audits 4.4.0-1073 → 4.4.0-1075

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.
package/dist/index.mjs CHANGED
@@ -39,18 +39,66 @@ function getWindowConfig() {
39
39
  return config;
40
40
  }
41
41
 
42
+ // src/utils/session-expiration.ts
43
+ var NONCE_INVALID_CODE = "rest_cookie_invalid_nonce";
44
+ var DEFAULT_AJAX_URL = "/wp-admin/admin-ajax.php";
45
+ var SessionExpiredError = class extends Error {
46
+ };
47
+ var nonceRefreshPromise = null;
48
+ function isNonceInvalidError(error) {
49
+ const response = error?.response;
50
+ return response?.status === 403 && response?.data?.code === NONCE_INVALID_CODE;
51
+ }
52
+ async function refreshAuditsNonce() {
53
+ if (nonceRefreshPromise) {
54
+ return nonceRefreshPromise;
55
+ }
56
+ nonceRefreshPromise = requestFreshNonce();
57
+ try {
58
+ return await nonceRefreshPromise;
59
+ } finally {
60
+ nonceRefreshPromise = null;
61
+ }
62
+ }
63
+ async function requestFreshNonce() {
64
+ const ajaxUrl = window.elementorCommon?.ajax?.config?.url ?? DEFAULT_AJAX_URL;
65
+ const url = new URL(ajaxUrl, window.location.origin);
66
+ url.searchParams.set("action", "rest-nonce");
67
+ const response = await fetch(url.toString(), { credentials: "same-origin" });
68
+ if (!response.ok) {
69
+ throw new Error(`Failed to refresh audits nonce: HTTP ${response.status}`);
70
+ }
71
+ const nonce = await response.text();
72
+ if (!nonce || "0" === nonce) {
73
+ throw new SessionExpiredError("Session expired \u2014 received invalid nonce");
74
+ }
75
+ window.elementorAudits = { ...getWindowConfig(), nonce };
76
+ return nonce;
77
+ }
78
+
42
79
  // src/api/page-context-client.ts
43
80
  async function fetchPageContext(documentId, attachmentIds) {
81
+ return requestPageContext(documentId, attachmentIds, true);
82
+ }
83
+ async function requestPageContext(documentId, attachmentIds, allowNonceRetry) {
44
84
  const { restNamespace, nonce } = getWindowConfig();
45
85
  const url = `${restNamespace}/audits/page-context`;
46
- const response = await httpService().get(url, {
47
- params: {
48
- document_id: documentId,
49
- attachment_ids: attachmentIds
50
- },
51
- headers: { "X-WP-Nonce": nonce }
52
- });
53
- return response.data;
86
+ try {
87
+ const response = await httpService().get(url, {
88
+ params: {
89
+ document_id: documentId,
90
+ attachment_ids: attachmentIds
91
+ },
92
+ headers: { "X-WP-Nonce": nonce }
93
+ });
94
+ return response.data;
95
+ } catch (error) {
96
+ if (!allowNonceRetry || !isNonceInvalidError(error)) {
97
+ throw error;
98
+ }
99
+ await refreshAuditsNonce();
100
+ return requestPageContext(documentId, attachmentIds, false);
101
+ }
54
102
  }
55
103
 
56
104
  // src/registry.ts
@@ -422,6 +470,9 @@ var slice = __createSlice({
422
470
  state.status = "error";
423
471
  state.error = action.payload;
424
472
  },
473
+ runAborted(state) {
474
+ state.status = state.report ? "ready" : "idle";
475
+ },
425
476
  reportRestored(state, action) {
426
477
  state.status = "ready";
427
478
  state.report = action.payload;
@@ -483,6 +534,10 @@ function useAuditReport() {
483
534
  dispatch(slice.actions.runSucceeded(nextReport));
484
535
  persistReport(documentIdToRun, nextReport);
485
536
  } catch (e) {
537
+ if (e instanceof SessionExpiredError) {
538
+ dispatch(slice.actions.runAborted());
539
+ return;
540
+ }
486
541
  dispatch(slice.actions.runFailed(e instanceof Error ? e.message : "Unknown error"));
487
542
  }
488
543
  };