Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🔵 App Router – Pages, Layouts और Navigation  ·  Lesson 13

Route Groups और Private Folders

Routes organise क्यों करें?

असली website में तीन तरह के pages होते हैं: public marketing pages, login/register pages और admin panel। हर एक का look अलग चाहिए, पर URL में /marketing/about नहीं चाहिए। Route groups यही हल करते हैं।

Route groups: (folder)

Folder name को parentheses में लिखें। Next.js इसे organisation और layouts के लिए use करता है, पर URL में skip करता है।

📁 app/
  📁 (marketing)/              # group – not in URL
    layout.jsx              # big header + footer
    page.jsx                # /
    about/page.jsx          # /about
    courses/page.jsx        # /courses
  📁 (auth)/
    layout.jsx              # small centered card, no menu
    login/page.jsx          # /login
    register/page.jsx       # /register
  📁 (admin)/
    layout.jsx              # sidebar dashboard
    admin/page.jsx          # /admin
    admin/students/page.jsx # /admin/students
  📁 _lib/                     # private – never a route
    format.js
  layout.jsx                # root layout (html, body)
तीन groups, तीन layouts — URLs छोटे रहते हैं
FileURL
app/(marketing)/about/page.jsx/about
app/(auth)/login/page.jsx/login
app/(admin)/admin/students/page.jsx/admin/students

हर group का अलग layout

JSX – app/(auth)/layout.jsx + login page
// app/(auth)/layout.jsx — login & register pages share this
export default function AuthLayout({ children }) {
  return (
    <div style={{ minHeight: '100vh', display: 'grid', placeItems: 'center', background: '#eef2ff' }}>
      <div style={{ width: 360, background: '#fff', padding: 28, borderRadius: 16 }}>
        <h2>EduSite</h2>
        {children}
      </div>
    </div>
  )
}

// app/(auth)/login/page.jsx  →  URL is /login (not /(auth)/login)
export default function Login() {
  return (
    <form>
      <input placeholder="Email" />
      <input type="password" placeholder="Password" />
      <button>Login</button>
    </form>
  )
}

(marketing) pages

  • Menu के साथ पूरा header
  • Links वाला footer
  • Wide content

(auth) pages

  • Menu नहीं (कम distraction)
  • Centered card
  • वही root
हर group का अपना layout

Private folders: _folder

Underscore से शुरू होने वाला folder, जैसे _lib या _components, और उसके अंदर की हर चीज़ कभी route नहीं बनती, चाहे उसमें page.jsx हो। app/ के अंदर helper files रखने के लिए use करें।

Colocation: files साथ रखना

सिर्फ़ page.jsx (और route.js) folder को public बनाते हैं। इसलिए एक page का component उसी के बगल में रह सकता है:

Folder structure
app/
  courses/
    page.jsx            # the route
    CourseSearch.jsx    # used only by this page – not a route
    courses.module.css  # styles for this page
    [slug]/page.jsx
page.jsxpublic URLComponent.jsxpublic नहींstyle.module.csspublic नहीं
सिर्फ़ special files routes बनाती हैं

@/ import alias

JSON + JS – jsconfig.json
// jsconfig.json (created by create-next-app)
{
  "compilerOptions": {
    "paths": { "@/*": ["./*"] }
  }
}

// anywhere in the project:
import Header from '@/components/Header'      // ✅ clean
import Header from '../../../components/Header' // ❌ hard to read

आम गलतियाँ

  • दो groups का एक ही URL बनना ((a)/about और (b)/about) → build error।
  • (admin) से pages protect होने की उम्मीद → group सिर्फ़ organisation है; auth checks से protect करें (Auth lesson)।
  • ऊपर app/layout.jsx के बिना कई root layouts → तब हर group के layout में <html> और <body> होने चाहिए।

सारांश

  • (group) folders routes organise करते हैं और अलग layouts देते हैं, URL बदले बिना।
  • _private folders कभी routes नहीं बनते।
  • Page-specific components साथ रखें; साफ़ imports के लिए @/ use करें।
✅ Practice

Home और about pages को (marketing) में डालिए और centered-card layout के साथ (auth)/login बनाइए। Check कीजिए URLs अब भी /about और /login हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या route group URL बदलता है?
नहीं। (marketing) जैसा parentheses वाला folder URL में ignore होता है। app/(marketing)/about/page.jsx फिर भी /about ही है।
दो groups में एक ही page path हो तो?
Build error आता है, क्योंकि दोनों एक ही URL पर जाएँगे। जैसे (shop)/about और (marketing)/about दोनों नहीं हो सकते।
क्या app folder में components रख सकते हैं?
हाँ। सिर्फ़ page.jsx और route.js किसी folder को public बनाते हैं। बाकी files रखना safe है, और _folder से इरादा साफ़ हो जाता है।
← Next.js Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicloading, error और not-found Pages आपकी प्रगति: 13 / 37 (35%) · Lesson 14 / 37 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें