Detect Fetch Requests with PerformanceObserver

Short answer: use PerformanceObserver on resource entries, then filter PerformanceResourceTiming entries where initiatorType is fetch. This observes completed resource timing entries. It does not give you request bodies, response bodies, custom headers, or a hook before the request leaves the browser.

I reach for this when I need passive telemetry: which endpoint was called, when it started, how long it took, and whether a page is doing extra work after hydration. If I need to modify the request, I use a real wrapper around fetch instead.

Copy/Paste Starting Point

javascriptCopy

const observer = new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    if (entry.entryType !== 'resource') continue;
    if (entry.initiatorType !== 'fetch') continue;

console.log({
      url: entry.name,
      startTime: Math.round(entry.startTime),
      duration: Math.round(entry.duration),
      transferSize: entry.transferSize,
      encodedBodySize: entry.encodedBodySize,
    });
  }
});

observer.observe({ type: 'resource', buffered: true });

The buffered option is important. Without it, your observer only sees entries created after the observer starts. On a Next.js or Shopify theme page, some fetches may already have happened by the time your script runs.

What You Can Reliably Use It For

What It Will Not Do

Filtering Your Own API Calls

javascriptCopy

const ownApiCalls = [];

const observer = new PerformanceObserver((list) => {
  for (const entry of list.getEntriesByType('resource')) {
    const url = new URL(entry.name);

if (entry.initiatorType === 'fetch' && url.pathname.startsWith('/api/')) {
      ownApiCalls.push({
        path: url.pathname,
        duration: entry.duration,
        size: entry.transferSize,
      });
    }
  }
});

observer.observe({ type: 'resource', buffered: true });

In production, I would sample this data and strip query strings before sending it anywhere. Query strings can contain search terms, emails, tokens, or customer identifiers.

When I Use a Fetch Wrapper Instead

If the goal is retries, auth headers, request IDs, request body inspection, or error normalization, PerformanceObserver is the wrong tool. Wrap fetch at the call site, or use your HTTP client layer. PerformanceObserver is best as an observer, not as control flow.

Sources

A Debug Overlay Pattern

For development builds, I sometimes want a tiny overlay that shows recent fetch calls without opening DevTools. PerformanceObserver is a good fit because it stays passive and does not alter network behavior.

javascriptCopy

const recentFetches = [];
const MAX_ROWS = 20;

new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    if (entry.initiatorType !== 'fetch') continue;
    recentFetches.unshift({
      url: entry.name,
      duration: Math.round(entry.duration),
      startedAt: Math.round(entry.startTime),
    });
    recentFetches.length = Math.min(recentFetches.length, MAX_ROWS);
  }
}).observe({ type: 'resource', buffered: true });

I would keep this out of production bundles unless it is sampled and privacy-reviewed. URLs can leak sensitive query parameters. If the page calls third-party endpoints, you also need to think about whether those URLs belong in your logs.

Cross-Origin Timing Caveat

Resource timing data can be limited for cross-origin requests unless the remote server opts in with Timing-Allow-Origin. That means you may see the URL and rough entry, but not all timing detail. If you are debugging your own API, configure timing headers deliberately. If you are observing third-party scripts, expect partial data.

PerformanceObserver vs Fetch Monkey Patch

Validation Steps

The API is small, but the boundary matters. PerformanceObserver tells you what the browser recorded. It does not make fetch observable in the same way an application-level client does.

How I would debug this in production

When I would use this in production, I would turn the idea into a repeatable debug path. Detect Fetch Requests with PerformanceObserver Without Monkey Patching Fetch should leave the reader with a command, fixture, checklist, or failure mode they can verify without guessing.

I would treat this as a real production decision: define the expected behavior, name the risk, make the smallest useful change, and verify the result with evidence from the page, command, metric, or support case.

Engineering validation checklist

Technical failure modes

Debug note template

textCopy

Debug checklist for Detect Fetch Requests with PerformanceObserver Without Monkey Patching Fetch:
- Reproduce the issue with a small fixture.
- Log the failing input and expected output.
- Patch the smallest responsible module.
- Add a regression test or repeatable command.
- Document the remaining production risk.

I keep this kind of note short so it can be reused during review without becoming another document nobody reads.

What I would test next

The next upgrade I would make is to add a real artifact: screenshot, command output, before/after table, benchmark, source link, or QA note. Those details give the page more authority and make it more useful to answer engines.

Top Rated Plus · 100% Job Success

Want this built for you instead of DIY?

Top Rated Plus · 100% Job Success · 200+ completed projects · $300K+ earned · 5,000+ Upwork hours. Send the actual workflow for a technical assessment.

Theme & Performance Development Case Studies Discuss Project WhatsApp Me

🛠️Web Development Tools You Might Like

💻\ \ JSON Formatter\ \ Format, validate, and minify JSON data 🔐\ \ JWT Decoder\ \ Decode and inspect JSON Web Tokens 🔍\ \ Regex Tester\ \ Test and debug regular expressions 🔑\ \ UUID Generator\ \ Generate random UUIDs instantly

Tags

#PerformanceObserver#Fetch API#Web Development#JavaScript

Share this article

Share on X (Twitter)Share on LinkedIn

📬 Get notified about new tools & tutorials

No spam. Unsubscribe anytime.

Subscribe

Comments (0)

Leave a Comment

Name *

Email * (not published)

Comment *

0/2000

Post Comment

No comments yet. Be the first to share your thoughts!