admin管理员组文章数量:1410737
I was going through this next.js docs.
- Here it says index.js acts as root of the directory. So If I enter
/blog
in the URL it takes me topages/blog/index.js
file.
Now my question is I have pages/blog/subponent.js
I don't want users to access /blog/subponent
because it is a subponent. How can I do that?
Now I saw another documentation from next.js that we can use page.js
instead of index.js
and only page.js
bees publicly accessible.
- so does that mean the other files cannot be accessible if it is not
page.js
?
I'm really confused. Any help would be appreciated
- https://nextjs/docs/pages/building-your-application/routing/pages-and-layouts
I was going through this next.js docs.
- Here it says index.js acts as root of the directory. So If I enter
/blog
in the URL it takes me topages/blog/index.js
file.
Now my question is I have pages/blog/subponent.js
I don't want users to access /blog/subponent
because it is a subponent. How can I do that?
- https://nextjs/docs/app/building-your-application/routing/pages-and-layouts
Now I saw another documentation from next.js that we can use page.js
instead of index.js
and only page.js
bees publicly accessible.
- so does that mean the other files cannot be accessible if it is not
page.js
?
I'm really confused. Any help would be appreciated
Share Improve this question asked Aug 2, 2023 at 4:39 Sanket hnSanket hn 792 silver badges9 bronze badges1 Answer
Reset to default 8There are two types of routing in Nextjs:
- Pages router
- App router
Pages router (The first link you shared)
It enforces you to use index.tsx if you have the page ponent in a folder of pages directory, and if you don't want to use a folder, you can just directly create a file with page name in your pages directory. Such as:
pages/blog.js → /blog (wihtout folder)
pages/blog/index.js → /blog (within a folder)
App router (The second link you shared)
It enforces you to use pages.tsx if you have the page ponent in a folder of app directory, and if you don't want to use a folder, you can just directly create a file with page name in your app directory. Such as:
app/blog.js → /blog (wihtout folder)
app/blog/pages.js → /blog (within a folder)
As for other files in the folder, they won't be accessible as long as you have pages.tsx or index.tsx in your folder. But it is not a good practice to place ponents in pages or app folder. You should only place the page main ponent in these directory.
本文标签: javascriptwhat is the difference between indexjs vs pagejs in nextjsStack Overflow
版权声明:本文标题:javascript - what is the difference between index.js vs page.js in next.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744332143a2601015.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论