cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.09k stars 3.19k forks source link

support the service worker static routing api #30611

Open mschile opened 1 week ago

mschile commented 1 week ago

What would you like?

The service worker static routing api was introduced in Chrome 123 and allows defined routes to bypass the service worker. We currently assume all routes within the service worker's scope will go through the service worker.

Take the following test:

it('supports the static routing api', () => {
  const script = () => {
    addEventListener('install', (event) => {
      // bypass the service worker for the 1mb request
      event.addRoutes({
        condition: {
          urlPattern: '/fixtures/1mb*',
        },
        source: 'network',
      })
    })

    self.addEventListener('fetch', function (event) {
      console.log('fetch', event.request.url)
      event.respondWith(fetch(event.request))
    })
  }

  cy.intercept('/fixtures/service-worker.js', (req) => {
    req.reply(`(${script})()`,
      { 'Content-Type': 'application/javascript' })
  })

  cy.visit('fixtures/service-worker.html')
  cy.get('#output').should('have.text', 'done')
})

Running with DEBUG logs turned on,

DEBUG=cypress*:proxy:http:util:prerequests,cypress-verbose:proxy:http,cypress:proxy:service-worker-manager yarn cypress:run --project ~/Projects/cypress/packages/driver --spec ~/Projects/cypress/packages/driver/cypress/e2e/e2e/service-worker.cy.js --browser chrome

we see the 1mb prerequest timed out (250ms) waiting to see if it was controlled by the service worker.

cypress:proxy:service-worker-manager timed out checking if pre-request is controlled by service worker: { url: 'http://localhost:3500/fixtures/1mb?j=0.5439428840854426', requestId: '87682.90' } +257ms
cypress:proxy:service-worker-manager Request is not controlled by service worker: { url: 'http://localhost:3500/fixtures/1mb?j=0.5439428840854426', requestId: '87682.90', requestPotentiallyControlledByServiceWorker: true } +0ms

Why is this needed?

Adds support for the service worker static routing api.

Other

In order to add support for the static routing api, we could override addRoutes and save the route config so we can determine if a request will go through the service worker.

If we remove proxy correlation, this change will not be necessary.