In this tutorial, we will learn how to create parallel routes in Next.js in just 16 minutes. Parallel routes allow multiple pages to be loaded at the same time, improving performance and user experience.
Step 1: Install Next.js
First, you need to have Node.js installed on your machine. Next, open your terminal and run the following command to create a new Next.js project:
npx create-next-app my-next-app
This command will create a new Next.js project in a directory called my-next-app
. Once the project is created, navigate to the project directory by running:
cd my-next-app
Step 2: Create Parallel Routes
Next, we need to create the parallel routes in our Next.js project. To do this, navigate to the pages
directory in your project and create two new files: page1.js
and page2.js
.
In page1.js
, add the following code:
import React from 'react';
const Page1 = () => {
return (
<div>
<h1>Page 1</h1>
</div>
);
};
export default Page1;
In page2.js
, add the following code:
import React from 'react';
const Page2 = () => {
return (
<div>
<h1>Page 2</h1>
</div>
);
};
export default Page2;
Step 3: Create Parallel Route
Next, we need to create a parallel route that will load both page1.js
and page2.js
at the same time. To do this, open the pages/index.js
file and add the following code:
import React from 'react';
import { ParallelRoutes } from 'next-parallel-routes';
import { useRouter } from 'next/router';
const ParallelRoute = () => {
const router = useRouter();
return (
<ParallelRoutes routes={[
{ Component: () => import('./page1'), path: '/page1' },
{ Component: () => import('./page2'), path: '/page2' }
]} router={router} />
);
};
export default ParallelRoute;
Step 4: Install Next Parallel Routes
To make use of parallel routes in Next.js, we need to install the next-parallel-routes
package. To do this, run the following command in your terminal:
npm install next-parallel-routes
Step 5: Start the Next.js Development Server
Now that we have set up our parallel routes, we can start the Next.js development server to see our changes in action. To start the server, run the following command in your terminal:
npm run dev
Once the server is running, open your browser and navigate to http://localhost:3000
. You should see a page with links to Page 1
and Page 2
. Clicking on either link should load the respective page without refreshing the entire page.
Congratulations! You have successfully learned how to create parallel routes in Next.js in just 16 minutes. You can now experiment with adding more parallel routes to your project to further improve performance and user experience.
What's horrifying to see is how people realying only on video insted of their offical documentation – video explained everything well and it was easy to grasp but you still gotta read the docs so you know more about it. Parallel routing definitely has its uses if you're building some complex dashboard .
Like the most of questions you guys are asking would literally start making sense if you just read the docs once lol.
I cant understand this no matter how many times i watch
IMO, Vercel took this simple concept and made it 10x more complex.
I couldn't really understand the point of parallel routes over components as they already are until 9min into this video, with the advanced nested parallel routes… but then to see it doesn't work in dev mode?… Having to restart the dev server for it to work can make the problem really obscure.
Why not using middleware for this?
Thanks but why tf would anyone do that just for a simple case
Hi Kyle, what about creating advanced JS / TS / React course ?
This feature is good for a dashboard with a lot of graphs that can take a while to load the data needed to render the graphs
in the last 4 years, this man helped me go through most of the challenges i face as as self thought programmer. Today I work for an MNC and 30%-40% of the code solutions Implemented, I learnt from WDS.
how does this differ from components?
why is this a thing if you could do the same thing by conditionally rendering components that do the same thing?
this all started for just solving the issue of having a separate loading file for each component is silly. If that is the only benefit of this. Then this is an overkill
what if i want a diffeent page and dont want to create page under each @ folder and also divide my logic like all my code for new page will be in settings > page.tsx
You know what! I really love your intro slogan. <3
Next.js just amazing
thanks for the video. this feature doesn’t make any sense. why complicate it when you can just import components? Also, you can pass props easily if you have any
Though NextJS makes me give weird names to my files…I like weird names better within JS code in strings
イケメン
Anyone know if it's possible to have a wrapper layout or page to pass in props to the slots? The use case if there was interactivity outside of the parallel routes that can show/hide a particular slot from a button click.
App routers will add extra json at the end of your html, double page size, the response time is slower than page router.
Is the purpose of this to increase app performance or just to improve the dev experience?