@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.js CHANGED
@@ -77,18 +77,66 @@ function getWindowConfig() {
77
77
  return config;
78
78
  }
79
79
 
80
+ // src/utils/session-expiration.ts
81
+ var NONCE_INVALID_CODE = "rest_cookie_invalid_nonce";
82
+ var DEFAULT_AJAX_URL = "/wp-admin/admin-ajax.php";
83
+ var SessionExpiredError = class extends Error {
84
+ };
85
+ var nonceRefreshPromise = null;
86
+ function isNonceInvalidError(error) {
87
+ const response = error?.response;
88
+ return response?.status === 403 && response?.data?.code === NONCE_INVALID_CODE;
89
+ }
90
+ async function refreshAuditsNonce() {
91
+ if (nonceRefreshPromise) {
92
+ return nonceRefreshPromise;
93
+ }
94
+ nonceRefreshPromise = requestFreshNonce();
95
+ try {
96
+ return await nonceRefreshPromise;
97
+ } finally {
98
+ nonceRefreshPromise = null;
99
+ }
100
+ }
101
+ async function requestFreshNonce() {
102
+ const ajaxUrl = window.elementorCommon?.ajax?.config?.url ?? DEFAULT_AJAX_URL;
103
+ const url = new URL(ajaxUrl, window.location.origin);
104
+ url.searchParams.set("action", "rest-nonce");
105
+ const response = await fetch(url.toString(), { credentials: "same-origin" });
106
+ if (!response.ok) {
107
+ throw new Error(`Failed to refresh audits nonce: HTTP ${response.status}`);
108
+ }
109
+ const nonce = await response.text();
110
+ if (!nonce || "0" === nonce) {
111
+ throw new SessionExpiredError("Session expired \u2014 received invalid nonce");
112
+ }
113
+ window.elementorAudits = { ...getWindowConfig(), nonce };
114
+ return nonce;
115
+ }
116
+
80
117
  // src/api/page-context-client.ts
81
118
  async function fetchPageContext(documentId, attachmentIds) {
119
+ return requestPageContext(documentId, attachmentIds, true);
120
+ }
121
+ async function requestPageContext(documentId, attachmentIds, allowNonceRetry) {
82
122
  const { restNamespace, nonce } = getWindowConfig();
83
123
  const url = `${restNamespace}/audits/page-context`;
84
- const response = await (0, import_http_client.httpService)().get(url, {
85
- params: {
86
- document_id: documentId,
87
- attachment_ids: attachmentIds
88
- },
89
- headers: { "X-WP-Nonce": nonce }
90
- });
91
- return response.data;
124
+ try {
125
+ const response = await (0, import_http_client.httpService)().get(url, {
126
+ params: {
127
+ document_id: documentId,
128
+ attachment_ids: attachmentIds
129
+ },
130
+ headers: { "X-WP-Nonce": nonce }
131
+ });
132
+ return response.data;
133
+ } catch (error) {
134
+ if (!allowNonceRetry || !isNonceInvalidError(error)) {
135
+ throw error;
136
+ }
137
+ await refreshAuditsNonce();
138
+ return requestPageContext(documentId, attachmentIds, false);
139
+ }
92
140
  }
93
141
 
94
142
  // src/registry.ts
@@ -460,6 +508,9 @@ var slice = (0, import_store.__createSlice)({
460
508
  state.status = "error";
461
509
  state.error = action.payload;
462
510
  },
511
+ runAborted(state) {
512
+ state.status = state.report ? "ready" : "idle";
513
+ },
463
514
  reportRestored(state, action) {
464
515
  state.status = "ready";
465
516
  state.report = action.payload;
@@ -521,6 +572,10 @@ function useAuditReport() {
521
572
  dispatch(slice.actions.runSucceeded(nextReport));
522
573
  persistReport(documentIdToRun, nextReport);
523
574
  } catch (e) {
575
+ if (e instanceof SessionExpiredError) {
576
+ dispatch(slice.actions.runAborted());
577
+ return;
578
+ }
524
579
  dispatch(slice.actions.runFailed(e instanceof Error ? e.message : "Unknown error"));
525
580
  }
526
581
  };