Rendering Strategies (App Router)
In this chapter, we will repeat the steps and see how `App Router` differs from `Pages Router`.
Pricing Page
Can we reuse the PricingSection component in both App Router and Pages Router?
Let us check. Inside the app folder, create a directory called app-router to store all the rendering strategies. Within this folder, create another folder ssr with a file called page.tsx. This is how the routing works in App Router. page.tsx file is the page that will be rendered while the folder name is the route.
Run the dev server and navigate to the /app-router/ssr route. You should see the pricing page which means the PricingSection component works.
If you compare the rendering section in the Next.js docs for the App Router and Pages Router the concepts of SSR, SSG, and CSR are not used in the App Router. This is crucial though the same concepts are referenced with different terminologies.
The reason for not using the same terminology is because of the React Server Components and they stick to just the terms "server" and "client". We will though in this chapter try to still use the same terminology.
Server Side Rendering (SSR)
But it is static, so how do we get that getServerSideProps to work in App Router? It is as simple as creating an async function and calling it right in the component.
This time we access the IP Address from the headers object provided by next/headers.
App Router supports React Server Components (RSC) by default. This means our component can be asynchronous. This entire component is built on the server and sent to the client. Simpler, right?
Run the build command:
You should see an output like the following:
With the build server (npm start) and ngrok server (ngrok http http://localhost:3000) running, navigate to the /app-router/ssr route on whatever free URL you get from ngrok and you should see the relevant pricing page.
Alright, SSR was easy. What about SSG?
Static Side Generation (SSG)
The logic from Pages Router remains the same. We have to generate the static pages and use our middleware to redirect to the right path.
Create a dynamic page inside app-router folder with the route being [country]:
The getStaticPaths function has been replaced by generateStaticParams. While the need for getStaticProps has been removed. The generateStaticParams has to return all the possible paths that should be generated at build time for the matching route.
Back in the middleware, we can update to also handle the App Router.
Run the build command:
You should see an output like the following:
With the build server (npm start) and ngrok server (ngrok http http://localhost:3000) running, navigate to the /app-router route on whatever free URL you get from ngrok and you should be redirected to the country-specific page.
Not much difference compared to the Pages Router. What about CSR?
Client Side Rendering (CSR)
There is almost no difference in the Client Side Rendering (CSR) approach. We just have to add a 'use client' directive at the top of the page and the rest of the logic remains identical.
Create a static page inside app-router folder with the route being csr and add the following code:
Run the build command:
You should see an output like the following:
With the build server (npm start) and ngrok server (ngrok http http://localhost:3000) running, navigate to the /app-router/csr route on whatever free URL you get from ngrok and you should see the relevant pricing.
Summary
In this chapter, we have discussed the differences between Pages Router and App Router.
Of course, there is more to App Router than just this one-to-one comparison with Pages Router. App Router allows for greater composition of components and rendering strategies which we will cover to a greater extent in a different course.
At this point, our code should match the code in the branch 3-rendering-app.
Last updated on