Search.../

afterRouter( )

Registers a hook that is called after a router.

Description

The afterRouter hook is a data binding router hook that is triggered after the router with the specified prefix has bound the data, but before the page is displayed. The router can be a code router or the data binding router that binds data to a dynamic page.

The afterRouter() function is not a function that you call from your code. You define the function in a file named routers.js in the Code File's Backend section of the Velo Sidebar. The function is called as described above.

Use this hook with a code router to change the router's response based on the data that was retrieved. For example, you can have two versions of a page, one for portrait oriented images and another for landscape oriented ones. After the image is pulled from the database, you can show the page that corresponds to the image's orientation.

The function receives a WixRouterRequest object containing information about the incoming request and a WixRouterResponse object containing information about the router's response.

The function returns a WixRouterResponse object that causes the router to respond with a specified page, specified data, and a success response code, or respond with any other HTTP response code. The returned response can be either the response received or a new response that overrides the one received.

If the function does not return a WixRouterResponse, the response received as the response parameter acts as the effective router response.

Typically, the response is created using one of the ok(), forbidden(), notFound(), redirect(), or sendStatus() functions.

Syntax

function afterRouter(request: WixRouterRequest, response: WixRouterResponse): Promise<WixRouterResponse>

afterRouter Parameters

NAME
TYPE
DESCRIPTION
request

The routing request.

response

The router response.

Returns

Fulfilled - A router response, either the response received or a new response.

Return Type:

Was this helpful?

After router stub

This example creates an after router hook on the myRouter prefix.

Copy Code
1// In routers.js
2
3export function myRouter_afterRouter(request, response) {
4 // after router code ...
5}
6
Basic after router hook

This example creates an after router hook on the myRouter prefix that possibly reroutes the request depending on some conditions.

Copy Code
1// In routers.js
2
3import {ok, forbidden} from 'wix-router';
4
5export function myRouter_afterRouter(request, response) {
6 if (some_condition)
7 return ok("different-page", response.data, response.head);
8 else if (other_condition)
9 return response;
10 else
11 return forbidden();
12}
13
After router hook that reroutes based on response data

This example creates an after router hook on the myRouter prefix that checks the orientation of a picture in the response's data and routes to a page that is built for displaying pictures in that orientation.

Copy Code
1// In routers.js
2
3import {ok} from 'wix-router';
4
5export function myRouter_afterRouter(request, response) {
6 if(response.status === 200 && response.page === "horizontal-pic") {
7 if(response.data.picture.orientation === "vertical")
8 return ok("vertical-pic", response.data, response.head);
9 else
10 return response;
11 }
12
13 return response;
14}
15