Note: This feature is not yet supported in
Wix Blocks.
The Wix Router API allows you to handle certain incoming requests to your site's pages and sitemap and customize the responses.
Some things you might do with the Router API are:
- Control the URL structure of your site.
- Customize your site's sitemap.
- Extend dynamic pages to include subpages that use different data.
- Cache router response data used to display pages.
The API gives you access to the following:
- Requests to certain pages on your site and routing responses for those pages.
- Sitemap requests and responses.
- Hooks to control the flow of the data used to display pages returned by the router or sitemap. Hooks are often used when working with the code for dynamic pages.
To enhance performance, you can implement caching for your router responses using the ok()
function. Caching allows you to temporarily store the function's return value, reducing response times and the need to repeatedly fetch data every time a visitor makes a request on your site. Learn more about router caching.
It's important to note the following points before starting to code:
- Code using the Routers API is stored in the routers.js file in the backend code section the Code sidebar. You shouldn't create this file manually.
- The file is created automatically when you add a router to your site.
To use the router()
and sitemap()
functions in the routers.js file, use the following convention:
To import the other API functions into the routers.js file, use the following syntax:
Get hands-on experience with the Routers API on our Hello Routers example page.
- Router: Part of the code on your site that receives requests for specific URLs and handles which page is
served and what data is included in that page.
- Sitemap: A model of a site's content structure, designed to help both users and search engines navigate the site.
- Data binding router hooks: Functions that run before, during, and after data is included in
the pages that the router and sitemap functions return. Router hooks are often used when working with dynamic pages. The API includes 4 hooks:
- Caching: The process of storing frequently accessed data in a temporary storage location so that it can be retrieved more quickly, improving the performance and speed of your site.