Detecting Fetch Requests on a Document using PerformanceObserver
Learn how to detect fetch requests on a document using the PerformanceObserver API.

TL;DR
The PerformanceObserver API can be used to detect fetch requests on a document, allowing for logging, analytics, and initialization of other libraries. By observing performance entries of type 'resource', you can monitor fetch requests and their associated metrics. This can be useful for various purposes, such as debugging and performance optimization.
Introduction
Detecting fetch requests on a document can be useful for various purposes, such as logging, analytics, or initializing other libraries. In this blog post, we'll explore how to achieve this using the PerformanceObserver API.
What is PerformanceObserver?
The PerformanceObserver API allows you to observe performance metrics of your web application. It provides a way to monitor various performance-related events, such as resource loading, paint timing, and more.
Detecting Fetch Requests
To detect fetch requests, we'll create a PerformanceObserver instance and observe the 'resource' entry type. Here's the code:
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (entry.initiatorType === "fetch") {
console.log('Fetch request detected to', entry.name);
// For loading aos dynamically
// AOS.init();
}
}
});
observer.observe({
entryTypes: ["resource"]
});Let's break down the code:
- We create a new PerformanceObserver instance, passing a callback function that will be executed when new performance entries are available.
- Inside the callback, we iterate over the list of performance entries using
list.getEntries(). - We check if the
initiatorTypeproperty of each entry is equal to 'fetch', indicating that the resource was loaded using the Fetch API. - If it's a fetch request, we log a message to the console with the URL of the request.
- Finally, we start observing performance entries of type 'resource' using
observer.observe().
Usage Examples
You can use this code to detect fetch requests for various purposes, such as:
- Logging fetch requests for analytics or debugging purposes
- Initializing other libraries or plugins dynamically when a fetch request is made
- Monitoring fetch request performance metrics
Frequently Asked Questions
What is the PerformanceObserver API used for?
The PerformanceObserver API is used to observe performance metrics of a web application, including resource loading, paint timing, and other performance-related events. It provides a way to monitor and analyze the performance of a web application, allowing developers to identify areas for improvement. The PerformanceObserver API can be used in conjunction with the 'PerformanceObserver fetch request' to detect and analyze fetch requests.
How do I detect fetch requests using PerformanceObserver?
To detect fetch requests using PerformanceObserver, you need to create a PerformanceObserver instance and observe the 'resource' entry type. You can then iterate over the list of performance entries and check if the initiatorType property is equal to 'fetch', indicating that the resource was loaded using the Fetch API. This allows you to detect and analyze fetch requests, including those made using the 'PerformanceObserver fetch request'.
What are some use cases for detecting fetch requests with PerformanceObserver?
Some use cases for detecting fetch requests with PerformanceObserver include logging fetch requests for analytics or debugging purposes, initializing other libraries or plugins dynamically when a fetch request is made, and monitoring fetch request performance metrics. By using the PerformanceObserver API to detect fetch requests, you can gain insights into the performance and behavior of your web application, and make data-driven decisions to improve its performance and user experience.
You Might Also Like
Related posts about Shopify API & Development: Shopify Agentic Storefronts: What Developers Need to Know (March 2026), Shopify Checkout Extensibility Deep Dive 2026: Functions, UI Extensions & APIs, Shopify's New API Rate Limits 2026: What Developers Need to Know
Conclusion
In this blog post, we've learned how to detect fetch requests on a document using the PerformanceObserver API. By observing performance entries of type 'resource' and checking the initiatorType property, we can identify fetch requests and perform actions accordingly.
🛠️Web Development Tools You Might Like
Tags
📬 Get notified about new tools & tutorials
No spam. Unsubscribe anytime.
Comments (0)
Leave a Comment
No comments yet. Be the first to share your thoughts!