Skip to content

Commit 0385bb7

Browse files
committed
Fix 404 hero action and match URL display
1 parent b8572a8 commit 0385bb7

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

app/components/errors.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ function PossibleMatchesSection({
209209
function asNotFoundMatchFromResourceSearch(value: unknown): NotFoundMatch | null {
210210
if (!value || typeof value !== 'object') return null
211211
const v = value as Record<string, unknown>
212-
const url = typeof v.url === 'string' ? v.url.trim() : ''
212+
const url = normalizeNotFoundUrl(typeof v.url === 'string' ? v.url : '')
213213
if (!url) return null
214214
const titleRaw = typeof v.title === 'string' ? v.title.trim() : ''
215215
const segmentRaw = typeof v.segment === 'string' ? v.segment.trim() : ''
@@ -226,6 +226,19 @@ function asNotFoundMatchFromResourceSearch(value: unknown): NotFoundMatch | null
226226
}
227227
}
228228

229+
function normalizeNotFoundUrl(value: string) {
230+
const trimmed = value.trim()
231+
if (!trimmed) return ''
232+
if (trimmed.startsWith('/')) return trimmed
233+
if (!trimmed.startsWith('http')) return trimmed
234+
try {
235+
const parsed = new URL(trimmed)
236+
return `${parsed.pathname}${parsed.search}${parsed.hash}`
237+
} catch {
238+
return trimmed
239+
}
240+
}
241+
229242
function FourOhFour({
230243
articles,
231244
possibleMatches: possibleMatchesProp,
@@ -266,6 +279,11 @@ function FourOhFour({
266279
}, [fetcher.data])
267280

268281
const possibleMatches = possibleMatchesProp ?? fetchedMatches
282+
const heroAction = possibleMatches?.length ? (
283+
<ArrowLink to="#possible-matches">Possible matches</ArrowLink>
284+
) : (
285+
<ArrowLink href="/">Go home</ArrowLink>
286+
)
269287

270288
return (
271289
<ErrorPage
@@ -276,7 +294,7 @@ function FourOhFour({
276294
title: "404 - Oh no, you found a page that's missing stuff.",
277295
subtitle: `"${pathname}" is not a page on kentcdodds.com. So sorry.`,
278296
image: <MissingSomething className="rounded-lg" aspectRatio="3:4" />,
279-
action: <ArrowLink to="#possible-matches">Possible matches</ArrowLink>,
297+
action: heroAction,
280298
}}
281299
/>
282300
)

0 commit comments

Comments
 (0)