Hello Friends ๐,
I executed the plan to add pages to the portfolio project. I love how convenient routing handling is in Next.js.
What I Learned
App Router
In version 13, Next.js introduced the new App Router that is built on React Server Component.
-
๐ Special note:
- In the docs, two features are available, App Router and Pages Router. We can choose between two from the dropdown menu at the top sidebar. We must keep in mind which tab we choose as each of them is unique.
- It's recommended to migrate to App Router to get the most out of React's latest features.
Pages
-
Next.js uses a file-system-based router where folders are used to define routes. For example, if my
app
folder has this structure:
app โโโ global.css โโโ layout.js โโโ page.js โโโ blog โโโ page.js โโโ post-one.md
Then the route to
post one
will bewebsite.com/blog/post-one
.The breakdown of the route:
- The
page.js
under theapp
folder is the route to the first slash (/
) afterwebsite.com
. - The
page.js
that lives under theblog
folder is the route toblog/
. - The
post-one.md
is the route topost-one
.
Read the Routing Fundamentals section in the docs for a thorough explanation.
- The
page.js
is used to make the route publicly accessible. The URL path from a folder withoutpage.js
won't be accessible to the public.
Link
- Like the anchor (
<a>
) tag in HTML,<Link>
also uses thehref
attribute/prop. The difference is that we use<Link>
if we want to navigate between routes in our application, and we use<a>
when we want to navigate to an external URL path.
What I Did
By default, there is a
page.js
file in theapp
folder. This file used to be calledindex.js
. I use it as the homepage of the website. For now, it only shows the homepage title.I created a
pages
folder in the root and addedabout-me.js
,blog.js
, andcontact.js
. ย Like the homepage, there's only a title on every page for now.-
I want every page to have a link back to the homepage so I don't need to type the URL path every time.
-
Import the Link.
import Link from "next/link"
-
Applied the link to get back to the homepage.
<p> Back to <Link href='/'>Homepage</Link> </p>
-
Some Thoughts
Now as I'm writing this post, I think I messed up between the two featuresโApp Router and Pages Routerโin the docs ๐ .
The Learn Next.js tutorial on the Next.js website uses the Pages Router. So I probably read theย Pages Routerย docs without switching to theย App Router. Because I'm using theย App Router, I think I should adjust something regarding the pages for the next step. Well, I need to reread the docs ๐.
Next Plan
Read the docs and see if I should fix anything regarding the pages.
Create a navbar and a footer.
Resources
๐ผ๏ธ Credit cover image: unDraw
Thank you for reading! Last, you can find me on Twitter, Mastodon, and BlueSky. Let's connect! ๐
Top comments (0)