Skip to content

fix: route conflict when static and dynamic routes overlap #1826

@johan456789

Description

@johan456789

Description

This report is from discord. Route conflict when static and dynamic routes overlap; static route should take precedence

Given routes:

  • Static: /bar
  • Dynamic: /[foo]

Dart Frog CLI (v1.2.6) reports a route conflict when you run dart_frog dev or dart_frog build:

Route conflict detected. /<foo> and /bar both resolve to /<foo>.

Steps To Reproduce

  1. create a [foo].dart in /routes

    import 'package:dart_frog/dart_frog.dart';
    
    Future<Response> onRequest(RequestContext context, String foo) async {
      // Simulate catch-all for any dynamic segment; return 404 for demonstration.
      return Response.json(statusCode: 404, body: {
        'route': 'dynamic /[foo]',
        'param': foo,
        'message': 'not found (example)'
      });
    }
  2. create a bar.dart in /routes

    import 'package:dart_frog/dart_frog.dart';
    
    Response onRequest(RequestContext context) {
      return Response.json(body: {'route': 'static /bar'});
    }
  3. run dart_frog dev or dart_frog build

  4. see the error Route conflict detected. /<foo> and /bar both resolve to /<foo>.

Expected Behavior

There should be no route conflict. Static routes should take precedence over dynamic routes (similar to frameworks like Next.js). The dynamic route can act as a catch-all for all other values.

Next.js' doc:

Image

Additional Context

If the dynamic part is not at the end, the error is reported differently.

e.g. /bar/baz and /<foo>/baz:

Route conflict detected. /bar/baz and /<foo>/baz both resolve to /bar/baz.
Route conflict detected. /<foo>/baz and /bar/baz both resolve to /<foo>/baz.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions