日本語

How to Pause Chrome DevTools on a Fetch to a Specific URL

​​Chrome DevTools​debugging,fetch,JavaScript,XMLHttpRequest

By adding a string that appears in the URL (such as price) to XHR/fetch breakpoints in the Sources panel of Chrome DevTools, execution pauses on the line that calls fetch() for that URL. This lets you find which code sends an API request to the wrong URL without searching through files. In a test environment, I stopped only on the price API out of three requests, and I include a screenshot of that along with the timing of when the request reached the server.

Note

The test environment was Ubuntu 24.04 with Chrome for Testing 153.0.8010.12 (the version bundled with Playwright 1.63.0). I launched a windowed Chrome on a virtual display and operated the DevTools docked on the right side of the window by sending mouse clicks and keystrokes. The DevTools UI was in English, so button and field names are given in English. The page was served from an HTTP server I started locally (http://localhost:8080/). I did not test macOS or Windows Chrome, requests issued from inside Web Workers or Service Workers, or requests to a different origin.

The test page

The page fetches three JSON resources when you press the "Check stock and price" button: stock, price, and reviews. Stock and price use fetch(), and reviews use XMLHttpRequest. The contents of app.js are as follows.

js
async function loadStock() {
  const res = await fetch('/api/stock.json');
  const data = await res.json();
  document.getElementById('stock').textContent = data.label;
}

async function loadPrice(itemId) {
  const res = await fetch(`/api/price.json?item=${itemId}`);
  const data = await res.json();
  document.getElementById('price').textContent = data.label;
}

function loadReviews() {
  const xhr = new XMLHttpRequest();
  xhr.open('GET', '/api/reviews.json');
  xhr.onload = () => {
    document.getElementById('reviews').textContent = JSON.parse(xhr.responseText).label;
  };
  xhr.send();
}

document.getElementById('load').addEventListener('click', () => {
  loadStock();
  loadPrice(42);
  loadReviews();
});

When the button is pressed, three requests go out in this order: /api/stock.json, /api/price.json?item=42, and /api/reviews.json. Here, I pause at the place where the price API request is sent.

Registering part of the URL as an XHR/fetch breakpoint

The steps are as follows.

  1. Open the Sources panel in DevTools.
  2. Click the XHR/fetch breakpoints heading in the right-hand pane to expand it.
  3. Click the "+" (Add XHR/fetch breakpoint) at the right end of the heading.
  4. In the input field labeled "Break when URL contains:", type a string that appears in the URL you want to stop on, then press Enter.

In the test environment, I typed price. After pressing Enter, a row reading "URL contains "price"" was added with its checkbox ticked.

The XHR/fetch breakpoints pane. The top image shows the state just after clicking +, with the "Break when URL contains:" input field showing. The bottom image shows the state after typing price and pressing Enter, with the checked "URL contains "price"" row present.
Just after clicking + (top), and after registering price (bottom)

Pressing the button pauses on the price fetch line

With this set, pressing the button on the page paused execution on the fetch() line inside loadPrice. The DevTools display was as follows.

Location Display
Top of the right-hand pane Paused on XHR or fetch http://localhost:8080/api/price.json?item=42
Editor Line 8 of app.js gets a yellow background (Line 8, Column 21 shown at the bottom of the editor)
Call Stack loadPrice app.js:8 → (anonymous) app.js:24
XHR/fetch breakpoints The "URL contains "price"" row gets a yellow background
The Sources panel when paused on price. The fetch on line 8 of app.js has a yellow background, and the right-hand pane shows Paused on XHR or fetch with the URL, Scope showing itemId: 42, and loadPrice in the Call Stack.
Paused on a fetch to a URL containing price

The second entry in the Call Stack, app.js:24, is the line inside the button's click listener that calls loadPrice(42). You can see not only the function that is about to send the request but also where it was called from. The Scope pane shows itemId: 42, so you can inspect the value used to build the URL at that moment.

While it was paused, I checked the access log of the HTTP server. Only /api/stock.json, which goes out first, had arrived; /api/price.json?item=42 had not yet arrived. The price field still showed its placeholder value, a dash ("-"). When I pressed the Resume button (Resume script execution), /api/price.json?item=42 and /api/reviews.json arrived, and all three fields on the page were filled in. Execution stops before the request is sent.

What strings cause a pause

The official documentation on breakpoints says that execution pauses when the registered string appears anywhere in the request URL. I changed the registered string and pressed the same button each time.

Registered string Result
price Paused on the fetch() for price (line 8)
Price Did not pause
item=42 Paused on the fetch() for price (line 8)
8080/api/price Paused on the fetch() for price (line 8)
reviews Paused on the xhr.send() for reviews (line 19)

Case was significant. With Price, the search did not pause even though the URL contains price. Because item=42 in the query string also triggers a pause, you can also stop only on requests with a specific ID.

The code uses the relative path /api/price.json, but 8080/api/price also paused. The string being matched is not the one written in the code but the full URL, including the host name and port, such as http://localhost:8080/api/price.json?item=42. The URL shown in the pause display was in this form as well.

With XMLHttpRequest, it pauses on the send() line

When I registered reviews, execution paused not on the xhr.open() line but on xhr.send() at line 19. The official documentation also says that for XHR, execution pauses on the line that calls send(). While it was paused, /api/reviews.json had not reached the server.

The Sources panel when paused on reviews. The xhr.send() on line 19 of app.js has a yellow background, and the top of the Call Stack is loadReviews app.js:19.
Paused on XMLHttpRequest send()

Registering an empty string pauses on every request

After clicking "+", I pressed Enter without typing anything, and an "Any XHR or fetch" row was created. With this set, pressing the button paused once for each of the three requests.

Order Line where it paused URL
1st fetch() in loadStock (line 2) http://localhost:8080/api/stock.json
2nd fetch() in loadPrice (line 8) http://localhost:8080/api/price.json?item=42
3rd xhr.send() in loadReviews (line 19) http://localhost:8080/api/reviews.json

If you don't know which request URL is the problem, first use this to step through the requests the page sends in order. Once you identify a suspicious URL, you can narrow things down by registering part of it instead.

To stop pausing, uncheck the row

If you uncheck the checkbox to the left of a registered row, the row remains but stops triggering pauses. I registered price, unchecked it, and pressed the button. It never paused, and all three requests reached the server.

​Posted by 86