---
schema_version: "1"
id: 0f397c04c9ddd922629aad8e109f7f11161df8634473d25da3db8f6744d1ad92
title: RouteHandlerFunction
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/lwr-node/lwr/references/ss-routing-reference/routehandlerfunction.md
canonical_url: https://developer.salesforce.com/docs/platform/lwr/references/ss-routing-reference/routehandlerfunction.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 4d1c9fe3a94b99cda0b287b4c7cecb8b4c2acfa0ef067802b0ac62c89d0fd263
products:
  - Lightning Web Runtime
---
# RouteHandlerFunction

To learn how to use `RouteHandlerFunction`, see [Server-Side Routing](https://developer.salesforce.com/docs/platform/lwr/guide/lwr-routing-server.html#route-handler-functions).

## Syntax

`RouteHandlerFunction` takes:

- a [`ViewRequest`](#viewrequest)
- a [`HandlerContext`](#handlercontext)

It returns a [`RouteHandlerViewResponse`](https://developer.salesforce.com/docs/platform/lwr/references/ss-routing-reference/routehandlerviewresponse.md).

```
type RouteHandlerFunction = (
  viewRequest: ViewRequest,
  handlerContext: HandlerContext,
) => Promise<RouteHandlerViewResponse>;
```

### `ViewRequest`

A `ViewRequest` object has to have:

- a URL
- a path (`requestPath`)

`ViewRequest` can optionally have any number of:

- parameters (`params`)
- queries (`query`)

```
// Request: given GET http://www.example.com/custom/thing?sort=desc
interface ViewRequest {
  url: string; // "/custom/thing?sort=desc
  requestPath: string; // "/custom/thing"
  params?: Record<string, string>; // { "param": "thing" }
  query?: Record<string, undefined | string | string[]>; // { "sort": "desc" }
}
```

For example, the `ViewRequest` object for the request `get http://www.example.com/custom/thing?sort=desc` looks like this.

```
{
  url: "/custom/thing?sort=desc
  requestPath: "/custom/thing"
  params?: { "param": "thing" }
  query?: { "sort": "desc" }
}
```

### `HandlerContext`

A `HandlerContext` object has to have:

- a route in the form of an `LwrRoute` or an `LwrErrorRoute` object
- a [`RouteHandlerViewApi`](#routehandlerviewapi) call

```
interface HandlerContext {
  route: LwrRoute | LwrErrorRoute; // the current route from lwr.config.json
  viewApi: RouteHandlerViewApi; // functions to call into the LWR server
}
```

### `RouteHandlerViewApi`

The `RouteHandlerViewApi` calls the functions:

- `hasViewResponse`
  - Takes a [`RouteHandlerView`](#routehandlerview) and a `Record`
  - Returns a boolean
- `getViewResponse`
  - Takes a `RouteHandlerView` and a `Record`
  - Returns a [`RouteHandlerViewResponse`](https://developer.salesforce.com/docs/platform/lwr/references/ss-routing-reference/routehandlerviewresponse.md) or `undefined`

```
interface RouteHandlerViewApi {
  // interact with the LWR rendered page view cache
  hasViewResponse(view: RouteHandlerView, viewParams?: Record<string, Json>): boolean;
  getViewResponse(
    view: RouteHandlerView,
    viewParams?: Record<string, Json>,
  ): Promise<RouteHandlerViewResponse | undefined>;
}
```

### `RouteHandlerView`

A `RouteHandlerView` object has this syntax:

```
type RouteHandlerView = Pick<LwrRoute, 'contentTemplate' | 'layoutTemplate' | 'rootComponent'>;
```

**See Also**

- [Server-Side Routing: Route Handler Functions](https://developer.salesforce.com/docs/platform/lwr/guide/lwr-routing-server.html#route-handler-functions)
