Next.js Discord

Discord Forum

Folder Structure

Answered
Lord_TCG#9068 posted this in #help-forum
Open in Discord
Avatar
Just curious, testing out the app router for the first time. During setup the option for /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?
Image

22 Replies

Avatar
i found the approach of saving all routes to the app folder usefull when you start using nested or paralel routes
Avatar
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.
Avatar
oh true, too many times switching to app routers docs sorry
Avatar
@vr1 i found the approach of saving all routes to the app folder usefull when you start using nested or paralel routes
Avatar
Meaning you keep the content like components, assets, etc outside of the /app folder?
Answer
Avatar
:sunglasses_1:
Avatar
and no.
i use:
root/pages <-- routes and api
/root/components
/root/messyness
Avatar
well yes and no. yes I use pages router but thats indicating my web content
Avatar
@Lord_TCG#9068 Meaning you keep the content like components, assets, etc outside of the `/app` folder?
Avatar
Yes, I generate components and save them under a folder inside /src then I can call them by @/components/ui/button for example
Avatar
@vr1 Yes, I generate components and save them under a folder inside /src then I can call them by @/components/ui/button for example
Avatar
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 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??
Avatar
I've not seen the @ as a path identifier yet, but I dont know everything
Avatar
@goodsie I've not seen the @ as a path identifier yet, but I dont know everything
Avatar
Perhaps it's only useable as an import alias, I'll try a more mainstream example 🤔
Avatar
theres likely a parse expectation of @ for the above examples
Avatar
could you try the same paths without the "@/" in your layout.js?
Avatar
@vr1 could you try the same paths without the "@/" in your layout.js?
Avatar
Without @ it works fine using ../../etc