DEV Community

Cover image for Mastering Next.js: The Ultimate Guide to Structuring Large-Scale Projects in 2024

Mastering Next.js: The Ultimate Guide to Structuring Large-Scale Projects in 2024

Vishal Yadav on August 02, 2024

Introduction: Taming the Next.js Jungle Hey there, code wranglers and Next.js enthusiasts! πŸ‘‹ Are you feeling like Indiana Jones, hacking...
Collapse
 
sheraz4194 profile image
Sheraz Manzoor

Great written boy. I am Senior Software Engineer and I appreciate this structure. One minor thing I will suggest to improve, the components is a different thing and forms is a different.

For forms and big layouts, which are route related (means which are for a specific page) they should kept in modules directory..

And only reusable components should kept in components directory. That it. I just wanted to see how others structure this and what are you teaching to other.

With that minor improvement, I suggest all the juniors to follow this structure!!!

Collapse
 
zakharovvladyslav profile image
Zakharov Vladyslav • Edited

Where are components styles? I have a structure

components/
|__ component-name/
|_____ component-name.module.scss
|_____ index.tsx
|__ index.ts

in index.ts:

export * from β€˜./component-name’

so after that I can import components like

import { ComponentName } from β€˜@/components’;

Collapse
 
princemuel profile image
Prince Muel

Barrel exports are easier to make but can increase your code import size. Also, they can leak client component code into server components and vice versa.

Collapse
 
zakharovvladyslav profile image
Zakharov Vladyslav

Thanks a lot for this. Never knew and never met explanations for it

Collapse
 
gikdev profile image
Mohammad Mahdi Bahrami

I do it the same way! πŸ˜…

Collapse
 
nikolailehbrink profile image
Nikolai Lehbrink

Nice article, but I think you should update the API Routes for the Win section @vyan, because that's not an API Route from Next.js 13+ :) It is more like:

// app/api/posts/route.ts
import { NextResponse } from 'next/server';

export async function GET() {
  const posts = await fetchPosts();
  return NextResponse.json(posts);
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
okabarack profile image
Barack Okaka Obama

Next is a very clean language.

Collapse
 
ttsoares profile image
Thomas TS

NextJS has JS in the name because is Javascript. And Javascript is a language.

Why bother with a Microsoft (evil) tool as Typescript if we have JSDoc?

Collapse
 
vitalets profile image
Vitaliy Potapov

Regarding to auth, we have an opposite structure - keep most pages in (authorized) dir and only login/register on the root level. This is because for all authorized pages we have a header in layout, but it is not shown on login/register pages:

|_ πŸ“ app
|  |_ πŸ“ (authorized)
|  |  |_ πŸ“ dashboard
|  |  |  |_ πŸ“„ page.tsx
|  |  |_ πŸ“„ layout.tsx
|  |_ πŸ“ login
|  |  |  |_ πŸ“„ page.tsx
|  |  |  |_ πŸ“„ layout.tsx
Enter fullscreen mode Exit fullscreen mode

Could you advice how can we achieve the same with (auth) dir?

Collapse
 
bijaydas profile image
Bijay Das

hey!!, this is exactly my structure

Collapse
 
vyan profile image
Vishal Yadav

Cool

Collapse
 
lwooden profile image
lwooden

Great article! I did notice that you omitted an β€œactions” folder under lib, but this is probably because you are using /api as a backend instead of SAs. Additionally, you have reference components that are using client side hooks but you didn’t annotate the file like so: β€œuse client”

Collapse
 
lord_delight profile image
Ogbole Divine

All these folders do they have extra price of coding within them?
If yes how do u write those and embed them on ur Next.js screen.....

Secondly can I use vs.code to work on a next.js app?
Can I also write them in pseudocode?

Collapse
 
zafercuz profile image
Michael Steven David

Where would you likely put server action files? Would it be inside its own folder within the root folder or inside the lib?

Collapse
 
rehan_arif_1234 profile image
Rehan Arif

"In the constantly changing field of web development.", the pursuit of speed, efficiency, and simplicity is ceaseless, much like the advances in AI-powered tools explored in our GitHub Copilot vs Chatbot article. Next, js 14, Vercel's latest innovation, has arrived to redefine the way we construct web applications. Equipped with an array of enhancements, including the groundbreaking TurboPack, robust Server Actions, and the eagerly anticipated Partial Pre-Rendering, Next js 14 is set to transform your web development experience, Read more at webzeto.com

Collapse
 
mohamed_karim_2dddebb42bd profile image
mohamed karim

Thank for sharing

Collapse
 
vyan profile image
Vishal Yadav

My Pleasure!

Collapse
 
ashok2406 profile image
Ashok Kumar

Hi I need nextjs with swagger-autogen 3.0

It's works but not able to get dynamic routes each endpoints of the top we need to add some swagger schema why

I don't want like to add schema like swagger 2.0

If any one solve it please add a blog and tag @ashok2406

Collapse
 
ouyangzhigang profile image
Voyagergle

oh no, is this the end?
but i still give a sponsor

Collapse
 
vyan profile image
Vishal Yadav

For What ?

Collapse
 
jamstra profile image
Jam Straw

Fantastic article.

Collapse
 
kresi profile image
Alberts Kresi

Great

Collapse
 
thundernet8 profile image
Touchumind

And another tip or maybe personal preference: use named exports for your components. This makes sense for code intellisense.

Collapse
 
itsrakesh profile image
Rakesh Potnuru

Nice! This is close to how I structure my Next.js apps.

Collapse
 
chris_leo profile image
Chris Leo

Wow I woul not wait to use it lately thanks for this awesome advice

Collapse
 
rmfloris profile image
rmfloris

Where/how would you add tests/mocks to the structure?

Collapse
 
sumonta056 profile image
Sumonta Saha Mridul

@vyan any GitHub link available? so that I can explore the structure more?

Collapse
 
developerkaushalkishor profile image
developerkaushalkishor

I love to learn from you,after seeing this article

Collapse
 
ashishsimplecoder profile image
Ashish Prajapati

Such a good content. Mine is also matching but learned a few things from you.

Collapse
 
boby_tiwari_fd03ffa35b156 profile image
Boby Tiwari

Nice

Collapse
 
ugener profile image
ugener

thanks for sharing!

Collapse
 
wesleychong profile image
Wesley Chong

Cool

Collapse
 
mohamed_karim_2dddebb42bd profile image
mohamed karim

Thank for sharing

Collapse
 
legationpro profile image
Anze

Hey, what are the pros of dynamic importe i.e code splitting?

Collapse
 
nikolailehbrink profile image
Nikolai Lehbrink

Imagine you have a <Dialog> component that heavily relies on JavaScript. If this component is loaded with the initial page render, it increases the initial bundle size unnecessarily, especially if the user never opens the dialog. With dynamic imports, such as in Next.js 13+ using const Dialog = dynamic(() => import('../components/Dialog')), you only load the component when it's needed. This means the JavaScript for the <Dialog> component is only loaded when the button to open it is clicked. Hope this helps. You can also take a look at the reference in the Next.js docs.

Collapse
 
kodmindset profile image
Emmanuel Awabil

I love this

Collapse
 
vyan profile image
Vishal Yadav

Nice

Collapse
 
renterra profile image
Renterra

Good post!

Collapse
 
vyan profile image
Vishal Yadav

Thanks!

Collapse
 
dharsan_r_5f964e92d555dcb profile image
DHARSAN R

Very useful post keep sharing more like this πŸ‘πŸ‘

Collapse
 
vyan profile image
Vishal Yadav

Yes, I will share more!

Collapse
 
abbatyya profile image
abbatyya

Useful article, thanks

Collapse
 
vyan profile image
Vishal Yadav

Tq!

Collapse
 
israel_ebube profile image
ANICHUKWU ISRAEL

Thank you for the knowledge.
I will add it to my project development

Collapse
 
codefolder profile image
Codefolder

Codefolder best coding platform

Collapse
 
gomezgondwee profile image
Gomez

Thank you found this very helpful

Collapse
 
sanwicklith_mkalinga_d243 profile image
Sanwicklith MKALINGA

Am planning to delve into next.js and I must say this article has really convinced me and given me great perspective about the framework.

Thanks

Collapse
 
throw-new-error profile image
Try Catch

Great article!! I was looking exactly for some architecture explanation like yours.
Thanks!

Collapse
 
vyan profile image
Vishal Yadav

Thanks!

Collapse
 
abdelrahman_mohamed_b7936 profile image
Abdelrahman Mohamed

Berfact ✌️
Keep going explain more

Collapse
 
codeakash profile image
Akash

πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘