Guide to using NextJS Parallel and Intercepting Routes
Introduction
In the previous article I guided some ways to use NextJS App Router, now we will continue with 2 other features including
- Parallel Routes: as the name implies you can understand that it allows to define many separate routes and put them together on 1 route for display, the very clear advantage is that it helps to separate UI, isolate errors (if any), easy to maintain and expand functionality
- Note that if you use this feature and in the main route has define an additional 1 page that is not a Parallel Routes, then in the Parallel Routes should have an additional file to show default information to avoid 404 errors when accessing directly (we will go into more detail in the detail part below)
- Intercepting Route: Allows "blocking" a route to display that content in another context (for example: a link will display a Modal, but when reloading that page, a separate page will open).
Prerequisites
- This article is continued to be developed from previous articles, please review if you do not understand to have basic information about App Router before continuing
- Note that in this article because I only focus on 2 features which are Parallel Routes and Intercepting Route so there will not be much explanation about the UIs, you can also replace with your corresponding source code to suit
Detail
Update file app/_type/common.ts
Update file app/_data/common.ts to create fake data for order
Create file app/dashboard/layout.tsx
This file plays the role of the main Layout for Dashboard, using the Parallel Routes feature of NextJS. It receives "slots" like analytics and orders (corresponding to the @analytics and @orders folders) along with traditional children. This layout arranges the slots into a grid system, allowing to display multiple pages at once on a single interface while maintaining independence of logic and data.
Create file app/dashboard/page.tsx
Create file app/dashboard/settings/page.tsx
Create file app/dashboard/@analytics/loading.tsx
Create file app/dashboard/@analytics/page.tsx
Create file app/dashboard/@analytics/default.tsx
Create file app/dashboard/@orders/page.tsx
Create file app/dashboard/@orders/default.tsx
Create file app/dashboard/@orders/(.)order/[id]/ModalFrame.tsx
This component is a Client Component that plays the role of a "frame" for the Modal. It handles user interactions such as closing the modal (router.back()), preventing event propagation when clicking on the container area, and navigating to the full detail page. By separating this frame, we can wrap any Server Component content inside while keeping the client-side logic for the Modal's UI.
Create file app/dashboard/@orders/(.)order/[id]/page.tsx
Create file app/dashboard/order/[id]/page.tsx
This is the order detail page in full page mode. What's special here is thanks to the Intercepting Routes technique (with the (.)order folder), when the user clicks on the link from the Dashboard, this page will be "blocked" and displayed as a Modal (the @orders/(.)order/[id] folder). However, if the user reloads the page or copies and pastes the link directly into the browser, NextJS will render this page.tsx file to display full information on an independent page, ensuring a smooth user experience and optimizing for SEO.
The final folder structure will be like this
I will explain the function of each component as follows:
- dashboard/layout.tsx: this is the layout apply to all routes when accessing /dashboard, contains slots for @analytics and @orders
- dashboard/page.tsx: is the main page of dashboard
- @analytics/loading.tsx: show loading while loading content from page.tsx because this is SSR
- @analytics/page.tsx: main content will show on UI
- @analytics/default.tsx: default page will show if no corresponding slot is found
- @orders/page.tsx: main content will show on UI
- @orders/default.tsx: default page will show if no corresponding slot is found
- @orders/(.)order/[id]/page.tsx: server component is modal, contains static or data load from server, when accessed via navigation link then will open as modal
- @orders/(.)order/[id]/ModalFrame.tsx: client component, because using modal needs interactions with UI so you must convert file page.tsx to client component or create additional 1 file like this to use
- order/[id]/page.tsx: is the detail page of order, used when you access direct address url
- settings/page.tsx: this is the page used to test default function of @analytics and @orders, if you don't add default.tsx file for 2 slots above then when accessing direct to /dashboard/settings will report 404 error
- Here pay attention about the priority order of Intercepting Route:
- If use navigation link: prioritize opening page @orders/(.)order/[id]/page.tsx
- If access directly on address url of browser: prioritize opening page order/[id]/page.tsx
The result will be as follows
If you don't have default.tsx file for @analytics and @orders
Happy coding!
Comments
Post a Comment