documentation/docs/api/route


sidebar_position: 10

Route

Whenever a network route is set up with Page#route or BrowserContext#route, the Route object
allows to handle the route.

Learn more about networking.

abort

def abort(errorCode: nil)

Aborts the route’s request.

continue

def continue(headers: nil, method: nil, postData: nil, url: nil)

Continues route’s request with optional overrides.

def handle(route, request)
  # override headers
  headers = request.headers
  headers['foo'] = 'bar' # set "foo" header
  headers['user-agent'] = 'Unknown Browser' # modify user-agent
  headers.delete('bar') # remove "bar" header

  route.continue(headers: headers)
end
page.route("**/*", method(:handle))

fallback

def fallback(headers: nil, method: nil, postData: nil, url: nil)

When several routes match the given pattern, they run in the order opposite to their registration. That way the last
registered route can always override all the previous ones. In the example below, request will be handled by the
bottom-most handler first, then it’ll fall back to the previous one and in the end will be aborted by the first
registered route.

page.route("**/*", -> (route,_) { route.abort })  # Runs last.
page.route("**/*", -> (route,_) { route.fallback })  # Runs second.
page.route("**/*", -> (route,_) { route.fallback })  # Runs first.

Registering multiple routes is useful when you want separate handlers to handle different kinds of requests, for example
API calls vs page resources or GET requests vs POST requests as in the example below.

# Handle GET requests.
def handle_post(route, request)
  if request.method != "GET"
    route.fallback
    return
  end

  # Handling GET only.
  # ...
end

# Handle POST requests.
def handle_post(route)
  if request.method != "POST"
    route.fallback
    return
  end

  # Handling POST only.
  # ...
end

page.route("**/*", handle_get)
page.route("**/*", handle_post)

One can also modify request while falling back to the subsequent handler, that way intermediate route handler can modify
url, method, headers and postData of the request.

def handle(route, request)
  # override headers
  headers = request.headers
  headers['foo'] = 'bar' # set "foo" header
  headers['user-agent'] = 'Unknown Browser' # modify user-agent
  headers.delete('bar') # remove "bar" header

  route.fallback(headers: headers)
end

fulfill

def fulfill(
      body: nil,
      contentType: nil,
      headers: nil,
      path: nil,
      response: nil,
      status: nil)

Fulfills route’s request with given response.

An example of fulfilling all requests with 404 responses:

page.route("**/*", ->(route, request) {
  route.fulfill(
    status: 404,
    contentType: 'text/plain',
    body: 'not found!!',
  )
})

An example of serving static file:

page.route("**/xhr_endpoint", ->(route, _) { route.fulfill(path: "mock_data.json") })

request

def request

A request to be routed.