Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NextJs: What are the implications of putting other files into /pages?

I like a modular architecture and I’d love to have something like:

/pages
--/somepage
----/index.tsx
----/_components
------/ComponentA.tsx
------/ComponentB.tsx

Rather than having a folder outside of /pages where I keep all of the components for /somepage organized.

However, I understand the /pages folder is treated differently. Are there any performance implications or reasons why I should not do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Every file in pages/ is treated as a real route in your application. For example, in your example, someone could visit mysite.com/somepage/_components/ComponentA as a real page.

If you want this behavior, go for it. If you don’t, put your components outside of the folder.

As for performance implications, Next.js will compile/build your components files as pages, too, which will probably slow down the build process needlessly.

So please don’t do this:

/pages
--/somepage
----/index.tsx
----/_components
------/ComponentA.tsx
------/ComponentB.tsx

and do this:

/components
--/somepage
----/ComponentA.tsx
----/ComponentB.tsx
/pages
--/somepage
----/index.tsx
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading