Skip to content

[go_router]If there is more than one route to match, use the first match #99833

Closed
flutter/packages
#1995

Description

@torikatsupg

Use case

If there is more than one route to match, use the first match.

The following was implemented using go_router.

  final _router = GoRouter(
    routes: [
      GoRoute(
        path: '/home',
        builder: (_, __) => const HomePage(),
        routes: [
          GoRoute(
            path: 'create',
            builder: (_, __) => const CreatePage(),
          ),
          GoRoute(
            path: ':id',
            builder: (_, __) => const DetailPage(),
          ),
      ),
    ],
    urlPathStrategy: UrlPathStrategy.path,
  );

I expected the following behavior.

/home/create -> CreatePage
/home/xxxx   -> DetailPage ("xxxx" is some string, but exclude "create")

But I opened /home/create, go_router displayed following error.

Exception: too many routes for location: /home/create
/home => create
/home => :id

go_router uses path_to_regexp internally.
Therefore, perhaps specifying a regular expression could solve this problem. However, we felt that this would not be concise and would be a hassle for the developer.

Proposal

This can be accomplished by removing go_router_delegate.dart l442-l463.

441
442    if (matchStacks.length > 1) {
443      final sb = StringBuffer()
444        ..writeln('too many routes for location: $location');
445
446      for (final stack in matchStacks) {
447        sb.writeln('\t${stack.map((m) => m.route.path).join(' => ')}');
448      }
449
450      throw Exception(sb.toString());
451    }
452
453    if (kDebugMode) {
454      assert(matchStacks.length == 1);
455      final match = matchStacks.first.last;
456      final loc1 = _addQueryParams(match.subloc, match.queryParams);
457      final uri2 = Uri.parse(location);
458      final loc2 = _addQueryParams(uri2.path, uri2.queryParameters);
459
460      // NOTE: match the lower case, since subloc is canonicalized to match the
461      // path case whereas the location can be any case
462      assert(loc1.toLowerCase() == loc2.toLowerCase(), '$loc1 != $loc2');
463    }
442

Activity

  1. added
    in triage
    p: go_router
    package
    c: proposal
    and removed
    in triage
    on Mar 10, 2022
  2. added
    P3
    on Mar 15, 2022
  3. nank1ro commented on Mar 17, 2022

    @nank1ro

    Really need this, would love if you do this.
    Please take a look at next js dynamic links documentation: https://nextjs.org/docs/routing/dynamic-routes#caveats
    They say:

    Predefined routes take precedence over dynamic routes, and dynamic routes over catch all routes.

    I completely agree with this behavior.

  4. rossheat commented on Apr 2, 2022

    @rossheat

    I wholeheartedly agree with the above sentiments.

    Right now, the following commonly seen URLs are not possible:

    twitter.com/[username]
    instagram.com/[username]
    etc.

  5. RaduGrama commented on Apr 5, 2022

    @RaduGrama

    Agree with the proposed behavior, most common routers pick the first found route, allowing the developer to prioritize.

  6. rhc commented on Apr 15, 2022

    @rhc

    Same here. Coming from a Web background, the ability to distinguish /projects/new and /projects/:id is crucial

  7. GauravCalidig commented on May 19, 2022

    @GauravCalidig

    Same here, I guess its just a matter of commenting code

  8. GauravCalidig commented on May 19, 2022

    @GauravCalidig

    Created a dummy repo here if anyone wants this and can't wait for package update.

  9. amerblackbird commented on May 26, 2022

    @amerblackbird

    Its crucial feature

  10. github-actions commented on Jun 10, 2022

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  11. locked as resolved and limited conversation to collaborators on Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3c: proposalp: go_routerpackage

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions