Skip to main content

Command Palette

Search for a command to run...

How I structure my Next JS Projects

Published
โ€ข4 min read
How I structure my Next JS Projects
A

Hey, I am a developer and I write about web development here.

Hello folks! It's been a while since I published an article. So today I'm going to explain how I structure my NextJS projects.

Note: There is no right or wrong way to structure a Next JS project, and this is highly opinionated.

So, the structure of a Next JS project depends mainly on the complexity of a project. And if a project needs only page and is small in terms of complexity, you should not over-structure it. With that being said, let's see how to manage your project depending upon the complexity.

๐Ÿ“ƒ Single Pages

Next JS automatically routes every file in the pages/ directory to a name associated with the file name.

For example, the React component inside the pages/dashboard.jsx will be routed to ${URL}/dashboard

For single pages, you can just create a single file that will export a React component.

Example:

- pages/index.js

function Home() {
  return <div>Hello world</div>;
}

export default Home;

๐Ÿงฉ Breaking down into smaller components

Now, at some point, your file will have a lot of lines, so you can make smaller standalone components.

Example:

- pages/dashboard.jsx

const Header = () => {
  return <header>{/* component code */}</header>;
};

const Hero = () => {
  return <div>{/* component code */}</div>;
};

function Dashboard() {
  return (
    <>
      <Header />
      <Hero />
    </>
  );
}

export default Dashboard;

๐Ÿ“ Creating custom files for components

The above example works if you have smaller components. But it is advisable to create standalone files for components.

Now, conventionally, components should be stored in the components directory at the root directory of the app.

Example:

- components/Header.jsx

const Header = () => {
  return (
    <header>
      {/* some code */}
      {/* some more code */}
    </header>
  );
};

export default Header;

Then, in the desired file, you can import and use it.

Example:

- pages/index.js

import Header from "../components/header.jsx";

function Home() {
  return (
    <>
      <Header />
      {/* main component */}
    </>
  );
}

export default Home;

Now, as your apps grow, they will contain more and more components, and while importing them, the code can be a little messy.

Here's a simple workaround for this:

First, make a file inside the components directory named index.js or index.jsx

Then, inside the file, export all the components at once.

- components/index.js

export * from "./Header.jsx";
export * from "./Hero.jsx";
export * from "./Footer";

Next, you can import all the components inside your desired file at once.

Example:

- pages/index.js

import { Header, Hero, Footer } from "../components";

function Home() {
  return (
    <>
      <Header />
      <Hero />
      <Footer />
    </>
  );
}

๐Ÿ—ƒ Making categories for similar components

Now, let's say you have components of similar kinds. Like the Header and the Footer component acts like navigation components. Similarly, you have different Card components that can be sorted into the Cards category.

So, you can create different folders inside the components directory

For example:

- components
    | - Navigation
        | - Header.component.jsx
        | - Footer.component.jsx
    | - Cards
        | - Confirm.card.jsx
        | - Checkout.card.jsx

๐Ÿ“– Making categories for similar pages

Back to pages, in some cases, pages can also fall into some categories. For example, the sign-up and login page falls into the auth category.

So, for that case, you can make a directory inside the pages directory named auth containing the sign-up and login pages.

- pages
    | - auth
        | - sign-up.jsx
        | - login.jsx

๐Ÿ—„ Storing Files, Fonts

Moving from this, the conventional way to store external files such as Images, Fonts, etc. is to store them in the public directory.

For example, you can store all the required images in the public/assets directory.

Similarly, you can store the required fonts in the public/fonts directory.

Example:

- public
    | - assests
        | - cover.png
        | - logo.png
    | - fonts
        | - poppins-medium.woff2
        | - sen-regular.woff2

๐Ÿ”ฎ Managing custom hooks, types, functions

In addition to this, you can create separate folders for custom hooks, types, functions, etc.

- hooks
    | - useuser.jsx
- @types
    | - propTypes.ts
- utils
    | - uploadImage.js

That's a wrap for this article. If you liked this, make sure to drop some reactions on this article.

About the Author

Y

Very helpful, thank you Anurag.
I would like to know how to structure the styles files?

K
Khoi Le4y ago

Don't use this

- components/index.js

export * from "./Header.jsx";
export * from "./Hero.jsx";
export * from "./Footer";

It will increase size when bundle source. You should use lazy load for components

V

I guess next js wont bundle the whole file right? It just builds what it needs right?

I am not sure, please correct me.

J

I would include a screens folder to split up your pages into multiple parts like Hero, CTA, etc.

This allows the page files to delegate while keeping the component library free of components that might only ever be used in one page.

2
P

Great! Have you tried implementing atomic design for component folders?

3
K

gg's

3
P

Great Article Anurag! Will use this method

3