Next.js is one of the most popular frameworks for developing React applications due to its advanced features and simplicity. One of the exciting features of Next.js is the ability to use the App Router, which allows you greater control over the routes and processes of your application. By utilizing this feature, you can design your application with routes that can be processed either as server-side or client-side.
In Next.js, you have the option to use both Edge processing and Node.js processing as an alternative. Edge processing lets you run your applications at locations closer to the users, which can improve performance and reduce latency. On the other hand, Node.js processing allows you to take advantage of powerful server capabilities for heavier and more complex processes.
Overall, utilizing the App Router provides you with the ability to leverage the best capabilities and features for both types of processing. For example, you can run parts of your application that require fast processing close to the user on Edge, while sections that need to interact extensively with server resources can be handled on Node.js.
Now that you are familiar with the general information about using the App Router in Next.js, let's review some simple code for setting up this capability. In this code snippet, we will look at the basic steps for configuring different processes and creating routes.
import { createRouter } from 'next/app-router';
const appRouter = createRouter({
routes: [
{
path: '/',
component: 'Home',
edge: true // sets this route to be processed in Edge.
},
{
path: '/about',
component: 'About',
node: true // sets this route to be processed in Node.js.
}
]
});
export default appRouter;
import { createRouter } from 'next/app-router';
Import the createRouter function from the package `next/app-router`. const appRouter = createRouter({
Create an instance of the router using the createRouter function. routes: [{ path: '/', component: 'Home', edge: true }]
Here we define a route that specifies that with the `edge: true` setting, it will be processed in Edge. { path: '/about', component: 'About', node: true }
For the second route `/about`, it specifies that this route will be processed server-side or in Node.js. export default appRouter;
The defined router is exported as a default so that it can be used elsewhere in the application.