Recap

The last section covered a basic plugin architecture, designed to separate the project code by unit feature and allowing developers to progressively toggle each one at any given time. Get caught up by making sure your project looks like the following.
PROJECT
config
develop.ts
plugins
app
views
home.tsx
plugin.ts
package.json
tsconfig.json
develop.ts
home.tsx
plugin.ts
package.json
tsconfig.json
Copy
If you are all caught up, then you can proceed to the next section.

3. Server Routes

The following section wil continue exploring the possibilities demonstrated within the app plugin by adding a page route to the project and how a view can consume the response. Page routes are a type of server route and server routes are also used to develop API endpoints.

3.1. Add Page Route

From the project root, create a file called plugins/app/pages/home.ts with the following code.
Copy
This basically consumes the data from the request. Data from a request is a merge of query params, POST data, and URL params respectively. Consider the following request.
Copy
In the example above, assuming the requested pathname is , the return of will be computed as where as URL params has priority over POST data which has priority over query params. Lastly, the is passed to the response via .

3.2. Register Page Route

Next let's change the plugins/app/plugin.ts file with the following code.
Copy
Now in the handler a route has been added that will lazily load the page when the path is requested. Similar to React suspense loading, the reason why lazily loading page routes is recommended is because it will allow bundlers to theoretically analyze the code and possibly split it into smaller chunks. We also want to de-prioritize the view by adding a priority to ensure it loads last. This allows other plugins to inject themselves in the routing phase.
NOTE: You could also use the traditional in your plugins/app/pages/home.ts to render the view, but the Plugin Architecture encourages decoupling the view from the page route. This way, you can progressively toggle everything in the plugins/app/plugin.ts file.

3.3. Consume Page Route

To consume the in the view, you can use the argument in the following way.
Copy

3.4. Check Page Route

  1. 1. In Terminal, restart
  2. 2. On your browser, visit http://localhost:3000/
  3. 3. Then, visit http://localhost:3000/?json
  4. 4. Then, visit http://localhost:3000/api/home
You should observe that by adding the flag you can check the server props being sent to your view. This is different from /api/home where the home page route is treated as an API endpoint.

3.5. Check Point

In case you got lost along the way, here is a checkpoint of what your project should look like.
PROJECT
config
develop.ts
plugins
app
pages
home.ts
views
home.tsx
plugin.ts
package.json
tsconfig.json
develop.ts
home.ts
home.tsx
plugin.ts
package.json
tsconfig.json
Copy
The next section will cover how server props works, how to send custom template data and how the view consumes it.