Folder Structure
Answered
Lord_TCG#9068 posted this in #help-forum
Just curious, testing out the app router for the first time. During setup the option for
Should all non route folders go into the app folder with a _name convention, or should you use a
/src
directory is offered. Should all non route folders go into the app folder with a _name convention, or should you use a
/src
above the /app
to keep all the components and assets and hooks etc? Functionally I don't think it changes anything from my understanding, is there any benefits either way?22 Replies
i found the approach of saving all routes to the app folder usefull when you start using nested or paralel routes
You can also refer to this:
https://nextjs.org/docs/pages/building-your-application/configuring/src-directory
https://nextjs.org/docs/pages/building-your-application/configuring/src-directory
either or
check the content
src Directory
As an alternative to having the special Next.js app or pages directories in the root of your project, Next.js also supports the common pattern of placing application code under the src directory.
This separates application code from project configuration files which mostly live in the root of a project, which is preferred by some individuals and teams.
To use the src directory, move the app Router folder or pages Router folder to src/app or src/pages respectively.
oh true, too many times switching to app routers docs sorry
@vr1 i found the approach of saving all routes to the app folder usefull when you start using nested or paralel routes
Meaning you keep the content like components, assets, etc outside of the
/app
folder?@goodsie
src Directory
As an alternative to having the special Next.js app or pages directories in the root of your project, Next.js also supports the common pattern of placing application code under the src directory.
This separates application code from project configuration files which mostly live in the root of a project, which is preferred by some individuals and teams.
To use the src directory, move the app Router folder or pages Router folder to src/app or src/pages respectively.
Yeah I know it's an alternative, I was more looking for people's opinions on what they do 🙂
@goodsie https://www.reddit.com/r/nextjs/comments/14fd1kl/do_you_use_src_folder_when_you_use_app_router/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
Noicee, thanks. 60/40 split near enough.
Do you use it?
Do you use it?
and no.
i use:
root/pages <-- routes and api
/root/components
/root/messyness
i use:
root/pages <-- routes and api
/root/components
/root/messyness
@goodsie and no.
i use:
root/pages <-- routes and api
/root/components
/root/messyness
Pages meaning the pages router?
well yes and no. yes I use pages router but thats indicating my web content
@Lord_TCG#9068 Meaning you keep the content like components, assets, etc outside of the `/app` folder?
Yes, I generate components and save them under a folder inside /src then I can call them by @/components/ui/button for example
@vr1 Yes, I generate components and save them under a folder inside /src then I can call them by @/components/ui/button for example
Oh nice, that's what I was leaning towards doing 🙂
I was about to make a new post on import aliases, but given your reply maybe you know. I was using the localFonts as a tester:
I setup the path in
I was about to make a new post on import aliases, but given your reply maybe you know. I was using the localFonts as a tester:
I setup the path in
jsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/fonts/*": ["src/fonts/*"]
}
}
}
and went to use them in the layout.js
import localFont from "next/font/local";
import "./globals.css";
const mediaSans = localFont({
src: [
{
path: "@/fonts/mediaSans/mediasans-regular.otf",
},
{
path: "@/fonts/mediaSans/mediasansextended-regular.otf",
},
{
path: "@/fonts/mediaSans/mediasanssemicondensed-regular.otf",
},
],
});
const sfPro = localFont({
src: [
{
path: "@/fonts/sfPro/SF-Pro-Display-Regular.otf",
},
{
path: "@/fonts/sfPro/SF-Pro-Text-Regular.otf",
},
],
});
export const metadata = {
title: "Page Title Here",
description: "Page description here",
};
export default function RootLayout({ children }) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
);
}
but I get errors: ⨯ src\app\layout.js
Module not found: Can't resolve './@/fonts/mediaSans/mediasans-regular.otf'
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
./node_modules/next/font/local/target.css?{"path":"src\\app\\layout.js","import":"","arguments":[{"src":[{"path":"@/fonts/mediaSans/mediasans-regular.otf"},{"path":"@/fonts/mediaSans/mediasansextended-regular.otf"},{"path":"@/fonts/mediaSans/mediasanssemicondensed-regular.otf"}]}],"variableName":"mediaSans"}
any ideas??I've not seen the @ as a path identifier yet, but I dont know everything
@goodsie I've not seen the @ as a path identifier yet, but I dont know everything
Perhaps it's only useable as an import alias, I'll try a more mainstream example 🤔
theres likely a parse expectation of @ for the above examples
could you try the same paths without the "@/" in your layout.js?
@vr1 could you try the same paths without the "@/" in your layout.js?
Without @ it works fine using
../../etc