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?
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.
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.
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.
The src directory is very common in many apps and Next. js supports it by default.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With