Registers a hook that is called after a router.
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.
The routing request.
The router response.
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Registers a hook that is called after a sitemap is created.
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 the Code File's Backend section of the Velo Sidebar.
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.
The sitemap request.
The generated sitemap entries.
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Registers a hook that is called before a router.
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 router can be a code router or the data binding router that binds data
to a dynamic 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 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 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.
The routing request.
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Registers a hook that is called after a route is resolved by the data binding router, but before the wix-data query is executed.
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 the Code File's Backend section of the Velo Sidebar.
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.
The routing request.
The resolved router URL pattern.
The wix-data query.
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Returns a response with a status code 403 (Forbidden) and instructs the router to show a 403 page.
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'.
The message to show.
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Returns a response that instructs the router to continue.
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.
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Returns a response with a status code 404 (Not Found) and instructs the router to show a 404 page.
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".
The message to show.
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Returns the WixRouterResponse
object with a status code 200 (OK) and instructs the router to show the selected page.
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 Page Code section of the Code 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 Code sidebar.
Optionally, you can pass the routerReturnedData
object to be used by the
page routed to and the head
options object for the page. The page routed to accesses the
data passed to it using the getRouterData()
function of wix-window-frontend
with optional data and HTML header.
For enhanced performance, you can also pass a cache
object in the options
parameter to temporarily store the return value of the ok()
function for a specified duration. This reduces response times and eliminates the need to repeatedly fetch router data. You can set the duration, in seconds, for which you want the return value to be cached. The default is approximately 1 week.
Assign identifiers to caches using the tags
property of the cache
object. Tags allow you to specify cached return values that may later require invalidation due to a significant change in your data. To invalidate router caches, use the invalidateCache()
function from the wix-cache-backend
module. Once invalidated, the return value is re-cached upon the subsequent router page request. Learn more about router caching.
Important:
tags
property is required for caching. Without tags, nothing is cached.invalidateCache()
method from wix-cache-backend is currently in developer preview.
A router page or an array of page fallbacks.
A data object.
HTML head members of the response.
Router caching options.
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Returns a response with a status code of 301 (Moved Permanently) or 302 (Found) and instructs the router to redirect to the given URL.
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"
.
The url to redirect to.
The status code to use.
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Function containing routing logic for a given URL prefix.
The router()
function handles all incoming requests with the specified URL
prefix. It is responsible for deciding which page to display 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 the Code File's Backend section of the Velo Sidebar. 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-frontend
.
You can also set members of the HTML head of the response using the response's
HeadOptions
parameter.
Notes:
The router()
function also runs when you navigate to a router page in the Editor.
In a request URL, spaces are replaced with dashes (-
). For example, a request for /animals/killer whale
or /animals/killer%20whale
will be received in the request as /animals/killer-whale
. Therefore, when searching for the data that matches the incoming request, you need to take special care if the path contains dashes (-
).
The routing request.
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Returns a response with the specified HTTP status code with an optional message.
The sendStatus()
function is used in the router()
, beforeRouter()
,
and afterRouter()
hooks to return a specified response.
Optionally, you can pass a message.
The HTTP status code to return.
The message to write as the HTTP body.
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Function containing sitemap logic for a given URL prefix.
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 the Code File's Backend section of the Velo Sidebar. 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
:
The sitemap()
function is also used to populate the items preview widget,
allowing you to switch between URLs in preview mode.
Note: The sitemap()
function also runs when you navigate to a router page in the Editor.
The sitemap request.
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.
Returns the details of the current site user who is logged in.
Returns an object with the id
and role
of the logged in user
who made the router request.