Route Groups

In the app directory, nested folders are typically mapped to URL paths. However, you can designate a folder as a Route Group to exclude it from the route's URL path. This is useful when you want to organize your routes into subdirectories without changing the URL structure.

This allows you to organize your route segments and project files into logical groups without affecting the URL path structure. It provides a convenient way to group related routes together and improve the overall organization of your application.

Route groups are useful for:

  • Organizing routes into groups e.g. by site section, intent, or team.
  • Enabling nested layouts in the same route segment level:
    • For example, you can have a layout.php file in the app/admin directory that is used for all routes in the admin group.

Convention

A route group can be created by wrapping a folder's name in parenthesis: (folderName)

Examples

Organize routes without affecting the URL path

To organize routes without affecting the URL, create a group to keep related routes together. The folders in parenthesis will be omitted from the URL (e.g. (marketing) or (shop)).

group-route

Even though routes inside (marketing) and (shop) share the same URL hierarchy, you can create a different layout for each group by adding a layout.php file inside their folders.

group-route

Opting specific segments into a layout

To opt specific routes into a layout, create a new route group (e.g. (shop)) and move the routes that share the same layout into the group (e.g. account and cart). The routes outside of the group will not share the layout (e.g. checkout).

group-route