ReportingObserver
, a mechanism that allows you to learn about the use of outdated features, and that the browser is interfering with the work of the page code. ReportingObserver
is part of this W3C specification.[Intervention]
or [Deprecation]
.ReportingObserver
exists precisely to help developers deal with this problem. Using ReportingObserver
you can send reports to the developer on the use of outdated APIs or on interventions in the operation of the browser page. Other error handling tools do not respond to such things.ReportingObserver
looks like: const observer = new ReportingObserver((reports, observer) => { for (const report of reports) { console.log(report.type, report.url, report.body); } }, { buffered: true }); observer.observe();
window.onerror
. However, window.onerror
does not track absolutely all problem situations that arise during the operation of the page. For example, this tool helps to know about runtime errors caused by javascript exceptions or syntax errors present in the code. However, if you receive a notification about the use of an outdated feature or browser intervention, window.onerror
will not respond to such a notification. It is in this situation that ReportingObserver
comes in handy.ReportingObserver
API, you need to create an appropriate observer object, providing it with a callback, when you call it, it will be sent, in the form of a list, reports on problems encountered on the page. Above, we have already considered the code needed to work with ReportingObserver
. Now let's take a look at an example of what data goes into a callback: const observer = new ReportingObserver((reports, observer) => { for (const report of reports) { // → report.id === 'XMLHttpRequestSynchronousInNonWorkerOutsideBeforeUnload' // → report.type === 'deprecation' // → report.url === 'https://reporting-observer-api-demo.glitch.me' // → report.body.message === 'Synchronous XMLHttpRequest is deprecated...' } }}); observer.observe();
ReportingObserver
you can filter reports. For example, here is a design that allows you to receive only reports on the use of obsolete features: const observer = new ReportingObserver((reports, observer) => { ... }, {types: ['deprecation']});
ReportingObserver
object, you pass the buffered: true
property to it, it will give you the opportunity to see the reports generated before creating such an object: const observer = new ReportingObserver((reports, observer) => { ... }, {types: ['intervention'], buffered: true});
observer.disconnect();
ReportingObserver
. Here is a diagram of the organization of sending the report to the server: const report = new ReportingObserver((reports, observer) => { for (const report of reports) { const stringifiedReport = JSON.stringify(report.body); // sendReport(stringifiedReport); } }, { types: ['deprecation'], buffered: true });
ReportingObserver
API allows the developer to expand the range of information he receives about errors that occur in his projects when working with real users. This API is available in Chrome 69, there is information that this feature will appear in the Edge. It is not yet known whether it is planned to implement something similar in Firefox or Safari.ReportingObserver
can be found here and here .Source: https://habr.com/ru/post/427403/
All Articles