Wix Editor: Creating a Router

Note This article is specific to the Wix Editor. For sites built with Wix Studio, read Wix Studio: Creating a Router.

Creating a router allows you to take complete control when handling certain incoming requests to your site.

This article takes you through what code you have to write in order to make your router work. To learn more about what a router is and why you would want to create one, see About Routers.

Add a Router

To add a router:

  1. Click on the icon that appears when hovering over the Page Code's Main Pages section  in the Velo Sidebar and choose Add a Router
  2. Enter a URL prefix for your router and click Add & Edit Code. All incoming requests with the specified URL prefix will be sent to your router for handling. 

When adding a router:

  • Your router's router() そして sitemap() functions are added in a routers.js file with sample code for a simple routing scenario. The routers.js file is found in the Code Files's Backend section of the Velo Sidebar.
  • A new section, Router Pages, is created in the Velo Sidebar for your routers. Router pages are grouped together under a title based on the prefix you chose earlier. One router page is created to start with. For example, if you named your router "myRouter", a page named myRouter-page is added under the title MyRouter Pages (Router).

注: モジュールのエクスポート形式によっては routers.js.詳しくは モジュール・エクスポート構文.

Sample Scenario

There are four parts to the sample code that's added to the routers.js file:

  1. について import statement.
  2. The sample data.
  3. について router() 関数である。
  4. について sitemap() 関数である。

Import statement

The functionality used to create a router is contained in the Router API. To use this functionality, you need to import it. By default, the ok() そして notFound() functions are imported, as well as the WixRouterSitemapEntry オブジェクトがある。

コピー
1

If you want to use more of the functionality from the Router API, you need to add it to the import statement.

Sample Data

In the sample scenario, the router uses some static data contained in an object named peopleData.

コピー
1

について peopleData object contains four keys, each representing a person. Each person contains two properties, a タイトル that is the person's full name and an イメージ that is a link to an image of the person.

Although you can use static data with a router, typically you would use data from your site's database or an external source. We'll see where you would write code to retrieve that data a little later.

Regardless of where you get the data from, since you'll be writing the code that handles it, the data can be structured in any way you like.  

Router Function

Remember, all incoming requests with the URL prefix you specified when creating the router are sent to your router for handling. The router() function is where you handle those requests.

について router() function is named with the following convention:

<router prefix>_Router(request)

So if you named your router myRouter, the code added to the routers.js file should look like:

myRouter_Router(request) { // routing code ...}

について router() function has a request parameter which receives a WixRouterRequest object that contains information about the incoming request. The object has information about the URL used to reach the router, where the request came from, and who the request came from.

Typically, the router() function will decide which page to show (if any) and what data to pass to the page. A response is then sent using the forbidden(), notFound(), ok(), redirect(), or sendStatus() functions.

Let's take a look at the sample router() function code:

コピー
1

について router() function begins by parsing the request's path to pull out a 名称 value.

const name = request.path[0];

For example, a user might reach this router() function by browsing to https://mysite.com/myRouter/Ash. The received WixRouterRequest object's path property will be an array with one element: ["Ash"]. Now the value of 名称 is "Ash".


Next, the router() function retrieves data based on the 名称 it received.

const data = peopleData[name];

Continuing our example above, we pull out Ash's information from the peopleData オブジェクトがある。

So data now holds:

コピー
1

If you want to retrieve data from an external source, this is where you place the call to retrieve that data.


After attempting to retrieve the data that corresponds to the incoming request, we check to see if any data was found:

コピー
1

If nothing was found, the if is skipped and we return a 404 error using the notFound() 関数である。

return notFound();


Assuming we found the data we were looking for, we now prepare some header data for our page:

コピー
1

Here we create a HeadOptions object that defines what will be put in the HTML head of the page we respond to the request with. That object is stored in the seoData variable.

The sample code creates a タイトル そして 記述 based on the タイトル property of the retrieved data object. It also sets noIndex to false, meaning search engines should index the page. Finally, some metaTags properties are added as well.

You can create the HeadOptions object using any information you want.

You can also add a キーワード property to the HeadOptions object with a string containing the page's keywords.


Finally, we return a WixRouterResponse object using the ok() function:

return ok("myRouter-page", data, seoData);

Here we route the user to the router page named "myRouter-page" and pass it the data that was retrieved and the seoData that was built for the page.

Depending on the situation, you can return a number of different responses from the router() function. So far we've seen the notFound() そして ok() functions in use. You can also use the forbidden() function to return a 403 response, the redirect() function to return a 301 or 302 response, or the sendStatus() function to return a response with any status code you choose.

Router Data

To use the data that was returned with a WixRouterResponse using the ok() function, use the wix-window-フロントエンド getRouterData() 関数である。

For example, we can take the data passed by the sample router code and use it to present a person's information on the myRouter-page page that was created.

First, we need to add a text and image element to serve as placeholders for a person's title and image.

Next, in the page's code we retrieve the router data and set the text and image elements to display the タイトル そして イメージ from the data.

コピー
1

If you preview the page, you'll see that the placeholders we put on the page are filled in with information from the data that was passed to the page. You can use the preview widget to see what the page looks like for any of the people in the peopleData object that was defined in the routers.js file.

The preview widget gets populated with the タイトル values from the WixRouterSitemapEntry objects that are returned by the router's sitemap() 関数である。

Sitemap Function

Like the router() function, the sitemap() function is named with the following convention:

<router prefix>_Sitemap(sitemapRequest)

So if you named your router myRouter, the code added to the routers.js file should look like:

myRouter_Sitemap(sitemapRequest) { // routing code ...}

について sitemap() function has a sitemapRequest parameter which receives a WixRouterSitemapEntry object that contains information about the incoming request. The object has information about the URL used to reach the router, the pages in the router, and who the request came from.

Typically, the sitemap() function creates a WixRouterSitemapEntry object for each item in the data used by the router. So, in our example, we create an entry for each person in the personData object. You also might want to include entries for other pages, like an index page or a search page.

Let's take a look at the sample sitemap() function code:

コピー
1

について sitemap() function takes the keys of the peopleData object and uses the JavaScript map() function to create an array of WixRouterSitemapEntry objects, one object for each key. Each entry is given values for the pageName, urlそして タイトル properties. Then the array is wrapped in a Promise and returned.

A sitemap entry can also contain changeFrequecy, lastModifiedそして priority properties to give search engines more information about each page.

In a case where you're using a router with external data, in the sitemap() function you would retrieve that data and build sitemap entries for each item that was retrieved.

役に立ちましたか?
はい
いいえ