I’m learning to use NextJS and I’m using the latest version with App Router, I’m currently stuck on how to do routing, such as where to put the register and login pages, and its’ folder structure in general, where I put components and how to group other related components together, can you shed light on this topic, and please make it as simple as possible and maybe give some examples because I’m still learning, any help would be much appreciated, thankyou !
2
Answers
some keep the
components
inside the app but I keep them outside to tell other engineers thatapp
directory include only page components. If you have similar routes that have shared layouts you can use()
syntax insideapp
directory. For exampleinside
auth
folder you can havelogin
andsignup
pages with the shared layout.Inside the components folder, you can create sub-folders for
providers
,ui
orshared
components.you can use various routing in latest App router version and even customise the routing to use your own customised routing.
Default Routing with Next.js App Router:
for creating a page,you can directly create a folder with page name inside the folder
for example about page,create about folder, create page.jsx(inside about folder,act as the index page for about i.e. can be accessed by URL/about)
App
/about
page.jsx
2.) To implement custom routing
you can add in next-config
I have researched over the directory structure too when i started
For Best Practices ,i directly create the pages inside the App directory.like
all/products/page.jsx or app/about/page.jsx
for admin ui , i use app/admin/pagenameofadmin/page.jsx
app/admin/page.tsx(admin home page)
for all other components use components folder,and create subfolders in components to find components easily
layout – for common and repetitive components like header/footer/sidebar
auth – for login signup related components
admin – for admin ui components
pagename – for page specific component
can also create for folder for specific content type if you want to keep everything organised at next level, i recommend to use it for complex projects only
product – for products realted components
keep apis inside app/utils folder.