Skip to content

Commit 467a757

Browse files
TWSingleSourceadamfriWix Tech Writers
authored
new wix routers intro
* DocWorks for wix-router - 6 changes detected, but 2 issue detected changes: Service wix-router.WixRouterSitemapEntry is new Service wix-router.WixRouterUser is new Service wix-router.WixRouterRequest is new Service wix-router.WixRouterResponse is new Service wix-router is new Service wix-router.WixRouterSitemapRequest is new issues: Operation customizeQuery has an unknown param type wix-data.WixDataQuery (router.js (300)) Operation customizeQuery has an unknown return type wix-data.WixDataQuery (router.js (300)) * Published by the Tech Writer Single Sourcing Tool * Delete wix-router-sdk directory * DocWorks for wix-router - no significant changes detected, but 2 issue detected issues: Operation customizeQuery has an unknown param type wix-data.WixDataQuery (router.js (300)) Operation customizeQuery has an unknown return type wix-data.WixDataQuery (router.js (300)) * DocWorks for wix-router - no significant changes detected, but 2 issue detected issues: Operation customizeQuery has an unknown param type wix-data.WixDataQuery (router.js (300)) Operation customizeQuery has an unknown return type wix-data.WixDataQuery (router.js (300)) --------- Co-authored-by: Adam Friedmann <[email protected]> Co-authored-by: Wix Tech Writers <[email protected]> Co-authored-by: adamfri <[email protected]>
1 parent 5f8148e commit 467a757

7 files changed

+140
-107
lines changed

wix-router-sdk/wix-router.service.json renamed to wix-router/wix-router.service.json

+43-103
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,50 @@
11
{ "name": "wix-router",
22
"mixes": [],
3-
"labels":
4-
[ "changed" ],
3+
"labels": [],
54
"location":
65
{ "lineno": 1,
76
"filename": "router.js" },
87
"docs":
98
{ "summary": "This module contains the APIs for code routers and data binding router hooks.",
109
"description":
11-
[ "The code for both routers and data binding router hooks is defined in the",
12-
" **routers.js** file that you [set up](https://support.wix.com/en/article/velo-creating-a-router#add-a-router) in the Code Files' Backend section of the [Velo Sidebar](https://support.wix.com/en/article/velo-working-with-the-velo-sidebar).",
10+
[ "The Wix Router API allows you to handle certain incoming requests to your site's pages and sitemap and customize the responses.",
1311
"",
14-
" > **Note:** You do not create the **routers.js** file manually. When you [set it up](https://support.wix.com/en/article/velo-creating-a-router#add-a-router), it will be",
15-
" created for you.",
12+
"Some things you might do with the Router API are:",
13+
" * Control the URL structure of your site.",
14+
" * Customize your site's sitemap.",
15+
" * Extend [dynamic pages](https://support.wix.com/en/article/content-manager-about-dynamic-pages) to include subpages that use different data.",
1616
"",
17-
" The functions are named with the following convention:",
17+
"The API gives you access to the following:",
18+
" * Requests to certain pages on your site and routing responses for those pages.",
19+
" * Sitemap requests and responses.",
20+
" * 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](https://support.wix.com/en/article/velo-about-data-hooks-for-dynamic-pages).",
1821
"",
19-
" ``` javascript",
20-
" export function <router prefix>_<function name>()",
21-
" ```",
22+
"Code using the Routers API is stored in the **routers.js** file in the backend code section the Velo Sidebar. You shouldn't create this file manually.",
23+
"The file is created automatically when you [add a router](https://support.wix.com/en/article/velo-creating-a-router#add-a-router) to your site.",
2224
"",
23-
" These are not functions that you call in your code, rather they are functions",
24-
" that you define. They are called when your users browse to a URL that is handled",
25-
" by a router as described below.",
25+
"To use the [`router()`](wix-router/router) and [`sitemap()`](wix-router/sitemap) functions in the **routers.js** file, use the following convention:",
2626
"",
27-
" For example, the following code creates a router on the myRouter [prefix](#prefixes) that",
28-
" shows a page when the path begins with the word \"good\" and returns a 404 in",
29-
" all other cases.",
27+
"```js",
28+
"export function <router prefix>_<function name>()",
29+
"```",
3030
"",
31-
" ```javascript",
32-
" import {ok, notFound} from \"wix-router\";",
31+
"To import the other API functions into the **routers.js** file, use the following syntax:",
3332
"",
34-
" export function myRouter_Router(request) {",
33+
"```js",
34+
"import { <function-name> } from 'wix-router';",
35+
"```",
3536
"",
36-
" // URL looks like:",
37-
" // https://mysite.com/myRouter/good",
38-
" // or:",
39-
" // https://user.wixsite.com/mysite/myRouter/good",
40-
" const status = request.path[0];",
37+
"## Terminology",
4138
"",
42-
" if(status === \"good\") {",
43-
" // Show a page",
44-
" return ok(\"myRouter-page\");",
45-
" }",
46-
" else {",
47-
" // Return 404",
48-
" return notFound();",
49-
" }",
50-
" }",
51-
" ```",
52-
"",
53-
" ### Code Router",
54-
" Code your own [`router()`](#router) and [`sitemap()`](#sitemap) functions for a",
55-
" [router](https://support.wix.com/en/article/routers) that handles all incoming",
56-
" requests with a specified URL [prefix](#prefixes). Your code decides what actions to",
57-
" perform, what responses to return, where to route the request, and what data",
58-
" to pass to pages.",
59-
"",
60-
" You might want to use a router to:",
61-
"",
62-
" + Display a dynamic page using content from any data source.",
63-
" + Customize your URLs to make them more meaningful and yield better SEO results.",
64-
" + Authenticate users and then display content just for them.",
65-
" + Return custom HTTP response codes.",
66-
"",
67-
"",
68-
" ### Data Binding Router Hooks",
69-
" When a request comes in for a page that a router handles, either a code router",
70-
" or a dynamic page, you can add [data binding router hooks](https://support.wix.com/en/article/data-binding-router-hooks)",
71-
" to intercept the process of the data getting bound to the page at certain",
72-
" points and insert additional logic.",
73-
"",
74-
" The hooks you can use are listed here in the order they are triggered:",
75-
"",
76-
" + [`beforeRouter`](wix-router/beforeRouter) - Before the data binding router logic.",
77-
" + [`customizeQuery`](wix-router/customizeQuery) - As the data binding router prepares a data query.",
78-
" + [`afterRouter`](wix-router/afterRouter) - After the data binding router completes its logic, but before the page is displayed.",
79-
" + [`afterSitemap`](wix-router/afterSitemap) - After the data binding sitemap function completes preparing the list of urls.",
80-
"",
81-
"",
82-
" ### Prefixes",
83-
" When using the `wix-router` API you often need to know the prefix of your code router",
84-
" or dynamic pages. You can find prefixes as follows:",
85-
"",
86-
" + **Code router**:",
87-
"",
88-
" 1. Go to in the Page Code's Router Pages section of the Velo Sidebar.",
89-
"",
90-
" 1. Click the ellipsis &nbsp; ![ellipsis](../assets/images/ellipses.png) &nbsp; icon that appears when you hover over the title of the router's grouped router pages.",
91-
"",
92-
" 1. Click **Change Router**.",
93-
"",
94-
" The router's prefix is displayed.",
95-
"",
96-
"",
97-
" + **Dynamic pages**:",
98-
"",
99-
" 1. Go to in the Page Code's Dynamic Pages section of the Velo Sidebar.",
100-
"",
101-
" 1. Click the ellipsis &nbsp; ![ellipsis](../assets/images/ellipses.png) &nbsp; icon that appears when you hover over the dynamic page.",
102-
"",
103-
" 1. Click **Settings**.",
104-
"",
105-
" The **Page Info** tab shows the page URL. The prefix is the",
106-
" first editable section of the URL up until the first forward slash (/)." ],
39+
" * **Router**: Part of the code on your site that receives requests for specific URLs and handles which page is ",
40+
" served and what data is included in that page.",
41+
" * **Sitemap**: A model of a site's content structure, designed to help both users and search engines navigate the site.",
42+
" * **Data binding router hooks**: Functions that run before, during, and after data is included in ",
43+
" the pages that the router and sitemap functions return. Router hooks are often used when working with [dynamic pages](https://support.wix.com/en/article/velo-about-data-hooks-for-dynamic-pages). The API includes 4 hooks:",
44+
" * [`beforeRouter`](wix-router/beforeRouter)",
45+
" * [`customizeQuery`](wix-router/customizeQuery)",
46+
" * [`afterRouter`](wix-router/afterRouter)",
47+
" * [`afterSitemap`](wix-router/afterSitemap)" ],
10748
"links": [],
10849
"examples": [],
10950
"extra":
@@ -142,7 +83,7 @@
14283
[ "wix-router.WixRouterResponse" ] },
14384
"doc": "Fulfilled - A router response, either the response received or a new response." },
14485
"locations":
145-
[ { "lineno": 266,
86+
[ { "lineno": 209,
14687
"filename": "router.js" } ],
14788
"docs":
14889
{ "summary": "Registers a hook that is called after a router.",
@@ -257,7 +198,7 @@
257198
[ "wix-router.WixRouterSitemapEntry" ] } ] },
258199
"doc": "Fulfilled - A sitemapEntries array." },
259200
"locations":
260-
[ { "lineno": 322,
201+
[ { "lineno": 265,
261202
"filename": "router.js" } ],
262203
"docs":
263204
{ "summary": "Registers a hook that is called after a sitemap is created.",
@@ -323,7 +264,7 @@
323264
[ "wix-router.WixRouterResponse" ] },
324265
"doc": "Fulfilled - Which page to display, redirect to, or which HTTP status code to respond with." },
325266
"locations":
326-
[ { "lineno": 218,
267+
[ { "lineno": 161,
327268
"filename": "router.js" } ],
328269
"docs":
329270
{ "summary": "Registers a hook that is called before a router.",
@@ -424,7 +365,7 @@
424365
{ "type": "wix-data.WixDataQuery",
425366
"doc": "A wix-data query." },
426367
"locations":
427-
[ { "lineno": 357,
368+
[ { "lineno": 300,
428369
"filename": "router.js" } ],
429370
"docs":
430371
{ "summary": "Registers a hook that is called after a route is resolved by the data binding router, but before the wix-data query is executed.",
@@ -491,7 +432,7 @@
491432
[ "wix-router.WixRouterResponse" ] },
492433
"doc": "Fulfilled - A response object with HTTP status code 403." },
493434
"locations":
494-
[ { "lineno": 439,
435+
[ { "lineno": 382,
495436
"filename": "router.js" } ],
496437
"docs":
497438
{ "summary": "Returns a response with a status code 403 (Forbidden) and instructs the router to show a 403 page.",
@@ -530,7 +471,7 @@
530471
[ "wix-router.WixRouterResponse" ] },
531472
"doc": "Fulfilled - The router response." },
532473
"locations":
533-
[ { "lineno": 502,
474+
[ { "lineno": 445,
534475
"filename": "router.js" } ],
535476
"docs":
536477
{ "summary": "Returns a response that instructs the router to continue.",
@@ -570,7 +511,7 @@
570511
[ "wix-router.WixRouterResponse" ] },
571512
"doc": "Fulfilled - A response object with HTTP status code 404." },
572513
"locations":
573-
[ { "lineno": 420,
514+
[ { "lineno": 363,
574515
"filename": "router.js" } ],
575516
"docs":
576517
{ "summary": "Returns a response with a status code 404 (Not Found) and instructs the router to show a 404 page.",
@@ -599,8 +540,7 @@
599540
"extra":
600541
{ } },
601542
{ "name": "ok",
602-
"labels":
603-
[ "changed" ],
543+
"labels": [],
604544
"nameParams": [],
605545
"params":
606546
[ { "name": "Page",
@@ -625,7 +565,7 @@
625565
[ "wix-router.WixRouterResponse" ] },
626566
"doc": "Fulfilled - The router response object." },
627567
"locations":
628-
[ { "lineno": 391,
568+
[ { "lineno": 334,
629569
"filename": "router.js" } ],
630570
"docs":
631571
{ "summary": "Returns a response with a status code 200 (OK) and instructs the router to show the selected page.",
@@ -745,7 +685,7 @@
745685
[ "wix-router.WixRouterResponse" ] },
746686
"doc": "Fulfilled - The redirect response object." },
747687
"locations":
748-
[ { "lineno": 458,
688+
[ { "lineno": 401,
749689
"filename": "router.js" } ],
750690
"docs":
751691
{ "summary": "Returns a response with a status code of 301 (Moved Permanently) or 302 (Found) and instructs the router to redirect to the given URL.",
@@ -800,7 +740,7 @@
800740
[ "wix-router.WixRouterResponse" ] },
801741
"doc": "Fulfilled - Which page to display, redirect to, or which HTTP status code to respond with." },
802742
"locations":
803-
[ { "lineno": 104,
743+
[ { "lineno": 47,
804744
"filename": "router.js" } ],
805745
"docs":
806746
{ "summary": "Function containing routing logic for a given URL prefix.",
@@ -1013,7 +953,7 @@
1013953
[ "wix-router.WixRouterResponse" ] },
1014954
"doc": "Fulfilled - A response object with the specified HTTP status." },
1015955
"locations":
1016-
[ { "lineno": 482,
956+
[ { "lineno": 425,
1017957
"filename": "router.js" } ],
1018958
"docs":
1019959
{ "summary": "Returns a response with the specified HTTP status code with an optional message.",
@@ -1066,7 +1006,7 @@
10661006
[ "wix-router.WixRouterSitemapEntry" ] } ] },
10671007
"doc": "Fulfilled - An array of sitemap entries." },
10681008
"locations":
1069-
[ { "lineno": 171,
1009+
[ { "lineno": 114,
10701010
"filename": "router.js" } ],
10711011
"docs":
10721012
{ "summary": "Function containing sitemap logic for a given URL prefix.",

wix-router-sdk/wix-router/WixRouterResponse.service.json renamed to wix-router/wix-router/WixRouterResponse.service.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{ "name": "WixRouterResponse",
22
"memberOf": "wix-router",
33
"mixes": [],
4-
"labels":
5-
[ "changed" ],
4+
"labels": [],
65
"location":
76
{ "lineno": 1,
87
"filename": "response.js" },
@@ -14,8 +13,7 @@
1413
{ } },
1514
"properties":
1615
[ { "name": "data",
17-
"labels":
18-
[ "changed" ],
16+
"labels": [],
1917
"get": true,
2018
"set": true,
2119
"type": "Object",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Coding with the Router API
2+
3+
4+
5+
The functions in the **routers.js** file are named with the following convention:
6+
7+
8+
``` javascript
9+
export function <router-prefix>_<function-name>()
10+
```
11+
12+
These are not functions that you call in your code, rather they are functions
13+
that you define. They are called when your users browse to a URL that is handled
14+
by a router as described below.
15+
16+
17+
For example, the following code creates a router on the myRouter [prefix](#prefixes) that
18+
shows a page when the path begins with the word "good" and returns a 404 in
19+
all other cases.
20+
21+
```javascript
22+
import {ok, notFound} from "wix-router";
23+
24+
export function myRouter_Router(request) {
25+
26+
// URL looks like:
27+
// https://mysite.com/myRouter/good
28+
// or:
29+
// https://user.wixsite.com/mysite/myRouter/good
30+
const status = request.path[0];
31+
32+
if(status === "good") {
33+
// Show a page
34+
return ok("myRouter-page");
35+
}
36+
else {
37+
// Return 404
38+
return notFound();
39+
}
40+
}
41+
```
42+
43+
### Code Router
44+
Code your own [`router()`](#router) and [`sitemap()`](#sitemap) functions for a
45+
[router](https://support.wix.com/en/article/routers) that handles all incoming
46+
requests with a specified URL [prefix](#prefixes). Your code decides what actions to
47+
perform, what responses to return, where to route the request, and what data
48+
to pass to pages.
49+
50+
You might want to use a router to:
51+
52+
53+
+ Display a dynamic page using content from any data source.
54+
+ Customize your URLs to make them more meaningful and yield better SEO results.
55+
+ Authenticate users and then display content just for them.
56+
+ Return custom HTTP response codes.
57+
58+
59+
### Data Binding Router Hooks
60+
When a request comes in for a page that a router handles, either a code router
61+
or a dynamic page, you can add [data binding router hooks](https://support.wix.com/en/article/data-binding-router-hooks)
62+
to intercept the process of the data getting bound to the page at certain
63+
points and insert additional logic.
64+
65+
The hooks you can use are listed here in the order they are triggered:
66+
67+
+ [`beforeRouter`](/wix-router/beforeRouter) - Before the data binding router logic.
68+
+ [`customizeQuery`](/wix-router/customizeQuery) - As the data binding router prepares a data query.
69+
+ [`afterRouter`](/wix-router/afterRouter) - After the data binding router completes its logic, but before the page is displayed.
70+
+ [`afterSitemap`](/wix-router/afterSitemap) - After the data binding sitemap function completes preparing the list of urls.
71+
72+
### Prefixes
73+
74+
When using the `wix-router` API you often need to know the prefix of your code router
75+
or dynamic pages. You can find prefixes as follows:
76+
77+
+ **Code router**:
78+
79+
1. Go to in the Page Code's Router Pages section of the Velo Sidebar.
80+
1. Click the ellipsis &nbsp; ![ellipsis](../assets/images/ellipses.png) &nbsp; icon that appears when you hover over the title of the router's grouped router pages.
81+
1. Click **Change Router**.
82+
83+
The router's prefix is displayed.
84+
85+
86+
+ **Dynamic pages**:
87+
88+
1. Go to in the Page Code's Dynamic Pages section of the Velo Sidebar.
89+
90+
1. Click the ellipsis &nbsp; ![ellipsis](../assets/images/ellipses.png) &nbsp; icon that appears when you hover over the dynamic page.
91+
92+
1. Click **Settings**.
93+
94+
The **Page Info** tab shows the page URL. The prefix is the
95+
first editable section of the URL up until the first forward slash (/).

0 commit comments

Comments
 (0)