This is a small repo to go with my blog post comparing the pages and app routers in Next.js: https://danaciocan.com/nextjs-pages-vs-app-router
You will need to have the following installed in order to run this code:
- Node.js
- NVM (node version manager)
To run this code, do the following in the root of this repo:
- Run
nmv use
- Run
npm install
- Run
npm run dev
Once the app is running, you can go to:
- http://localhost:3000 to visit the homepage
- http://localhost:3000/product/1234 to visit the Product page for an imaginary product with ID 1234 - you can change the ID to anything you like and see it update in the UI
Global styles are not allowed in the pages router, which means we have to use CSS modules even when we know that the styles don't clash. In the app router, this is not a problem and you can use global CSS in your child components.
Sharing a nav bar between pages in a section is a lot trickier in pages router - it has to be a separate component that manually gets inserted into the page, rather than defining a new layout.tsx
and putting it in the directory where you'd like it to apply.