The functions in the routers.js file are named with the following convention:
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.
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.
You might want to use a router to:
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.When using the wix-router
API you often need to know the prefix of your code router
or dynamic pages. You can find prefixes as follows:
Code router:
The router's prefix is displayed.
Dynamic pages:
Go to in the Page Code's Dynamic Pages section of the Velo Sidebar.
Click the ellipsis icon that appears when you hover over the dynamic page.
Click Settings.
The Page Info tab shows the page URL. The prefix is the first editable section of the URL up until the first forward slash (/).