Skip to content

Commit 2405fc6

Browse files
committed
Fix TypeError: Cannot read properties of null
Turbo expects that when there are errors in validations, for example, it should respond with status 422, which causes the iframe to disappear from the DOM, so the null error occurs in subsequent requests.
1 parent 6d5a5b8 commit 2405fc6

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

webroot/js/inject-iframe.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ if (elem) {
7777
url: this._arguments && this._arguments[1],
7878
type: this.getResponseHeader('Content-Type'),
7979
};
80-
iframe.contentWindow.postMessage(`ajax-completed$$${JSON.stringify(params)}`, window.location.origin);
80+
iframe.contentWindow?.postMessage(`ajax-completed$$${JSON.stringify(params)}`, window.location.origin);
8181
}
8282
if (original) {
8383
return original.apply(this, [].slice.call(arguments));
@@ -124,7 +124,7 @@ if (elem) {
124124
url,
125125
type: response.headers.get('Content-Type'),
126126
};
127-
iframe.contentWindow.postMessage(`ajax-completed$$${JSON.stringify(params)}`, window.location.origin);
127+
iframe.contentWindow?.postMessage(`ajax-completed$$${JSON.stringify(params)}`, window.location.origin);
128128
}
129129
return response;
130130
} catch (error) {
@@ -138,7 +138,7 @@ if (elem) {
138138
type: null,
139139
error: error.message,
140140
};
141-
iframe.contentWindow.postMessage(`ajax-completed$$${JSON.stringify(params)}`, window.location.origin);
141+
iframe.contentWindow?.postMessage(`ajax-completed$$${JSON.stringify(params)}`, window.location.origin);
142142
throw error;
143143
}
144144
};

0 commit comments

Comments
 (0)