Route Groups & Nested Layouts in Next.js
N نکست.جی‌اس

Route Groups & Nested Layouts in Next.js

Organize routes with (folders) that do not affect URLs and compose shared layouts.

BugCast Admin··۵ دقیقه مطالعه

Route groups use parentheses in folder names to organize files without changing the URL. Nested layouts share UI shells across route segments.

Common Interview Questions

Q: What is a route group?

Answer: Folders wrapped in () are omitted from the URL. Useful for splitting layouts or teams without nested paths.


app/
  (marketing)/
    about/page.tsx    → /about
  (shop)/
    products/page.tsx → /products

Q: Multiple root layouts?

Answer: Different route groups can have their own layout.tsx without sharing a parent — e.g., marketing vs dashboard with different html/body (rare, advanced).

Q: layout.tsx responsibilities?

Answer: Wrap child pages with persistent UI (nav, sidebar). State in layouts persists across navigations within the segment.

Q: template.tsx vs layout.tsx?

Answer: Templates remount on navigation (new key), resetting state. Layouts persist. Use templates for enter animations or keyed state reset.

Key Points for Interviews

  • Parentheses folders are organizational only

  • Layouts nest automatically down the tree

  • Colocate loading.tsx and error.tsx per segment

  • Split auth vs public sections with groups

  • Do not confuse groups with dynamic [segments]

#layouts#routing#interview

مطالب مرتبط