4

I wonder if I should implement my homepage directly inside the index.js file, or if I should create a seperate "home-page.js" file in the pages folder. Is the index.js file merely the starting point for the application or should I use it as a page itself? Or should I just redirect from index.js to my homepage? What are the different use cases for the index.js file in next.js vs react.js? I do not have any experiences and would like to hear some best practices or things to look out for.

  • 1
    I think the index file directly maps to the `/` URL, so if you did use `home-page` then the URL for the home page would have to be `/home-page`? I'm not sure I see the advantage of doing that. – evolutionxbox Jan 14 '22 at 08:18
  • Thanks for your opinion, I agree. It seems a bit overcomplicated to me as well. I am just asking myself if it would be a bad thing if the index.js file would become too large when I import a lot of components or if this is the purpose of this file and the name is just a default-name for next to find the root file –  Jan 14 '22 at 08:22
  • 1
    The name is also the path, keep that in mind. – evolutionxbox Jan 14 '22 at 08:24

1 Answers1

8

(I assume that you mean the Next.js file /pages/index.js)

You should use the Next.js /pages/index.js file as a page itself (1), i.e. as the page for the route http://localhost:3000, just the same as you would use e.g. /pages/about.js for the route http://localhost:3000/about.

The index.js files in Next.Js and in pure React are actually completely different, unrelated files:

  1. React index.js is the entry point for the apps javascript execution (or "starting point" how you called it).
  2. Next.js pages/index.js is the default route. It corresponds to the index.html of static webages, i.e. the page that comes up if you type e.g. http://localhost:3000 (without any page name) in the browsers address bar.

Next.js doesn't have any "entry point index.js" file at all (in the folders you are editing), instead all the final .js files will only be generated inside the .next folder when you start next build.

See also:

(1) I don't have any opinion about if you should use the /index.js directly, or redirect to e.g. /home, or avoid using /index.js altogether. Here I just try to explain what the /index.js file does, because I think that is what the question was actually asking.

kca
  • 4,856
  • 1
  • 20
  • 41