Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this Next.JS folder structure recommended?

We have resorted to this following project structure

|- pages     |- <page_name>         |- index.js             # To do a default export of the main component         |- MainComponent.jsx    # Contains other subcomponents         |- main-component.css   # CSS for the main component         |- OtherComponents.jsx  # more than 1 file for child components that are used only in that page         |- __tests__            # Jest unit and snapshot tests |- components     |- index.js                 # Exports all the default components of each component as named exports     |- CommonCmpnt1         |- CommonCmpnt1.jsx         |- common-cmpnt-1.css         |- index.js             # To default export the component         |- __tests__     |- CommonCmpnt2         |- CommonCmpnt2.jsx         |- common-cmpnt-2.css         |- index.js             # To default export the component         |- __tests__ 

To clarify, nothing has broken and it works amazingly well. We have components that are reused in multiple pages inside the components directory, where we import as follows

// Assuming we are inside MainComponent.jsx // ... import { CommonCmpnt1 } from '../../components';    // We even take it a step further and use absolute imports 

Also, components that are used only once reside side-by-side in it's pages folder.

The only problem we face now is the Hot Module reloading is broken, i.e. whenever we edit the .jsx file inside either the pages or the components directory, we have to manually reload the page to see effected changes (It doesn't affect the CSS files). It is something that we've grown accustomed to and so doesn't affect us seriously.

My question is, is there any impending catastrophe that we do not know of?

like image 661
cr05s19xx Avatar asked Dec 19 '18 15:12

cr05s19xx


People also ask

Where should I put my NextJS components?

NextJS treats every component file under the pages folder as a page, so by placing non-page components in the pages folder, you are drastically increasing build time because now NextJS goes and builds every one of those components as a page.

Is NextJS scalable?

Next. js is a scalable and high-performance React. js framework for modern web development and provides a large set of features, such as hybrid rendering, route prefetching, automatic image optimization, and internationalization, out of the box.

Do companies use NextJS?

Next. js is widely used by the biggest and most popular companies all over the world like Netflix, Uber, Starbucks, or Twitch. It's also considered as one of the fastest-growing React frameworks, perfect to work with static sites – which was the hottest topic in the web development world lately.

Does NextJS have a src folder?

The src directory is very common in many apps and Next. js supports it by default.


1 Answers

Stumbled upon this post while searching for a suitable folder structure for NextJS myself. I've been using a similar structure but recently found that this is not how you're suppose to use NextJS.

I don't know too much about the details, but NextJS has optimizations at the page level. When you build a NextJS project, you will see the pages logged as part of the build. NextJS treats every component file under the pages folder as a page, so by placing non-page components in the pages folder, you are drastically increasing build time because now NextJS goes and builds every one of those components as a page.

like image 134
Hans Avatar answered Sep 22 '22 17:09

Hans