英文:
Can i use both /app and /pages folder on my Next 13 app?
问题
我正在进行一个项目,它将/app文件夹作为主要路由容器,但由于Next.js存储库上的某些内存泄漏问题,我们在本地主机上遇到了性能问题。我想知道是否可以在/pages文件夹中构建项目的新部分(当然不能与现有路由冲突),甚至可以使用TypeScript,因为现有项目是使用JavaScript构建的。
英文:
The full detail is that I’m working on a project that is using the /app folder as its main route container but we’ve been having performance issues on localhost due to some memory leaks described on an issue on the Next js repo. I want to know if I can build new sections of the project (without conflicting routes, of course) in the /pages folder and even use typescript because the existing project is built with JavaScript.
答案1
得分: 5
根据Next.js文档,您可以同时使用/app和/pages文件夹。
然而,/app实际上是/pages文件夹的更新版本,是Next JS 13中的默认文件夹。在他们的文档中,他们描述如下:
App路由器位于一个名为app的新目录中。app目录与pages目录一起工作,以支持增量采用。这允许您将应用程序的某些路由选择到新行为,同时保留pages目录中以前的行为的其他路由。如果您的应用程序使用pages目录,请还参阅Pages Router文档。
这使我们了解到此时/pages文件夹被支持作为迁移到新/app文件夹的一种方式。此外,他们提到:
App路由器优先于Pages路由器。跨目录的路由不应解析为相同的URL路径,将会导致构建时错误以防止冲突。
从技术上讲,您可以同时使用两者,但我认为不应该这样做。
您可以访问此页面以获取更多信息:
Routing Fundamentals
英文:
According to NextJs documentation, you can use both /app and /pages folders.
However, /app is kind of the updated version of the /pages folder, the default folder in Next JS 13. In their documentation, they describe the following:
> The App Router works in a new directory named app. The app directory works alongside the pages directory to allow for incremental adoption. This allows you to opt some routes of your application into the new behavior while keeping other routes in the pages directory for previous behavior. If your application uses the pages directory, please also see the Pages Router documentation.
It leads us to understand that at this point the /pages folder is supported as a way to migrate to the new /app folder. Also, they mention:
> The App Router takes priority over the Pages Router. Routes across directories should not resolve to the same URL path and will cause a build-time error to prevent a conflict.
I would say that technically you can use both, but I think you shouldn't.
You can visit this page for more info:
Routing Fundamentals
答案2
得分: 1
如果您正在创建一个新的Next.js应用程序,我强烈建议使用/app文件夹。然而,如果您正在升级一个现有的应用程序,该应用程序使用/pages文件夹,您可以继续使用该文件夹结构。只需知道/app文件夹具有优先权,如果两者中存在相似路由,同时在同一项目中使用它们将引发冲突。
英文:
If you are creating a new Next.js application, I highly recommend using the /app folder. However, if you are upgrading an existing application that uses the /pages folder, you can continue to use that folder structure. Just know that the /app folder takes precedence, and using both in the same project will cause conflict if there is similar routes in the two.
答案3
得分: 0
I don't think you should mix these two routing mechanisms together. You either use App Router or Pages Router. Even the documentation is separated to not mix these two:
For better understanding the App Router, I recommend you read this page.
https://nextjs.org/blog/next-13-4#nextjs-app-router
App Router is more flexible and is the recommended way. So if you created a new app, stick to the App Router for sure, even though I still think the examples and documentation related to it are not organized well on their website.
英文:
I don't think you should mix these two routing machanisms together. You either use App Router or Pages Router. Even the documentation is separated to not mix these two:
For better undertanding the App Router I recommend you read this page.
https://nextjs.org/blog/next-13-4#nextjs-app-router
App Router is more flexible and is the recommended way. So if you created a new app stick to the App Router for sure even though I still think the examples and documentation related to it is not organised well on their website.
答案4
得分: 0
根据文档,您只能指向注释,不要在页面文件夹中创建 Index.tsx 文件,否则会引发冲突错误。
值得知道:尽管您可以在同一项目中使用两个路由器,但应用中的路由将优先于页面。我们建议在新项目中仅使用一个路由器以避免混淆。
非常感谢。
英文:
As per documentation you can only point to note dont create the Index.tsx file in the page folder
Otherwise it will throw conflict Error.
Good to know: Although you can use both routers in the same project, routes in app will be prioritized over pages. We recommend using only one router in your new project to avoid confusion.
Many thanks
答案5
得分: -3
是的,你可以在你的Next.js 13应用中同时使用/app和/pages文件夹。
/pages文件夹是创建Next.js应用页面的默认文件夹。在这里,你可以创建顶级页面,如首页、关于页面等。
另一方面,/app文件夹是Next.js 11中引入的新文件夹,允许你创建自定义的App组件,它是包裹在应用中的所有其他组件周围的顶级组件。你可以使用这个文件夹来自定义应用的布局和行为,比如添加全局CSS或JavaScript,设置自定义路由等。
因此,如果你需要自定义应用的行为或布局,你可以使用/app文件夹来创建自定义的App组件。如果需要创建页面,你可以像往常一样使用/pages文件夹。重要的是,你可以在/app和/pages文件夹中创建子文件夹,以组织你的组件和页面。
英文:
Yes, you can use both the /app and /pages folders in your Next.js 13 app.
The /pages folder is the default folder for creating pages in a Next.js app. It is where you can create your top-level pages, such as your home page, about page, etc.
The /app folder, on the other hand, is a new folder introduced in Next.js 11 that allows you to create a custom App component, which is the top-level component that wraps around all other components in your app. You can use this folder to customize your app's layout and behaviour, such as adding global CSS or JavaScript, setting up custom routing, etc.
So, if you need to customize your app's behaviour or layout, you can use the /app folder to create a custom App component. If you need to create pages, you can use the /pages folder as usual. It's important to note that you can create subfolders within both /app and /pages to organize your components and pages.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论