From ce131ccd51b40f9494fca9f69f779797d5807682 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Mon, 27 Nov 2023 15:02:37 -0500 Subject: [PATCH 1/2] Fix issue when rendering Link/NavLink outside of matched routes --- .changeset/pretty-dolphins-relax.md | 5 +++ .../__tests__/link-href-test.tsx | 33 +++++++++++++++++++ packages/router/utils.ts | 9 +++-- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 .changeset/pretty-dolphins-relax.md diff --git a/.changeset/pretty-dolphins-relax.md b/.changeset/pretty-dolphins-relax.md new file mode 100644 index 0000000000..bd30b1c8ca --- /dev/null +++ b/.changeset/pretty-dolphins-relax.md @@ -0,0 +1,5 @@ +--- +"@remix-run/router": patch +--- + +Fix `relative="path"` issue when rendering `Link`/`NavLink` outside of matched routes diff --git a/packages/react-router-dom/__tests__/link-href-test.tsx b/packages/react-router-dom/__tests__/link-href-test.tsx index 51de95ac07..a961fc4afa 100644 --- a/packages/react-router-dom/__tests__/link-href-test.tsx +++ b/packages/react-router-dom/__tests__/link-href-test.tsx @@ -927,4 +927,37 @@ describe(" href", () => { ); warnSpy.mockRestore(); }); + + test("renders fine when used outside a router context", () => { + let renderer: TestRenderer.ReactTestRenderer; + TestRenderer.act(() => { + renderer = TestRenderer.create( + + Route + + Path + + + ); + }); + + let anchors = renderer.root.findAllByType("a"); + expect(anchors.map((a) => ({ href: a.props.href, text: a.children }))) + .toMatchInlineSnapshot(` + [ + { + "href": "/route", + "text": [ + "Route", + ], + }, + { + "href": "/path", + "text": [ + "Path", + ], + }, + ] + `); + }); }); diff --git a/packages/router/utils.ts b/packages/router/utils.ts index 06e9a1db7f..b0303548fa 100644 --- a/packages/router/utils.ts +++ b/packages/router/utils.ts @@ -1202,9 +1202,12 @@ export function resolveTo( if (toPathname == null) { from = locationPathname; } else if (isPathRelative) { - let fromSegments = routePathnames[routePathnames.length - 1] - .replace(/^\//, "") - .split("/"); + let fromSegments = + routePathnames.length === 0 + ? [] + : routePathnames[routePathnames.length - 1] + .replace(/^\//, "") + .split("/"); if (toPathname.startsWith("..")) { let toSegments = toPathname.split("/"); From 539234c6cc5b1d51a804c6c583abaf3c1caf5af3 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Mon, 4 Dec 2023 17:48:49 -0500 Subject: [PATCH 2/2] Update packages/react-router-dom/__tests__/link-href-test.tsx --- packages/react-router-dom/__tests__/link-href-test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-router-dom/__tests__/link-href-test.tsx b/packages/react-router-dom/__tests__/link-href-test.tsx index a961fc4afa..387a4150f1 100644 --- a/packages/react-router-dom/__tests__/link-href-test.tsx +++ b/packages/react-router-dom/__tests__/link-href-test.tsx @@ -928,7 +928,7 @@ describe(" href", () => { warnSpy.mockRestore(); }); - test("renders fine when used outside a router context", () => { + test("renders fine when used outside a route context", () => { let renderer: TestRenderer.ReactTestRenderer; TestRenderer.act(() => { renderer = TestRenderer.create(