Search code examples
chromiumpuppeteer

How to stop puppeteer follow redirects


Currently it seems the default behaviour of puppeteer is to follow redirects and return the DOM at the end of the chain.

How can I make the .goto() method to stop after the first redirect happened and simply return the html from that first 3xx page when i call page.content() method?


Solution

  • You can enable a request interception and abort additional requests if a chain of requests is identified:

    await page.setRequestInterception(true);
    
    page.on('request', request => {
      if (request.isNavigationRequest() && request.redirectChain().length !== 0) {
        request.abort();
      } else {
        request.continue();
      }
    });
    
    await page.goto('https://www.example.com/');