wix-router

wix-router

This module contains the APIs for code routers and data binding router hooks.

The code for both routers and data binding router hooks are defined in the routers.js file in the Backend section of the Site Structure sidebar.

The functions are named with the following convention:

IGV4cG9ydCBmdW5jdGlvbiAmbHQ7cm91dGVyIHByZWZpeCZndDtfJmx0O2Z1bmN0aW9uIG5hbWUmZ3Q7KCk=
 export function <router prefix>_<function name>()

These are not functions that you call in your code, rather they are functions that you define. They are called when your users browse to a URL that is handled by a router as described below.

For example, the following code creates a router on the myRouter prefix that shows a page when the path begins with the word "good" and returns a 404 in all other cases.

IGltcG9ydCB7b2ssIG5vdEZvdW5kfSBmcm9tICJ3aXgtcm91dGVyIjsKCiBleHBvcnQgZnVuY3Rpb24gbXlSb3V0ZXJfUm91dGVyKHJlcXVlc3QpIHsKCiAgIC8vIFVSTCBsb29rcyBsaWtlOgogICAvLyBodHRwczovL215c2l0ZS5jb20vbXlSb3V0ZXIvZ29vZAogICAvLyBvcjoKICAgLy8gaHR0cHM6Ly91c2VyLndpeHNpdGUuY29tL215c2l0ZS9teVJvdXRlci9nb29kCiAgIGNvbnN0IHN0YXR1cyA9IHJlcXVlc3QucGF0aFswXTsKCiAgIGlmKHN0YXR1cyA9PT0gImdvb2QiKSB7CiAgICAgLy8gU2hvdyBhIHBhZ2UKICAgICByZXR1cm4gb2soIm15Um91dGVyLXBhZ2UiKTsKICAgfQogICBlbHNlIHsKICAgICAvLyBSZXR1cm4gNDA0CiAgICAgcmV0dXJuIG5vdEZvdW5kKCk7CiAgIH0KIH0=
 import {ok, notFound} from "wix-router";

 export function myRouter_Router(request) {

   // URL looks like:
   // https://mysite.com/myRouter/good
   // or:
   // https://user.wixsite.com/mysite/myRouter/good
   const status = request.path[0];

   if(status === "good") {
     // Show a page
     return ok("myRouter-page");
   }
   else {
     // Return 404
     return notFound();
   }
 }

Code Router

Code your own router() and sitemap() functions for a router that handles all incoming requests with a specified URL prefix. Your code decides what actions to perform, what responses to return, where to route the request, and what data to pass to pages.

Data Binding Router Hooks

When a request comes in for a page that a router handles, either a code router or a dynamic page, you can add data binding router hooks to intercept the process of the data getting bound to the page at certain points and insert additional logic.

The hooks you can use are listed here in the order they are triggered:

  • beforeRouter - Before the data binding router logic.
  • customizeQuery - As the data binding router prepares a data query.
  • afterRouter - After the data binding router completes its logic, but before the page is displayed.
  • afterSitemap - After the data binding sitemap function completes preparing the list of urls.

Contents

WixRouterRequest An object representing an incoming page request received by a router.
WixRouterResponse An object representing a response to a request received by a router.
WixRouterSitemapRequest An object representing a request for a sitemap.
WixRouterUser An object that contains information about the current site visitor who is logged in.
WixRouterSitemapEntry An object representing a single entry in a sitemap.
afterRouter( ) Registers a hook that is called after a router.
afterSitemap( ) Registers a hook that is called after a sitemap is created.
beforeRouter( ) Registers a hook that is called before a router.
customizeQuery( ) Registers a hook that is called after a route is resolved by the data binding router, but before the wix-data query is executed.
forbidden( ) Returns a response with a status code 403 (Forbidden) and instructs the router to show a 403 page.
next( ) Returns a response that instructs the router to continue.
notFound( ) Returns a response with a status code 404 (Not Found) and instructs the router to show a 404 page.
ok( ) Returns a response with a status code 200 (OK) and instructs the router to show the selected page.
redirect( ) Returns a response with a status code of 301 (Moved Permanently) or 302 (Found) and instructs the router to redirect to the given URL.
router( ) Function containing routing logic for a given URL prefix.
sendStatus( ) Returns a response with the specified HTTP status code with an optional message.
sitemap( ) Function containing sitemap logic for a given URL prefix.
afterRouter( )

afterRouter( )

Registers a hook that is called after a router.

export function <prefix of router>_afterRouter(request: WixRouterRequest,
  response: WixRouterResponse): Promise<WixRouterResponse>

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 afterRouter() function is not a function that you call from your code. You define the function in a file named routers.js in your site's Backend section. The function is called as described above.

Use this hook 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.

Parameters

request WixRouterRequest The routing request.
response WixRouterResponse The router response.

Return Value

Returns a Promise

On fulfillment WixRouterResponse A router response, either the response received or a new response.

Examples

After router stub

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

Ly8gSW4gcm91dGVycy5qcwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX2FmdGVyUm91dGVyKHJlcXVlc3QsIHJlc3BvbnNlKSB7CiAgLy8gYWZ0ZXIgcm91dGVyIGNvZGUgLi4uCn0K
// In routers.js

export function myRouter_afterRouter(request, response) {
  // after router code ...
}

Basic after router hook

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

Ly8gSW4gcm91dGVycy5qcwoKaW1wb3J0IHtvaywgZm9yYmlkZGVufSBmcm9tICd3aXgtcm91dGVyJzsKCmV4cG9ydCBmdW5jdGlvbiBteVJvdXRlcl9hZnRlclJvdXRlcihyZXF1ZXN0LCByZXNwb25zZSkgewogIGlmIChzb21lX2NvbmRpdGlvbikKICAgIHJldHVybiBvaygiZGlmZmVyZW50LXBhZ2UiLCByZXNwb25zZS5kYXRhLCByZXNwb25zZS5oZWFkKTsKICBlbHNlIGlmIChvdGhlcl9jb25kaXRpb24pCiAgICByZXR1cm4gcmVzcG9uc2U7CiAgZWxzZQogICAgcmV0dXJuIGZvcmJpZGRlbigpOwp9Cg==
// In routers.js

import {ok, forbidden} from 'wix-router';

export function myRouter_afterRouter(request, response) {
  if (some_condition)
    return ok("different-page", response.data, response.head);
  else if (other_condition)
    return response;
  else
    return forbidden();
}

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 diplaying pictures in that orientation.

Ly8gSW4gcm91dGVycy5qcwoKaW1wb3J0IHtva30gZnJvbSAnd2l4LXJvdXRlcic7CgpleHBvcnQgZnVuY3Rpb24gbXlSb3V0ZXJfYWZ0ZXJSb3V0ZXIocmVxdWVzdCwgcmVzcG9uc2UpIHsKICBpZihyZXNwb25zZS5zdGF0dXMgPT09IDIwMCAmJiByZXNwb25zZS5wYWdlID09PSAiaG9yaXpvbnRhbC1waWMiKSB7CiAgICBpZihyZXNwb25zZS5kYXRhLnBpY3R1cmUub3JpZW50YXRpb24gPT09ICJ2ZXJ0aWNhbCIpCiAgICAgIHJldHVybiBvaygidmVydGljYWwtcGljIiwgcmVzcG9uc2UuZGF0YSwgcmVzcG9uc2UuaGVhZCk7CiAgICBlbHNlCiAgICAgIHJldHVybiByZXNwb25zZTsKICB9CgogIHJldHVybiByZXNwb25zZTsKfQo=
// In routers.js

import {ok} from 'wix-router';

export function myRouter_afterRouter(request, response) {
  if(response.status === 200 && response.page === "horizontal-pic") {
    if(response.data.picture.orientation === "vertical")
      return ok("vertical-pic", response.data, response.head);
    else
      return response;
  }

  return response;
}

See Also

ok(), forbidden(), notFound(), redirect(), sendStatus()

afterSitemap( )

afterSitemap( )

Registers a hook that is called after a sitemap is created.

export function <prefix of router>_afterSitemap(
  sitemapRequest: WixRouterSitemapRequest,
  sitemapEntries: WixRouterSitemapEntry[]):
  Promise<WixRouterSitemapEntry[]>

Description

The afterSitemap hook is a data binding router hook that is triggered after a sitemap is created for the specified router.

The afterSitemap() function is not a function that you call from your code. You define the function in a file named routers.js in your site's Backend section. The function is called as described above.

Use this hook to revise the list of pages in your sitemap. For example, you can add a search page's information to your sitemap.

The function returns an array of WixRouterSitemapEntry objects. Typically the returned array is a modified version of the one the function received.

If the function does not return a WixRouterSitemapEntry array, the array received as the sitemapEntries parameter acts as the router's effective sitemap entries.

Parameters

request WixRouterSitemapRequest The sitemap request.
sitemapEntries WixRouterSitemapEntry[ ] The generated sitemap entries.

Return Value

Returns a Promise

On fulfillment WixRouterSitemapEntry[ ] A sitemapEntries array.

Examples

After sitemap stub

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

Ly8gSW4gcm91dGVycy5qcwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX2FmdGVyU2l0ZW1hcChzaXRlbWFwUmVxdWVzdCwgc2l0ZW1hcEVudHJpZXMpIHsKICAvLyBhZnRlciBzaXRlbWFwIGNvZGUgLi4uCn0K
// In routers.js

export function myRouter_afterSitemap(sitemapRequest, sitemapEntries) {
  // after sitemap code ...
}

Add a search page to the sitemap

This example creates an after sitemap hook on the myRouter prefix that adds a search page to the sitemap. Note that the sitemap function only publishes a URL for the search page. To actually implement a page at this URL you can use a beforeRouter() hook to check for the request for the search page URL and route the user to a search page.

Ly8gSW4gcm91dGVycy5qcwoKaW1wb3J0IHtXaXhSb3V0ZXJTaXRlbWFwRW50cnl9IGZyb20gJ3dpeC1yb3V0ZXInOwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX2FmdGVyU2l0ZW1hcChzaXRlbWFwUmVxdWVzdCwgc2l0ZW1hcEVudHJpZXMpIHsKICBsZXQgcHJlZml4ID0gc2l0ZW1hcFJlcXVlc3QucHJlZml4OwogIGxldCBlbnRyeSA9IG5ldyBXaXhSb3V0ZXJTaXRlbWFwRW50cnkoYCR7cHJlZml4fS9zZWFyY2hgKTsKICBzaXRlbWFwRW50cmllcy5wdXNoKGVudHJ5KTsKICByZXR1cm4gc2l0ZW1hcEVudHJpZXM7Cn0K
// In routers.js

import {WixRouterSitemapEntry} from 'wix-router';

export function myRouter_afterSitemap(sitemapRequest, sitemapEntries) {
  let prefix = sitemapRequest.prefix;
  let entry = new WixRouterSitemapEntry(`${prefix}/search`);
  sitemapEntries.push(entry);
  return sitemapEntries;
}
beforeRouter( )

beforeRouter( )

Registers a hook that is called before a router.

export function <prefix of router>_beforeRouter(request: WixRouterRequest):
  Promise<WixRouterResponse>

Description

The beforeRouter hook is a data binding router hook that is triggered before the router with the specified prefix has bound the data to the page.

The beforeRouter() function is not a function that you call from your code. You define the function in a file named routers.js in your site's Backend section. The function is called as described above.

Use this hook to route requests to a different page or return an error response. For example, you can check who is requesting the page and then decide based on the user's role whether to let the router continue to the next step or to return an error type response code.

The function receives a WixRouterRequest object containing information about the incoming request.

The function returns a WixRouterResponse object that causes the router to continue its routing, or respond with an HTTP response code.

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

Parameters

request WixRouterRequest The routing request.

Return Value

Returns a Promise

On fulfillment WixRouterResponse Which page to display, redirect to, or which HTTP status code to respond with.

Examples

Before router stub

This example creates a before router hook on the myRouter prefix.

Ly8gSW4gcm91dGVycy5qcwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX2JlZm9yZVJvdXRlcihyZXF1ZXN0KSB7CiAgLy8gYmVmb3JlIHJvdXRlciBjb2RlIC4uLgp9Cg==
// In routers.js

export function myRouter_beforeRouter(request) {
  // before router code ...
}

Basic before router hook

This example creates a before router hook on the myRouter prefix that continues with the request if some_condition is true and returns a 403 in all other cases.

Ly8gSW4gcm91dGVycy5qcwoKaW1wb3J0IHtmb3JiaWRkZW4sIG5leHR9IGZyb20gJ3dpeC1yb3V0ZXInOwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX2JlZm9yZVJvdXRlcihyZXF1ZXN0KSB7CiAgaWYgKHNvbWVfY29uZGl0aW9uKQogICAgcmV0dXJuIG5leHQoKTsKICBlbHNlCiAgICByZXR1cm4gZm9yYmlkZGVuKCk7Cn0K
// In routers.js

import {forbidden, next} from 'wix-router';

export function myRouter_beforeRouter(request) {
  if (some_condition)
    return next();
  else
    return forbidden();
}

Before router hook that restricts access based on user role

This example creates a before router hook on the myRouter prefix that continues with the request as long as the path does not begin with "admin". If it does, the hook checks to see if the user is the site owner before continuing.

Ly8gSW4gcm91dGVycy5qcwoKaW1wb3J0IHtmb3JiaWRkZW4sIG5leHR9IGZyb20gJ3dpeC1yb3V0ZXInOwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX2JlZm9yZVJvdXRlcihyZXF1ZXN0KSB7CiAgaWYgKHJlcXVlc3QucGF0aC5sZW5ndGggPiAxICYmIHJlcXVlc3QucGF0aFswXSA9PT0gImFkbWluIikgewogICAgaWYgKHJlcXVlc3QudXNlciAmJiByZXF1ZXN0LnVzZXIucm9sZSA9PSAic2l0ZU93bmVyIikKICAgICAgcmV0dXJuIG5leHQoKTsKICAgIGVsc2UKICAgICAgcmV0dXJuIGZvcmJpZGRlbigpOwogIH0KCiAgcmV0dXJuIG5leHQoKTsKfQo=
// In routers.js

import {forbidden, next} from 'wix-router';

export function myRouter_beforeRouter(request) {
  if (request.path.length > 1 && request.path[0] === "admin") {
    if (request.user && request.user.role == "siteOwner")
      return next();
    else
      return forbidden();
  }

  return next();
}

See Also

next(), forbidden(), notFound(), redirect(), sendStatus()

customizeQuery( )

customizeQuery( )

Registers a hook that is called after a route is resolved by the data binding router, but before the wix-data query is executed.

export function <prefix of router>_customizeQuery(
  request: WixRouterRequest,
  route: string,
  query: WixDataQuery):
  WixDataQuery

Description

The customizeQuery hook is a data binding router hook that is triggered before the data query is executed for the pages in the specified router.

The customizeQuery() function is not a function that you call from your code. You define the function in a file named routers.js in your site's Backend section. The function is called as described above.

Use this hook to further refine or change the query that will determine what data is bound to your page's dataset. For example, you can filter the query to only return items that have a status field set to "active".

The function returns a WixDataQuery object. Typically the returned query is a modified version of the one the function received.

The customizeQuery() hook is triggered when using dynamic pages, but not when you code your own router.

Parameters

request WixRouterRequest The routing request.
route String The resolved router URL pattern.
query WixDataQuery The wix-data query.

Return Value

WixDataQuery A wix-data query.

Examples

Customize query stub

This example creates a customize query hook on the myRouter prefix.

Ly8gSW4gcm91dGVycy5qcwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX2N1c3RvbWl6ZVF1ZXJ5KHJlcXVlc3QsIHJvdXRlLCBxdWVyeSkgewogIC8vIGN1c3RvbWl6ZSBxdWVyeSBjb2RlIC4uLgp9Cg==
// In routers.js

export function myRouter_customizeQuery(request, route, query) {
  // customize query code ...
}

Filter query to return on active users

This example creates a customize query hook on the myRouter prefix that filters the query for a certain route to only query for active users.

Ly8gSW4gcm91dGVycy5qcwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX2N1c3RvbWl6ZVF1ZXJ5KHJlcXVlc3QsIHJvdXRlLCBxdWVyeSkgewogIGlmIChyb3V0ZSA9PT0gIi91c2Vycy97bmFtZX0iKQogICAgcmV0dXJuIHF1ZXJ5LmVxKCJzdGF0dXMiLCAiYWN0aXZlIik7CiAgZWxzZQogICAgcmV0dXJuIHF1ZXJ5Owp9Cg==
// In routers.js

export function myRouter_customizeQuery(request, route, query) {
  if (route === "/users/{name}")
    return query.eq("status", "active");
  else
    return query;
}
forbidden( )

forbidden( )

Returns a response with a status code 403 (Forbidden) and instructs the router to show a 403 page.

import {forbidden} from "wix-router";
function forbidden([message: string]): Promise<WixRouterResponse>

Description

The forbidden() function is used in the router(), beforeRouter(), and afterRouter() hooks to indicate that the requested page is forbidden. Optionally, you can pass a message that otherwise defaults to 'Forbidden'.

Parameters

message (optional) String The message to show.

Return Value

Returns a Promise

On fulfillment WixRouterResponse A response object with HTTP status code 403.

Examples

Create a forbidden response

aW1wb3J0IHtmb3JiaWRkZW59IGZyb20gJ3dpeC1yb3V0ZXInOwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIHJldHVybiBmb3JiaWRkZW4oKTsKfQo=
import {forbidden} from 'wix-router';

export function myRouter_Router(request) {

  return forbidden();
}

See Also

router( ), beforeRouter( ), afterRouter( )

next( )

next( )

Returns a response that instructs the router to continue.

import {next} from "wix-router";
function next(): Promise<WixRouterResponse>

Description

The next() function is used in the beforeRouter() hook to indicate the hook has completed and the routing should continue to the data binding router.

Return Value

Returns a Promise

On fulfillment WixRouterResponse The router response.

Examples

Create a response to continue routing

aW1wb3J0IHtuZXh0fSBmcm9tICd3aXgtcm91dGVyJzsKCmV4cG9ydCBmdW5jdGlvbiBteVJvdXRlcl9iZWZvcmVSb3V0ZXIocmVxdWVzdCkgewoKICByZXR1cm4gbmV4dCgpOwp9Cg==
import {next} from 'wix-router';

export function myRouter_beforeRouter(request) {

  return next();
}

See Also

beforeRouter( )

notFound( )

notFound( )

Returns a response with a status code 404 (Not Found) and instructs the router to show a 404 page.

import {notFound} from "wix-router";
function notFound([message: string]): Promise<WixRouterResponse>

Description

The notFound() function is used in the router(), beforeRouter(), and afterRouter() hooks to indicate that the requested page was not found. Optionally, you can pass a message that otherwise defaults to "Not Found".

Parameters

message (optional) String The message to show.

Return Value

Returns a Promise

On fulfillment WixRouterResponse A response object with HTTP status code 404.

Examples

Create a not found response

aW1wb3J0IHtub3RGb3VuZH0gZnJvbSAnd2l4LXJvdXRlcic7CgpleHBvcnQgZnVuY3Rpb24gbXlSb3V0ZXJfUm91dGVyKHJlcXVlc3QpIHsKCiAgcmV0dXJuIG5vdEZvdW5kKCk7Cn0K
import {notFound} from 'wix-router';

export function myRouter_Router(request) {

  return notFound();
}

See Also

router( ), beforeRouter( ), afterRouter( )

ok( )

ok( )

Returns a response with a status code 200 (OK) and instructs the router to show the selected page.

import {ok} from "wix-router";
function ok(page: string, [routerReturnedData: Object], [head: HeadOptions]):
  Promise<WixRouterResponse>

Description

The ok() function is used in the router() and afterRouter() hooks to continue the routing process to the specified page with a successful HTTP response code. The specified page must be a page found under the given router in the Site Structure sidebar. For example, if you want to route to myRouter-page from the myRouter_Router() function, myRouter-page must be in the MyRouter Pages (Router) section of your site's Site Structure sidebar. To learn more about router pages in the Site Structure sidebar, see Working with the Site Structure Sidebar.

Optionally, you can pass a data object to be used by the page routed to and header options for the page. The page routed to accesses the data passed to it using the getRouterData() function of wix-window. with optional data and HTML header.

Parameters

Page String |
String[ ]
A router page or an array of page fallbacks.
routerReturnedData (optional) Object A data object.
head (optional) HeadOptions HTML head members of the response.

Return Value

Returns a Promise

On fulfillment WixRouterResponse The router response object.

Examples

Create an okay response

aW1wb3J0IHtva30gZnJvbSAnd2l4LXJvdXRlcic7CgpleHBvcnQgZnVuY3Rpb24gbXlSb3V0ZXJfUm91dGVyKHJlcXVlc3QpIHsKCiAgcmV0dXJuIG9rKCJyb3V0ZXItcGFnZSIpOwp9Cg==
import {ok} from 'wix-router';

export function myRouter_Router(request) {

  return ok("router-page");
}

Create an okay response with data and head options

aW1wb3J0IHtva30gZnJvbSAnd2l4LXJvdXRlcic7CgpleHBvcnQgZnVuY3Rpb24gbXlSb3V0ZXJfUm91dGVyKHJlcXVlc3QpIHsKCiAgcmV0dXJuIG9rKCJyb3V0ZXItcGFnZSIsIGRhdGFPYmosIGhlYWRPcHRzKTsKfQo=
import {ok} from 'wix-router';

export function myRouter_Router(request) {

  return ok("router-page", dataObj, headOpts);
}

Create an okay response with data and head options

aW1wb3J0IHtva30gZnJvbSAnd2l4LXJvdXRlcic7CgpleHBvcnQgZnVuY3Rpb24gbXlSb3V0ZXJfUm91dGVyKHJlcXVlc3QpIHsKCiAgbGV0IGRhdGFPYmogPSB7CiAgICAiZmllbGQxIjogInZhbHVlMSIsCiAgICAiZmllbGQyIjogInZhbHVlMiIKICB9OwoKICBsZXQgaGVhZE9wdGlvbnMgPSB7CiAgICAidGl0bGUiOiAiQSBwYWdlIHRpdGxlIiwKICAgICJkZXNjcmlwdGlvbiI6ICJBIHBhZ2UgZGVzY3JpcHRpb24iLAogICAgImtleXdvcmRzIjogIndpeCBjb2RlIGV4YW1wbGUiLAogICAgIm5vSW5kZXgiOiB0cnVlLAogICAgIm1ldGFUYWdzIjogewogICAgICAiY29weXJpZ2h0IjogIldpeC5jb20iLAogICAgICAiQ2xhc3NpZmljYXRpb24iOiAiRXhhbXBsZSIsCiAgICAgICJvZzp0aXRsZSI6ICJUaGUgUm9jayIsCiAgICAgICJvZzppbWFnZSI6ICJodHRwOi8vaWEubWVkaWEtaW1kYi5jb20vcm9jay5qcGciCiAgICB9CiAgfTsKCiAgcmV0dXJuIG9rKCJyb3V0ZXItcGFnZSIsIGRhdGFPYmosIGhlYWRPcHRpb25zKTsKfQo=
import {ok} from 'wix-router';

export function myRouter_Router(request) {

  let dataObj = {
    "field1": "value1",
    "field2": "value2"
  };

  let headOptions = {
    "title": "A page title",
    "description": "A page description",
    "keywords": "wix code example",
    "noIndex": true,
    "metaTags": {
      "copyright": "Wix.com",
      "Classification": "Example",
      "og:title": "The Rock",
      "og:image": "http://ia.media-imdb.com/rock.jpg"
    }
  };

  return ok("router-page", dataObj, headOptions);
}

See Also

router( ), afterRouter( )

redirect( )

redirect( )

Returns a response with a status code of 301 (Moved Permanently) or 302 (Found) and instructs the router to redirect to the given URL.

import {redirect} from "wix-router";
function redirect(url: string, [statusCode: string]):
  Promise<WixRouterResponse>

Description

The redirect() function is used in the router(), beforeRouter(), and afterRouter() hooks to redirect to a page that is not the requested page.

Optionally, you can pass a status code of "301" or "302" using the statusCode parameter. If any other status code is passed or no status code is passed at all, the status code defaults to "302".

Parameters

url String The url to redirect to.
statusCode (optional) String The status code to use.

Return Value

Returns a Promise

On fulfillment WixRouterResponse The redirect response object.

Examples

Create a redirect response

aW1wb3J0IHtyZWRpcmVjdH0gZnJvbSAnd2l4LXJvdXRlcic7CgpleHBvcnQgZnVuY3Rpb24gbXlSb3V0ZXJfUm91dGVyKHJlcXVlc3QpIHsKCiAgcmV0dXJuIHJlZGlyZWN0KCJodHRwOi8vbXlvdGhlcnNpdGUuY29tIik7Cn0K
import {redirect} from 'wix-router';

export function myRouter_Router(request) {

  return redirect("http://myothersite.com");
}

Create a redirect response with HTTP code 301

aW1wb3J0IHtyZWRpcmVjdH0gZnJvbSAnd2l4LXJvdXRlcic7CgpleHBvcnQgZnVuY3Rpb24gbXlSb3V0ZXJfUm91dGVyKHJlcXVlc3QpIHsKCiAgcmV0dXJuIHJlZGlyZWN0KCJodHRwOi8vbXlvdGhlcnNpdGUuY29tIiwgIjMwMSIpOwp9Cg==
import {redirect} from 'wix-router';

export function myRouter_Router(request) {

  return redirect("http://myothersite.com", "301");
}

See Also

router( ), beforeRouter( ), afterRouter( )

router( )

router( )

Function containing routing logic for a given URL prefix.

export function <router prefix>_router(request: WixRouterRequest):
  Promise<WixRouterResponse>

Description

The router() function handles all incoming requests with the specified URL prefix. It is responsible for deciding which page to diplay and what data to pass to the page, where to redirect to, or which HTTP status code to respond with.

The router() function is not a function that you call from your code. You define the function in a file named routers.js in your site's Backend section. The function is called when your users browse to a URL that is handled by the router with the specified prefix.

The function receives a WixRouterRequest object containing information about the incoming request.

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.

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

Data that is returned as part of the WixRouterResponse is accessed in the page code of the page that was routed to using the getRouterData() function of wix-window.

You can also set members of the HTML head of the response using the response's HeadOptions parameter.

Parameters

request WixRouterRequest The routing request.

Return Value

Returns a Promise

On fulfillment WixRouterResponse Which page to display, redirect to, or which HTTP status code to respond with.

Examples

Router stub

This example creates a router on the myRouter prefix.

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CiAgLy8gcm91dGVyIGNvZGUgLi4uCn0K
export function myRouter_Router(request) {
  // router code ...
}

Basic router

This example creates a router on the myRouter prefix that shows a page when the path begins with the word "good" and returns a 404 in all other cases.

aW1wb3J0IHtvaywgbm90Rm91bmR9IGZyb20gIndpeC1yb3V0ZXIiOwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIC8vIFVSTCBsb29rcyBsaWtlOgogIC8vIGh0dHBzOi8vbXlzaXRlLmNvbS9teVJvdXRlci9nb29kCiAgLy8gb3I6CiAgLy8gaHR0cHM6Ly91c2VyLndpeHNpdGUuY29tL215c2l0ZS9teVJvdXRlci9nb29kCiAgY29uc3Qgc3RhdHVzID0gcmVxdWVzdC5wYXRoWzBdOwoKICBpZihzdGF0dXMgPT09ICJnb29kIikgewogICAgLy8gU2hvdyBhIHBhZ2UKICAgIHJldHVybiBvaygibXlSb3V0ZXItcGFnZSIpOwogIH0KICBlbHNlIHsKICAgIC8vIFJldHVybiA0MDQKICAgIHJldHVybiBub3RGb3VuZCgpOwogIH0KfQo=
import {ok, notFound} from "wix-router";

export function myRouter_Router(request) {

  // URL looks like:
  // https://mysite.com/myRouter/good
  // or:
  // https://user.wixsite.com/mysite/myRouter/good
  const status = request.path[0];

  if(status === "good") {
    // Show a page
    return ok("myRouter-page");
  }
  else {
    // Return 404
    return notFound();
  }
}

A router with static data

This example creates a router on the myRouter prefix that shows a page when the path begins with a name that is found in a static data object. The appropriate data and HTML head options are also passed in the response. If the requested item does not exist, a 404 "Not Found" response is returned.

aW1wb3J0IHtvaywgbm90Rm91bmR9IGZyb20gIndpeC1yb3V0ZXIiOwoKLy8gU2FtcGxlIGRhdGEKY29uc3QgcGVvcGxlRGF0YSA9IHsKICAgIkFzaCI6IHsKICAgICB0aXRsZTogICAgICJBc2ggU3Rvd2UiLAogICAgIGltYWdlU2l0ZTogImh0dHA6Ly9zdGF0aWMud2l4c3RhdGljLmNvbS9tZWRpYS8iLAogICAgIGltYWdlOiAgICAgImI4ZjM4M2UwZmUyYjQ3OGVhOTEzNjJiNzA3ZWYyNjdiLmpwZyIKICAgIH0sCiAgICJBaWRlbiI6IHsKICAgIHRpdGxlOiAgICAgICJBaWRlbiBKb2huc29uIiwKICAgIGltYWdlU2l0ZTogICJodHRwOi8vc3RhdGljLndpeHN0YXRpYy5jb20vbWVkaWEvIiwKICAgIGltYWdlOiAgICAgICJjYTNjN2FjNTQyN2U0MzkyOGFhNWYzZjQ0M2FlMjE2My5qcGciCiAgIH0sCiAgICJKZXNzIjogewogICAgdGl0bGU6ICAgICAgIkplc3MgV2hpdGUiLAogICAgaW1hZ2VTaXRlOiAgImh0dHA6Ly9zdGF0aWMud2l4c3RhdGljLmNvbS9tZWRpYS8iLAogICAgaW1hZ2U6ICAgICAgIjE0N2ZlNmYzN2ZlMjRlODM5NzdiNDEyNGU0MWI2ZDNkLmpwZyIKICAgfSwKICAgIk1vcmdhbiI6IHsKICAgIHRpdGxlOiAgICAgICJNb3JnYW4gSmFtZXMiLAogICAgaW1hZ2VTaXRlOiAgImh0dHA6Ly9zdGF0aWMud2l4c3RhdGljLmNvbS9tZWRpYS8iLAogICAgaW1hZ2U6ICAgICAgIjU5ZTFmMmY0ZGJiYzRmN2M5ZDZlNjZlM2UxMjVkODMwLmpwZyIKICAgfQp9OwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIC8vIEdldCBpdGVtIG5hbWUgZnJvbSBVUkwgcmVxdWVzdAogIC8vIFVSTCBsb29rcyBsaWtlOgogIC8vIGh0dHBzOi8vbXlzaXRlLmNvbS9teVJvdXRlci9Nb3JnYW4KICAvLyBvcjoKICAvLyBodHRwczovL3VzZXIud2l4c2l0ZS5jb20vbXlzaXRlL215Um91dGVyL01vcmdhbgogIGNvbnN0IG5hbWUgPSByZXF1ZXN0LnBhdGhbMF07CgogIC8vIEdldCB0aGUgaXRlbSBkYXRhIGJ5IG5hbWUKICBjb25zdCBkYXRhID0gcGVvcGxlRGF0YVtuYW1lXTsKCiAgLy8gSWYgdGhlIGl0ZW0gZXhpc3RzCiAgaWYoZGF0YSkgewoKICAgIC8vRGVmaW5lIFNFTyB0YWdzCiAgICBjb25zdCBzZW9EYXRhID0gewogICAgICB0aXRsZTogZGF0YS50aXRsZSwKICAgICAgZGVzY3JpcHRpb246IGBUaGlzIGlzIGEgZGVzY3JpcHRpb24gb2YgJHtkYXRhLnRpdGxlfSBwYWdlYCwKICAgICAgbm9JbmRleDogZmFsc2UsCiAgICAgIG1ldGFUYWdzOiB7CiAgICAgICAgIm9nOnRpdGxlIjogZGF0YS50aXRsZSwKICAgICAgICAib2c6aW1hZ2UiOiBkYXRhLmltYWdlU2l0ZSArIGRhdGEuaW1hZ2UKICAgICAgfQogICAgfTsKCiAgICAvLyBSZW5kZXIgaXRlbSBwYWdlLCBwYXNzaW5nIGRhdGEgYW5kIGhlYWQgaW5mbwogICAgcmV0dXJuIG9rKCJteVJvdXRlci1wYWdlIiwgZGF0YSwgc2VvRGF0YSk7CiAgfQoKICAvLyBSZXR1cm4gNDA0IGlmIHRoZSBpdGVtIGlzIG5vdCBmb3VuZAogIHJldHVybiBub3RGb3VuZCgpOwp9Cg==
import {ok, notFound} from "wix-router";

// Sample data
const peopleData = {
   "Ash": {
     title:     "Ash Stowe",
     imageSite: "http://static.wixstatic.com/media/",
     image:     "b8f383e0fe2b478ea91362b707ef267b.jpg"
    },
   "Aiden": {
    title:      "Aiden Johnson",
    imageSite:  "http://static.wixstatic.com/media/",
    image:      "ca3c7ac5427e43928aa5f3f443ae2163.jpg"
   },
   "Jess": {
    title:      "Jess White",
    imageSite:  "http://static.wixstatic.com/media/",
    image:      "147fe6f37fe24e83977b4124e41b6d3d.jpg"
   },
   "Morgan": {
    title:      "Morgan James",
    imageSite:  "http://static.wixstatic.com/media/",
    image:      "59e1f2f4dbbc4f7c9d6e66e3e125d830.jpg"
   }
};

export function myRouter_Router(request) {

  // Get item name from URL request
  // URL looks like:
  // https://mysite.com/myRouter/Morgan
  // or:
  // https://user.wixsite.com/mysite/myRouter/Morgan
  const name = request.path[0];

  // Get the item data by name
  const data = peopleData[name];

  // If the item exists
  if(data) {

    //Define SEO tags
    const seoData = {
      title: data.title,
      description: `This is a description of ${data.title} page`,
      noIndex: false,
      metaTags: {
        "og:title": data.title,
        "og:image": data.imageSite + data.image
      }
    };

    // Render item page, passing data and head info
    return ok("myRouter-page", data, seoData);
  }

  // Return 404 if the item is not found
  return notFound();
}

A router with data from a collection

This example creates a router on the myRouter prefix that shows an index page when the path is empty. If the path contains an item title, that title is looked up in a collection. If the item is found in the collection, it shows an item page and passes the item data. If the item is not found in the collection, a 404 "Not Found" response is returned.

aW1wb3J0IHdpeERhdGEgZnJvbSAnd2l4LWRhdGEnOwppbXBvcnQge29rLCBub3RGb3VuZH0gZnJvbSAnd2l4LXJvdXRlcic7CgpleHBvcnQgZnVuY3Rpb24gbXlSb3V0ZXJfcm91dGVyKHJlcXVlc3QpIHsKCiAgLy8gRW1wdHkgcGF0aCAtIHNob3cgaW5kZXggcGFnZQogIC8vIFVSTCBsb29rcyBsaWtlOgogIC8vIGh0dHBzOi8vbXlzaXRlLmNvbS9teVJvdXRlcgogIC8vIG9yOgogIC8vIGh0dHBzOi8vdXNlci53aXhzaXRlLmNvbS9teXNpdGUvbXlSb3V0ZXIKICBpZiAocmVxdWVzdC5wYXRoLmxlbmd0aCA8IDEpIHsKICAgIHJldHVybiBvaygiaW5kZXgtcGFnZSIpOwogIH0KCiAgLy8gUGF0aCB3aXRoIGl0ZW0gLSBzaG93IGl0ZW0gcGFnZSB3aXRoIGRhdGEgZnJvbSBjb2xsZWN0aW9uCiAgLy8gVVJMIGxvb2tzIGxpa2U6CiAgLy8gaHR0cHM6Ly9teXNpdGUuY29tL215Um91dGVyL2l0ZW1UaXRsZQogIC8vIG9yOgogIC8vIGh0dHBzOi8vdXNlci53aXhzaXRlLmNvbS9teXNpdGUvbXlSb3V0ZXIvaXRlbVRpdGxlCiAgLy8gUmV0cmlldmUgZGF0YSBmcm9tIGNvbGxlY3Rpb24KICByZXR1cm4gd2l4RGF0YS5xdWVyeSgibXlDb2xsZWN0aW9uIikKICAgIC5lcSgidGl0bGUiLCByZXF1ZXN0LnBhdGhbMF0pCiAgICAuZmluZCgpCiAgICAudGhlbiggKHF1ZXJ5UmVzdWx0KSA9PiB7CiAgICAgIGlmIChxdWVyeVJlc3VsdC5sZW5ndGggPiAwKSB7CiAgICAgICAgcmV0dXJuIG9rKCJpdGVtLXBhZ2UiLCBxdWVyeVJlc3VsdC5pdGVtc1swXSk7CiAgICAgIH0KICAgICAgcmV0dXJuIG5vdEZvdW5kKCk7CiAgICB9ICk7Cn0K
import wixData from 'wix-data';
import {ok, notFound} from 'wix-router';

export function myRouter_router(request) {

  // Empty path - show index page
  // URL looks like:
  // https://mysite.com/myRouter
  // or:
  // https://user.wixsite.com/mysite/myRouter
  if (request.path.length < 1) {
    return ok("index-page");
  }

  // Path with item - show item page with data from collection
  // URL looks like:
  // https://mysite.com/myRouter/itemTitle
  // or:
  // https://user.wixsite.com/mysite/myRouter/itemTitle
  // Retrieve data from collection
  return wixData.query("myCollection")
    .eq("title", request.path[0])
    .find()
    .then( (queryResult) => {
      if (queryResult.length > 0) {
        return ok("item-page", queryResult.items[0]);
      }
      return notFound();
    } );
}

See Also

ok(), forbidden(), notFound(), redirect(), sendStatus()

sendStatus( )

sendStatus( )

Returns a response with the specified HTTP status code with an optional message.

import {sendStatus} from "wix-router";
function sendStatus(statusCode: string, [message: string]):
  Promise<WixRouterResponse>

Description

The sendStatus() function is used in the router(), beforeRouter(), and afterRouter() hooks to return a specified response. Optionally, you can pass a message.

Parameters

statusCode String The HTTP status code to return.
message (optional) String The message to write as the HTTP body.

Return Value

Returns a Promise

On fulfillment WixRouterResponse A response object with the specified HTTP status.

Examples

Create a response with a specified status code

aW1wb3J0IHtzZW5kU3RhdHVzfSBmcm9tICd3aXgtcm91dGVyJzsKCmV4cG9ydCBmdW5jdGlvbiBteVJvdXRlcl9Sb3V0ZXIocmVxdWVzdCkgewoKICByZXR1cm4gc2VuZFN0YXR1cygiNDE4Iik7Cn0K
import {sendStatus} from 'wix-router';

export function myRouter_Router(request) {

  return sendStatus("418");
}

Create a response with a specified status code and message

aW1wb3J0IHtzZW5kU3RhdHVzfSBmcm9tICd3aXgtcm91dGVyJzsKCmV4cG9ydCBmdW5jdGlvbiBteVJvdXRlcl9Sb3V0ZXIocmVxdWVzdCkgewoKICByZXR1cm4gc2VuZFN0YXR1cygiNDE4IiwgIk1lc3NhZ2UiKTsKfQo=
import {sendStatus} from 'wix-router';

export function myRouter_Router(request) {

  return sendStatus("418", "Message");
}

See Also

router( ), beforeRouter( ), afterRouter( )

sitemap( )

sitemap( )

Function containing sitemap logic for a given URL prefix.

export function <prefix of router>_sitemap(
  sitemapRequest: WixRouterSitemapRequest):
  Promise<WixRouterSitemapEntry[]>

Description

The sitemap() function handles sitemap requests for pages with the specified URL prefix. Use this function to make sure search engines can find the links to your router's pages.

The sitemap() function is not a function that you call from your code. You define the function in a file named routers.js in your site's Backend section. The function is called when a sitemap request is received for the router with the specified prefix.

The function receives a WixRouterSitemapRequest object containing information about the incoming sitemap request.

The function returns an array of WixRouterSitemapEntry objects each of which includes information about a page, such as its URL, title, and name.

To create sitemap entry objects, first import WixRouterSitemapEntry from wix-router:

IGltcG9ydCB7V2l4Um91dGVyU2l0ZW1hcEVudHJ5fSBmcm9tICJ3aXgtcm91dGVyIjs=
 import {WixRouterSitemapEntry} from "wix-router";

The sitemap() function is also used to populate the items preview widget, allowing you to switch between URLs in preview mode.

Parameters

request WixRouterSitemapRequest The sitemap request.

Return Value

Returns a Promise

On fulfillment WixRouterSitemapEntry[ ] An array of sitemap entries.

Examples

Sitemap stub

This example creates a sitemap on the myRouter prefix.

Ly8gSW4gcm91dGVycy5qcwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1NpdGVNYXAoc2l0ZW1hcFJlcXVlc3QpIHsKICAvLyBzaXRlbWFwIGNvZGUgLi4uCn0K
// In routers.js

export function myRouter_SiteMap(sitemapRequest) {
  // sitemap code ...
}

Basic sitemap

This example creates a sitemap on the myRouter prefix by looping through a data object and creating a sitemap entry for each object.

Ly8gSW4gcm91dGVycy5qcwoKaW1wb3J0IHtXaXhSb3V0ZXJTaXRlbWFwRW50cnl9IGZyb20gJ3dpeC1yb3V0ZXInOwoKZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1NpdGVNYXAoc2l0ZW1hcFJlcXVlc3QpIHsKCglsZXQgc2l0ZU1hcEVudHJpZXMgPSBbXTsKCiAgLy8gQ3JlYXRlIGEgc2l0ZW1hcCBlbnRyeSBmb3IgZWFjaCBpdGVtCglmb3IgKHZhciBrZXkgaW4gbXlEYXRhKSB7CiAgICBsZXQgZW50cnkgPSBuZXcgV2l4Um91dGVyU2l0ZW1hcEVudHJ5KGtleSk7CiAgICBlbnRyeS5wYWdlTmFtZSA9ICJteVJvdXRlci1wYWdlIjsKICAgIGVudHJ5LnVybCA9ICIvbXlSb3V0ZXIvIiArIGtleTsKICAgIGVudHJ5LnRpdGxlID0gbXlEYXRhW2tleV0udGl0bGU7CiAgICBzaXRlTWFwRW50cmllcy5wdXNoKGVudHJ5KTsKCX0KCiAgLy8gUmV0dXJuIHRoZSBzaXRlbWFwIGVudHJpZXMKICByZXR1cm4gc2l0ZU1hcEVudHJpZXM7Cn0K
// In routers.js

import {WixRouterSitemapEntry} from 'wix-router';

export function myRouter_SiteMap(sitemapRequest) {

	let siteMapEntries = [];

  // Create a sitemap entry for each item
	for (var key in myData) {
    let entry = new WixRouterSitemapEntry(key);
    entry.pageName = "myRouter-page";
    entry.url = "/myRouter/" + key;
    entry.title = myData[key].title;
    siteMapEntries.push(entry);
	}

  // Return the sitemap entries
  return siteMapEntries;
}

A sitemap with static data

This example creates a sitemap on the myRouter prefix by mapping an object's keys to an arrray of corresponding sitemap entries.

Ly8gSW4gcm91dGVycy5qcwoKaW1wb3J0IHtXaXhSb3V0ZXJTaXRlbWFwRW50cnl9IGZyb20gJ3dpeC1yb3V0ZXInOwoKLy8gU2FtcGxlIGRhdGEKY29uc3QgcGVvcGxlRGF0YSA9IHsKICAgIkFzaCI6IHsKICAgICB0aXRsZTogICAgICJBc2ggU3Rvd2UiLAogICAgIGltYWdlU2l0ZTogImh0dHA6Ly9zdGF0aWMud2l4c3RhdGljLmNvbS9tZWRpYS8iLAogICAgIGltYWdlOiAgICAgImI4ZjM4M2UwZmUyYjQ3OGVhOTEzNjJiNzA3ZWYyNjdiLmpwZyIKICAgIH0sCiAgICJBaWRlbiI6IHsKICAgIHRpdGxlOiAgICAgICJBaWRlbiBKb2huc29uIiwKICAgIGltYWdlU2l0ZTogICJodHRwOi8vc3RhdGljLndpeHN0YXRpYy5jb20vbWVkaWEvIiwKICAgIGltYWdlOiAgICAgICJjYTNjN2FjNTQyN2U0MzkyOGFhNWYzZjQ0M2FlMjE2My5qcGciCiAgIH0sCiAgICJKZXNzIjogewogICAgdGl0bGU6ICAgICAgIkplc3MgV2hpdGUiLAogICAgaW1hZ2VTaXRlOiAgImh0dHA6Ly9zdGF0aWMud2l4c3RhdGljLmNvbS9tZWRpYS8iLAogICAgaW1hZ2U6ICAgICAgIjE0N2ZlNmYzN2ZlMjRlODM5NzdiNDEyNGU0MWI2ZDNkLmpwZyIKCiAgIH0sCiAgICJNb3JnYW4iOiB7CiAgICB0aXRsZTogICAgICAiTW9yZ2FuIEphbWVzIiwKICAgIGltYWdlU2l0ZTogICJodHRwOi8vc3RhdGljLndpeHN0YXRpYy5jb20vbWVkaWEvIiwKICAgIGltYWdlOiAgICAgICI1OWUxZjJmNGRiYmM0ZjdjOWQ2ZTY2ZTNlMTI1ZDgzMC5qcGciCiAgIH0KfTsKCmV4cG9ydCBmdW5jdGlvbiBteVJvdXRlcl9TaXRlTWFwKHNpdGVtYXBSZXF1ZXN0KSB7CgogIC8vQ29udmVydCB0aGUgZGF0YSB0byBzaXRlbWFwIGVudHJpZXMKICBjb25zdCBzaXRlTWFwRW50cmllcyA9IE9iamVjdC5rZXlzKHBlb3BsZURhdGEpLm1hcCggKG5hbWUpID0+IHsKICAgIGNvbnN0IGRhdGEgPSBwZW9wbGVEYXRhW25hbWVdOwogICAgY29uc3QgZW50cnkgPSBuZXcgV2l4Um91dGVyU2l0ZW1hcEVudHJ5KG5hbWUpOwogICAgZW50cnkucGFnZU5hbWUgPSAibXlSb3V0ZXItcGFnZSI7CiAgICBlbnRyeS51cmwgPSBgL215Um91dGVyLyR7bmFtZX1gOwogICAgZW50cnkudGl0bGUgPSBkYXRhLnRpdGxlOwogICAgcmV0dXJuIGVudHJ5OwogIH0gKTsKCiAgLy8gUmV0dXJuIHRoZSBzaXRlbWFwIGVudHJpZXMKICByZXR1cm4gc2l0ZU1hcEVudHJpZXM7Cn0K
// In routers.js

import {WixRouterSitemapEntry} from 'wix-router';

// Sample data
const peopleData = {
   "Ash": {
     title:     "Ash Stowe",
     imageSite: "http://static.wixstatic.com/media/",
     image:     "b8f383e0fe2b478ea91362b707ef267b.jpg"
    },
   "Aiden": {
    title:      "Aiden Johnson",
    imageSite:  "http://static.wixstatic.com/media/",
    image:      "ca3c7ac5427e43928aa5f3f443ae2163.jpg"
   },
   "Jess": {
    title:      "Jess White",
    imageSite:  "http://static.wixstatic.com/media/",
    image:      "147fe6f37fe24e83977b4124e41b6d3d.jpg"

   },
   "Morgan": {
    title:      "Morgan James",
    imageSite:  "http://static.wixstatic.com/media/",
    image:      "59e1f2f4dbbc4f7c9d6e66e3e125d830.jpg"
   }
};

export function myRouter_SiteMap(sitemapRequest) {

  //Convert the data to sitemap entries
  const siteMapEntries = Object.keys(peopleData).map( (name) => {
    const data = peopleData[name];
    const entry = new WixRouterSitemapEntry(name);
    entry.pageName = "myRouter-page";
    entry.url = `/myRouter/${name}`;
    entry.title = data.title;
    return entry;
  } );

  // Return the sitemap entries
  return siteMapEntries;
}