Skip to content

Block resources without request interception

berstend̡̲̫̹̠̖͚͓̔̄̓̐̄͛̀͘ edited this page Jun 27, 2022 · 5 revisions
const blockedResources = [
  // Assets
  '*/favicon.ico',
  '.css',
  '.jpg',
  '.jpeg',
  '.png',
  '.svg',
  '.woff',

  // Analytics and other fluff
  '*.optimizely.com',
  'everesttech.net',
  'userzoom.com',
  'doubleclick.net',
  'googleadservices.com',
  'adservice.google.com/*',
  'connect.facebook.com',
  'connect.facebook.net',
  'sp.analytics.yahoo.com',
]
// Use existing CDP connection from Puppeteer (puppeteer >= 14.4.1: page._client -> page._client())
const client = typeof page._client === 'function' ? page._client() : page._client
await client.send('Network.setBlockedURLs', { urls: blockedResources });

To do that in playwright (note: only chrome/chromium browsers support CDP):

const client = await page.context().newCDPSession(page)
await client.send('Network.setBlockedURLs', { urls: blockedResources });

Note: The above will use the underlying CDP (Chrome Devtools Protocol) directly, documentation for Network.setBlockedURLs can be found here.