RouteHandlerFunction

To learn how to use RouteHandlerFunction, see Server-Side Routing.

Syntax

RouteHandlerFunction takes:

It returns a RouteHandlerViewResponse.

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:

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:

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