Skip to main content

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.


Methods​

abort​

Added before v1.9 route.abort

Aborts the route's request.

Usage

Route.abort();
Route.abort(errorCode);

Arguments

  • errorCode String (optional)#

    Optional error code. Defaults to failed, could be one of the following:

    • 'aborted' - An operation was aborted (due to user action)
    • 'accessdenied' - Permission to access a resource, other than the network, was denied
    • 'addressunreachable' - The IP address is unreachable. This usually means that there is no route to the specified host or network.
    • 'blockedbyclient' - The client chose to block the request.
    • 'blockedbyresponse' - The request failed because the response was delivered along with requirements which are not met ('X-Frame-Options' and 'Content-Security-Policy' ancestor checks, for instance).
    • 'connectionaborted' - A connection timed out as a result of not receiving an ACK for data sent.
    • 'connectionclosed' - A connection was closed (corresponding to a TCP FIN).
    • 'connectionfailed' - A connection attempt failed.
    • 'connectionrefused' - A connection attempt was refused.
    • 'connectionreset' - A connection was reset (corresponding to a TCP RST).
    • 'internetdisconnected' - The Internet connection has been lost.
    • 'namenotresolved' - The host name could not be resolved.
    • 'timedout' - An operation timed out.
    • 'failed' - A generic failure occurred.

Returns


fallback​

Added in: v1.23 route.fallback

Continues route's request with optional overrides. The method is similar to Route.resume() with the difference that other matching handlers will be invoked before sending the request.

Usage

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 -> {
// Runs last.
route.abort();
});

page.route("**/*", route -> {
// Runs second.
route.fallback();
});

page.route("**/*", route -> {
// Runs first.
route.fallback();
});

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.
page.route("**/*", route -> {
if (!route.request().method().equals("GET")) {
route.fallback();
return;
}
// Handling GET only.
// ...
});

// Handle POST requests.
page.route("**/*", route -> {
if (!route.request().method().equals("POST"))