Skip to content

feat: add support for route groups#1563

Merged
marvinhagemeister merged 6 commits into
mainfrom
route-refactor
Aug 2, 2023
Merged

feat: add support for route groups#1563
marvinhagemeister merged 6 commits into
mainfrom
route-refactor

Conversation

@marvinhagemeister

@marvinhagemeister marvinhagemeister commented Aug 1, 2023

Copy link
Copy Markdown
Contributor

This PR adds support for route groups which allows you to use different layouts for routes in the same URL segment.

/routes
  /(marketing)
    /_layout.tsx   <-- only applies to about.tsx and career.tsx
    /about.tsx
    /career.tsx
  /(info)
    /_layout.tsx   <-- only applies to index.tsx and contact.tsx
    /index.tsx
    /contact.tsx

To be able to do this we need to keep track of the original route paths. Previously, matching _layout and _middleware was done via their URLPattern, but this doesn't work with groups anymore. With groups multiple _layout files can have the same URLPattern and it becomes ambiguous as to which file to pick.

To solve this we need to match on the original file path semantics instead of their resulting URLPattern. To do that the PR adds an additional property baseRoute to each route which is the normalised file path. When a route matches we can retrieve that (like /foo/(bar)/(bob)/about) and easily match the needed layout files.

The PR is a bit longer than I'd hoped. Most of the changes relate to changing the internal route signature from:

interface Route {
  [K in "GET" | "PATCH" ...]: ...
}

to

interface Route {
  baseRoute: string;
  methods: {
    [K in "GET" | "PATCH" ...]: ...
  }
}

Instead of making baseRoute just a string, I made it a nominal type. Noticed that I ran into a couple of errors during development where some values started with a /, others didn't etc. With the nominal type we can force it to have been parsed first and ensure it always has the expected format.

Fixes #1150 .

@marvinhagemeister

Copy link
Copy Markdown
Contributor Author

oh no, looks like I broke some tests 🙈

@marvinhagemeister marvinhagemeister marked this pull request as draft August 1, 2023 12:39
@marvinhagemeister marvinhagemeister marked this pull request as ready for review August 1, 2023 17:19
@marvinhagemeister

Copy link
Copy Markdown
Contributor Author

Alright, ready to review now 🎉

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, no comments. I love the approach of using a nominal type 👍

@marvinhagemeister marvinhagemeister merged commit e4d797a into main Aug 2, 2023
@marvinhagemeister marvinhagemeister deleted the route-refactor branch August 2, 2023 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Route grouping support

2 participants