Next.js / Basic: routes and execution boundaries / Lesson 7
Route groups and shared layouts
Organize App Router files with route groups while preserving URLs and understanding layout boundaries.
On this page
Lesson goals
- Map route group folders to public URLs.
- Apply a shared nested layout.
- Detect collisions and root layout transitions.
- Map route groups and nested layouts to public paths without conflicts.
- Separate shared shell lifetime from server authorization and direct-entry behavior.
Organize without adding a URL segment
A route group is a parenthesized folder in the App Router. Its name organizes files but is omitted from the public URL. It can group routes that share a nested layout without adding an extra path component. Start by writing the expected URL beside each page file; this makes it easier to spot collisions before moving existing routes.
Map a page to its URL
Place this page at app/(public)/about/page.jsx. Its URL is /about, not /public/about, and the nested layout wraps it beneath the existing root layout. A second page in another group that also resolves to /about conflicts with this one. The framework must build the route tree to verify the real application configuration.
export default function AboutPage() {
return <main><h1>About</h1><p>Welcome to the workshop.</p></main>;
}
About
Welcome to the workshop.
The page contributes the About content at the URL derived without the group folder name.
Understand layout boundaries
A single root layout can enclose several groups with different nested layouts. Applications may instead define multiple root layouts, but navigation between different root layouts causes a full page load. Check the official convention for the installed version and ensure the home route has an appropriate root layout. A group organizes routes; it does not authorize access to data.
Verify routes and navigation
Build the route tree in a controlled project and request /about directly, then navigate to it from another route. Confirm the intended layout, heading and public URL. Search for pages in other groups that resolve to the same path. If multiple root layouts are used, observe the transition behavior and verify that required state is restored rather than assuming client navigation preserves it.
Map organizational groups to public paths
Parenthesized route groups organize App Router files without adding their names to the URL. For example, app/(public)/about/page.tsx maps to /about, not /public/about. This can group routes that share a layout or ownership convention while keeping public addresses independent of internal organization. Draw the complete file tree and derive each public path before deciding whether a refactor preserves links.
Two groups can therefore conflict even though their folder names differ: app/(public)/about/page.tsx and app/(marketing)/about/page.tsx both resolve to /about. Choose distinct public segments or remove the duplicate. The course uses the dated Next 16.4.3 App Router baseline with cacheComponents:false. These file trees are synthetic, unexecuted fixtures; verify actual routes in a compatible isolated build.
Use layouts to define shared UI scope
A layout wraps the pages and nested layouts below it and receives their rendered content as children. Put a section shell in the appropriate group or segment when its pages share navigation and presentation. A root layout supplies the required document structure for its root. Multiple root layouts can be useful for genuinely different shells, but navigation between routes using different root layouts causes a full page load according to the documented route-group behavior.
A shared layout can be preserved during client navigation within its shared subtree. That is useful for persistent UI state, but it is not a promise that every route transition remounts the shell or refreshes every permission decision. Test the actual navigation and state lifetime in the configured application. Choose page-local or shared ownership for a draft based on its required lifetime, not on where it is easiest to import.
Keep layout presentation separate from authority
A layout may display navigation or user context, but a visible shell is not a server authorization boundary. A direct request, Route Handler or Server Action can reach data or a mutation without relying on a previous client navigation through the shell. Authenticate and authorize at the actual data and mutation entry points, using current identity and object eligibility. A route group name such as protected is organizational; it does not enforce access by itself.
If a user changes account while a shared shell remains mounted, define how the application resets account-scoped state and obtains current permissions. Do not rely on a stale UI flag to protect a record. Test the same target with eligible and ineligible synthetic callers, including direct entry and a mutation that bypasses the visible page. Keep sensitive data out of client props and shared caches unless the recipient and audience are explicitly authorized.
Verify both mapping and integration
A pure route-tree model can identify that two parenthesized groups produce the same /about path. It cannot prove that Next built the project, that a layout rendered the intended shell or that the host served a deep link. Run an isolated compatible integration for route conflicts and navigation when those claims matter, and use a browser to observe state preservation and focus. Record the file tree, version and actual observations.
Include a conflicting pair, a unique nested path, a page outside a section layout and direct access by an ineligible caller. Verify that an organizational group does not accidentally become an authorization assumption. A local model can support its mapping rule only; server policy, cache audience and client payload require separate evidence. The snippets below are unexecuted examples, not claims of a Next build or deployment.
20 worked examples
1. Group omitted from URL
Problem: Map an organized page to its public path.
- The parenthesized catalog folder is a route group.
- The books folder supplies the public segment.
File: app/(catalog)/books/page.tsx
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The public route is /books, not /catalog/books.
Why this result follows: A route group organizes files without adding a URL segment.
2. Conflicting groups
Problem: Detect two pages resolving to one public path.
- Neither parenthesized group contributes its name to the URL.
- Both remaining paths contain the same about segment.
app/(public)/about/page.tsx
app/(marketing)/about/page.tsx
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: Both files resolve to /about and therefore conflict.
Why this result follows: Distinct internal group names do not create distinct public paths.
3. Nested shared layout
Problem: Identify a shell for a section subtree.
- The page is a descendant of both layout files.
- The group name does not add a URL segment.
app/layout.tsx
app/(catalog)/layout.tsx
app/(catalog)/books/page.tsx
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The books page is wrapped by the catalog group layout and the root layout.
Why this result follows: Layout ancestry controls the shared shell while the group remains absent from the URL.
4. Group is not authorization
Problem: Assess a direct request to a grouped page.
- The parenthesized folder is an organizational convention.
- A direct request still reaches a server entry point requiring current authority.
File: app/(protected)/account/page.tsx
Caller: unauthenticated
Server policy: authentication required
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The server must reject or redirect the caller according to its policy; the group name alone does not protect the page.
Why this result follows: Authorization must be enforced by server logic rather than a folder label.
5. Different root layouts
Problem: Predict navigation between separate root shells.
- The two routes do not share the same root layout.
- Crossing root-layout boundaries changes the document shell.
Route A uses root layout in (shop)
Route B uses root layout in (admin)
User navigates from A to B
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: Navigation between these separate root layouts causes a full page load under the documented behavior.
Why this result follows: Separate root layouts have different navigation lifetime consequences.
6. Unique grouped paths
Problem: Map two pages that do not collide.
- The group names are omitted from both paths.
- The remaining about and settings segments are different.
app/(public)/about/page.tsx
app/(account)/settings/page.tsx
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The public paths are /about and /settings, so these two pages do not conflict.
Why this result follows: Unique public segments keep the grouped pages distinct.
7. Nested dynamic path
Problem: Map a grouped dynamic course page.
- The catalog group adds no segment.
- The courses folder and dynamic id form the remaining path.
File: app/(catalog)/courses/[id]/page.tsx
Example id: c-1
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The example public path is /courses/c-1.
Why this result follows: Route groups and dynamic segments each contribute according to their own conventions.
8. Section layout does not wrap sibling
Problem: Identify a page outside a nested shell.
- The books page is inside the catalog subtree.
- The about page is not a descendant of that layout.
app/(catalog)/layout.tsx
app/(catalog)/books/page.tsx
app/(public)/about/page.tsx
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The catalog layout wraps books but not the about page in the other group.
Why this result follows: A nested layout applies only to pages within its descendant route subtree.
9. Root layout and grouped page
Problem: Trace two layers of layout wrapping.
- Both layouts are ancestors of the page.
- The root supplies the outer application shell.
app/layout.tsx
app/(public)/layout.tsx
app/(public)/about/page.tsx
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The about page receives the public group layout inside the root layout.
Why this result follows: Nested layout ancestry determines the wrapper order.
10. Folder move preserves URL but changes shell
Problem: Assess a page moved between groups.
- Neither group name appears in the URL.
- The page now belongs to a different layout subtree.
Before: app/(public)/about/page.tsx
After: app/(marketing)/about/page.tsx
Both groups have different layouts
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The public path remains /about, but its layout and navigation behavior can change.
Why this result follows: URL equivalence does not imply identical layout ancestry.
11. Named segment changes URL
Problem: Compare a group with a normal folder.
- The parenthesized group is omitted from the original URL.
- The ordinary public folder contributes a path segment.
Before: app/(public)/about/page.tsx
After: app/public/about/page.tsx
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The path changes from /about to /public/about.
Why this result follows: Parentheses distinguish organizational groups from public segments.
12. Shared sidebar state
Problem: Reason about sibling pages under one layout.
- Both pages use the same layout boundary.
- A preserved client state owner is not recreated for that transition.
Shared layout owns sidebarOpen=true
Navigate between two descendant pages in that layout
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The sidebar state can persist if the shared layout is preserved during client navigation.
Why this result follows: A shared layout can span multiple page transitions.
13. Page-local draft
Problem: Choose a narrower state owner for an editor.
- The required draft lifetime ends before the shared shell’s lifetime.
- Putting it in the layout could preserve it longer than intended.
Requirement: draft resets when leaving the edit page
Shared layout persists across sibling navigation
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: Keep the draft in the page or feature owner and verify its reset behavior.
Why this result follows: State placement should match the required lifetime.
14. Direct protected request
Problem: Check authority without prior shell navigation.
- A direct request does not depend on a previous client transition.
- The folder label does not enforce identity.
Caller requests /account directly
Group name: (protected)
Session: absent
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The server must apply its authentication policy even though the route is in a protected-named group.
Why this result follows: Access control belongs at the server entry and data boundaries.
15. Account switch in shared shell
Problem: Prevent stale account-scoped display in a preserved shared shell.
- The shell’s persistence can outlive the old account context.
- A’s selection is not automatically appropriate for B.
Layout remains visible
Account changes from A to B
Sidebar contains A-specific selection
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: Reset or refresh the account-scoped selection under an explicit policy and recheck server authority.
Why this result follows: Shared UI lifetime must not silently extend private state across identities.
16. Async parameter in grouped page
Problem: Resolve a dynamic segment before using it.
- The page awaits the framework-provided params value.
- The group organization does not make the id trusted.
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params
return <h1>Course {id}</h1>
}
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: For id c-1 the intended heading is Course c-1; domain validation and authorization remain necessary.
Why this result follows: Asynchronous resolution and authority are separate from route grouping.
17. Two groups with unique child segments
Problem: Avoid a conflict by changing a public path deliberately.
- Both group names are omitted.
- The ordinary company folder remains in the second URL.
app/(public)/about/page.tsx -> /about
app/(marketing)/company/about/page.tsx -> /company/about
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The paths are distinct because the second has the public company segment.
Why this result follows: Unique remaining segments avoid this route collision.
18. Direct deep-link host
Problem: Separate source mapping from deployed delivery.
- No server response is recorded in the fixture.
- Deployment routing and assets can fail independently of a tree diagram.
Tree maps /books
Direct GET /books on intended host: not tested
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The source tree suggests a route, but actual host delivery remains unverified.
Why this result follows: A mapping model does not replace a host integration check.
19. Link after file move
Problem: Review a public URL change in navigation.
- The file move changed the public destination.
- The existing link still names the old path.
Old path: /about
New file path maps to /company/about
Existing Link still points to /about
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: Update links or provide a deliberate redirect according to the product contract, then test direct and client navigation.
Why this result follows: Route refactors must account for their public navigation contract.
20. Mapping model limit
Problem: Interpret a passing local conflict check.
- The fixture evaluates a mapping rule outside Next.
- The framework build was not executed.
Pure model: two files map to /about
Next build: not run
This contextual example is not executed; a compatible Next project and isolated host are required where applicable.
Expected result: The model identifies a likely route collision but does not establish an actual build diagnostic.
Why this result follows: Evidence should be reported at the boundary that was actually exercised.
Common errors and fixes
A group name appears in the requested URL.
- Why it happens
- The folder convention was mistaken for a URL segment.
- How to fix it
- Use the path derived from the non-group folders.
- Prevent it next time
- Record the expected URL for each page.
The build reports conflicting routes.
- Why it happens
- Two groups contain pages that resolve to the same path.
- How to fix it
- Give the pages distinct public paths or remove the duplicate.
- Prevent it next time
- Review the complete route map before moving files.
Navigation unexpectedly reloads the page.
- Why it happens
- The routes belong to different root layouts.
- How to fix it
- Decide whether a shared root layout fits the design.
- Prevent it next time
- Test transitions across root layout boundaries.
20 practice exercises
1. What URL does app/(public)/about/page.jsx serve?
Show hint
Remove the parenthesized organizational group when deriving the URL.
Show worked solution
It serves /about. The parenthesized group organizes files but does not add public to the URL. Verify the route in the built application.
The public path is /about because (public) organizes files without contributing a URL segment. Verify the route in the actual built application and distinguish the mapping model from host delivery.
Check your solution
- The URL does not include public.
- The built route is verified separately.
2. Why can two groups not both contain an about page at the same level?
Show hint
Compare the public paths after omitting both group names.
Show worked solution
Both would resolve to /about, creating a route conflict. Choose unique public paths or remove the duplication.
Two about pages at the same public level both map to /about even when their groups have different names. Choose distinct public paths or remove the duplication, then verify the configured build.
Check your solution
- Both original mappings are /about.
- The revised tree has unique public paths.
3. Map app/(shop)/products/page.tsx to its public URL and explain the group.
Show hint
Parentheses mark an organizational group.
Show worked solution
The page maps to /products because shop is omitted from the URL. Verify the mapping in a compatible build and check that no other group defines the same public path.
Check your solution
- The group name is absent from the URL.
- The products segment remains.
4. Two groups each contain contact/page.tsx at the same public level. What happens?
Show hint
Remove group names before comparing paths.
Show worked solution
Both pages resolve to /contact and conflict. Choose a distinct public segment for one route or remove the duplicate, then run the actual build in an isolated project.
Check your solution
- Both mappings are /contact.
- The conflict is resolved deliberately.
5. For a grouped dynamic course page, map app/(catalog)/courses/[id]/page.tsx using id c-2.
Show hint
Combine public and dynamic segments.
Show worked solution
The example path is /courses/c-2 because catalog is omitted and id supplies c-2. The server must still validate the parameter and authorize the requested course.
Check your solution
- The group is absent from the path.
- The dynamic id is checked at the server boundary.
6. A books page sits under a catalog layout and an about page under a public group. Which shell applies?
Show hint
Trace layout ancestry.
Show worked solution
The catalog layout wraps its books descendants, while the about page outside that subtree does not receive it. Test both pages in the configured app and verify their intended navigation.
Check your solution
- Books receives the catalog shell.
- About uses its own applicable layout.
7. A page moves from (public) to (marketing) but keeps /about. Is every behavior unchanged?
Show hint
The layout ancestry may change.
Show worked solution
No. The public URL can remain /about while the page gains a different shell, state lifetime or error boundary. Review and test those relationships after the move.
Check your solution
- The URL remains /about.
- The new layout behavior is verified.
8. A folder changes from (public) to public. What happens to an about page URL?
Show hint
Only the grouped folder is omitted.
Show worked solution
The path changes from /about to /public/about because the ordinary folder contributes a segment. Update links or define a deliberate redirect and test direct entry.
Check your solution
- The new segment appears in the path.
- Old links have an intentional outcome.
9. A shared layout owns an expanded sidebar across sibling pages. What should a test observe?
Show hint
The layout may persist during client navigation.
Show worked solution
Navigate between the sibling pages and verify whether the sidebar remains expanded under the configured application. Do not use the shell’s lifetime as proof of current authorization.
Check your solution
- The actual state lifetime is observed.
- Server access checks remain independent.
10. A draft should reset when leaving one editor page. Should it always live in the shared layout?
Show hint
Match owner lifetime to the draft.
Show worked solution
No. Keep it in the page or feature owner when its required lifetime ends on leaving, and test leaving and returning. Promote it only if persistence across routes is intentional.
Check your solution
- The draft has a defined owner.
- Its reset matches the product rule.
11. A route is inside a group named protected. Does that enforce authentication?
Show hint
The folder name is organizational.
Show worked solution
No. Authenticate and authorize at the server data or mutation boundary, including direct requests. Test an unauthenticated caller rather than relying on the group label.
Check your solution
- The server applies current authority.
- The group name grants no permission.
12. A shared shell survives an account switch. What account-scoped state needs review?
Show hint
The shell can outlive one identity.
Show worked solution
Reset or refresh the previous account’s selection and obtain current permissions according to an explicit policy. Test that account B does not see account A’s private state.
Check your solution
- Old account state is cleared or revalidated.
- Current server permission is checked.
13. Navigation crosses two distinct root layouts. What behavior should be expected and tested?
Show hint
The document shell changes.
Show worked solution
The documented route-group behavior uses a full page load across different root layouts. Test the actual transition and ensure users do not lose a draft unexpectedly.
Check your solution
- The root layouts are identified.
- Draft and navigation behavior are verified.
14. A grouped dynamic page receives params as a promise. What must it do before using id?
Show hint
Resolve the framework value.
Show worked solution
Await params in the async page, validate the id’s shape and authorize the current caller for the record. Group placement does not make the parameter trustworthy.
Check your solution
- The parameter is awaited.
- Validation and authorization are separate.
15. Two groups produce /about. How can one path become unique without deleting a page?
Show hint
Add a deliberate public segment.
Show worked solution
Place one page under a normal segment such as company so its path becomes /company/about, then update navigation and any redirect policy. Verify the two resulting routes in a build.
Check your solution
- The public paths differ.
- Links reflect the intended addresses.
16. A route-tree model says /books exists. Does that prove the deployed host serves it?
Show hint
Source mapping and delivery differ.
Show worked solution
No. Test the exact candidate with a direct /books request and refresh on the intended host, including asset responses. Report the local mapping separately from observed delivery.
Check your solution
- The direct route is checked.
- The model claim is not overstated.
17. An existing Link still points to /about after the page moves to /company/about. What next?
Show hint
The public contract changed.
Show worked solution
Update the link or provide a deliberate redirect according to the product policy. Test old and new direct URLs and client navigation so users do not reach an unintended failure.
Check your solution
- The old path has an intended outcome.
- The new destination works.
18. A group layout displays a user name. Can it be the only permission check for a Server Action?
Show hint
Actions are independently callable.
Show worked solution
No. Resolve the current identity and authorize the target inside the action or shared server domain boundary. Test a direct unauthorized call even if the layout hides the control.
Check your solution
- The action denies unauthorized access.
- The layout is treated as presentation.
19. Two sibling pages share a layout but use different query filters. Where should query parsing live?
Show hint
A persistent shell is not necessarily a fresh page parser.
Show worked solution
Parse and validate the query at the appropriate page or client boundary according to the selected API, and test transitions and direct entry. Do not treat a preserved layout as an automatic freshness guarantee.
Check your solution
- Query values are validated.
- Direct and sibling navigation are checked.
20. A local mapping test finds a conflict but no Next build ran. What can be reported?
Show hint
State the evidence boundary.
Show worked solution
Report that the model maps both files to the same public path and that the actual framework build has not been executed. Run the isolated build when a framework diagnostic is required.
Check your solution
- The model result is specific.
- Unrun framework behavior is not claimed.
Key takeaways
- Route group names do not appear in URLs.
- Nested layouts can be shared within a group.
- Check route collisions and root layout transitions.
- Parenthesized group names do not add URL segments.
- Different groups can still resolve to the same conflicting path.
- A layout or group name is not an access-control rule.