Skip to content

Regression: empty string paths no longer match #6941

@heygrady

Description

@heygrady

https://github.com/ReactTraining/react-router/blob/0c058adc5b56c6d9b409af5e4d8330c645fac00c/packages/react-router/modules/matchPath.js#L38

Broken by 7bd1407 (#6715)

It is possible to have an empty-string path. Prior to the breaking change this would essentially match anything.

I changed the check to be more specific:

// improve the bail to allow empty-string paths
if (!path && path !== '') return null;
// I added this test to `matchPath-test.js`
describe('with path=""', () => {
    it('returns correct url at ""', () => {
      const path = "";
      const pathname = "/";
      const match = matchPath(pathname, path);
      expect(match.url).toBe("/"); // <-- weird 🤷🏻‍♂️ 
    });

    it('returns correct url at "/somewhere/else"', () => {
      const path = "";
      const pathname = "/somewhere/else";
      const match = matchPath(pathname, path);
      expect(match.url).toBe(""); // <-- expected
    });
  });

I confirmed the above changes fixed the issue for my app.

I was relying on empty-string paths in my app to enable a custom route config where I needed a conceptual route that always matched. Upgrading to v5.0.1 broke my app and I couldn't figure out why.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions