Skip to content

Commit 0cdb18a

Browse files
committed
fix: route search hits through React Router
Render DocSearch hits with React Router links so Algolia's absolute URLs can be normalized to relative paths and selected without reloading the app.
1 parent 465e2b0 commit 0cdb18a

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

__tests__/Search_.test.res

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ module ThrowsOnRender = {
4141
let make = (): React.element => failwith("search render exploded")
4242
}
4343

44+
module CurrentPath = {
45+
@react.component
46+
let make = () => {
47+
let location = ReactRouter.useLocation()
48+
<span> {React.string((location.pathname :> string))} </span>
49+
}
50+
}
51+
4452
// ---------------------------------------------------------------------------
4553
// markdownToHtml
4654
// ---------------------------------------------------------------------------
@@ -230,6 +238,23 @@ test("getContentHtml prefers crawler snippet markup over plain content", async (
230238
)
231239
})
232240

241+
test("hitComponent routes relative hit URLs through React Router", async () => {
242+
await viewport(1440, 500)
243+
244+
let hit = makeHit(~type_=Lvl1, ~url="/docs/manual/api/stdlib/list/")
245+
246+
let screen = await render(
247+
<ReactRouter.MemoryRouter initialEntries=["/"]>
248+
<CurrentPath />
249+
{Search.hitComponent({hit, children: React.null})}
250+
</ReactRouter.MemoryRouter>,
251+
)
252+
253+
await element(await screen->getByText("/"))->toBeVisible
254+
await (await screen->getByText("Test Page"))->click
255+
await element(await screen->getByText("/docs/manual/api/stdlib/list/"))->toBeVisible
256+
})
257+
233258
test(
234259
"getContentHtml falls back to plain content when crawler hit has no snippet result",
235260
async () => {

src/components/Search.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ let hitComponent = ({hit, children: _}: DocSearch.hitComponent): React.element =
253253
let contentHtml = getContentHtml(hit)
254254
let isChild = isChildHit(hit)
255255

256-
<a href={hit.url}>
256+
<ReactRouter.Link.String to=hit.url>
257257
<div className="DocSearch-Hit-Container">
258258
{isChild ? <Icon.DocTree /> : React.null}
259259
{isChild ? <Icon.DocHash /> : <Icon.DocPage />}
@@ -271,7 +271,7 @@ let hitComponent = ({hit, children: _}: DocSearch.hitComponent): React.element =
271271
</div>
272272
<Icon.DocSelect />
273273
</div>
274-
</a>
274+
</ReactRouter.Link.String>
275275
}
276276

277277
module ErrorBoundary = {

0 commit comments

Comments
 (0)