Skip to content

Commit 7c31276

Browse files
authored
docs: show possible similar pages on 404 page (medusajs#13840)
* docs: show possible similar pages on 404 page * fix 404 page in api reference
1 parent 61565d2 commit 7c31276

File tree

11 files changed

+183
-14
lines changed

11 files changed

+183
-14
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { AcademicCapSolid, BookOpen } from "@medusajs/icons"
2+
import clsx from "clsx"
3+
import { CardList, H1, H2, MDXComponents, SimilarPages } from "docs-ui"
4+
import { config } from "../config"
5+
6+
const P = MDXComponents.p as React.FC<
7+
React.HTMLAttributes<HTMLParagraphElement>
8+
>
9+
const A = MDXComponents.a as React.FC<
10+
React.AnchorHTMLAttributes<HTMLAnchorElement>
11+
>
12+
13+
const NotFoundPage = () => {
14+
return (
15+
<div
16+
className={clsx(
17+
"w-full h-fit",
18+
"max-w-inner-content-xs sm:max-w-inner-content-sm md:max-w-inner-content-md",
19+
"lg:max-w-inner-content-lg xl:max-w-inner-content-xl xxl:max-w-inner-content-xxl",
20+
"xxxl:max-w-inner-content-xxxl",
21+
"px-docs_1 md:px-docs_4"
22+
)}
23+
>
24+
<H1>Page Not Found</H1>
25+
<P>The page you were looking for isn&apos;t available.</P>
26+
<P>
27+
If you&apos;re looking for Medusa v1 documentation, it&apos;s been moved
28+
to <A href="https://docs.medusajs.com/v1">docs.medusajs.com/v1</A>.
29+
</P>
30+
<P>
31+
If you think this is a mistake, please{" "}
32+
<A href="https://github.com/medusajs/medusa/issues/new?assignees=&labels=type%3A+docs&template=docs.yml">
33+
report this issue on GitHub
34+
</A>
35+
.
36+
</P>
37+
<SimilarPages />
38+
<H2>Other Resources</H2>
39+
<CardList
40+
itemsPerRow={2}
41+
items={[
42+
{
43+
title: "Get Started Docs",
44+
href: `${config.baseUrl}/learn`,
45+
icon: BookOpen,
46+
},
47+
{
48+
title: "Commerce Modules",
49+
href: `${config.baseUrl}/resources/commerce-modules`,
50+
icon: AcademicCapSolid,
51+
},
52+
]}
53+
/>
54+
</div>
55+
)
56+
}
57+
58+
export default NotFoundPage

www/apps/book/app/_not-found.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
hide_content_menu: true
33
---
44

5-
import { CardList, H1 } from "docs-ui"
5+
import { CardList, H1, SimilarPages } from "docs-ui"
66
import {
77
BookOpen,
88
AcademicCapSolid,
@@ -22,6 +22,10 @@ If you're looking for Medusa v1 documentation, it's been moved to [docs.medusajs
2222

2323
If you think this is a mistake, please [report this issue on GitHub](https://github.com/medusajs/medusa/issues/new?assignees=&labels=type%3A+docs&template=docs.yml).
2424

25+
<SimilarPages />
26+
27+
## Other Resources
28+
2529
<CardList
2630
itemsPerRow={2}
2731
items={[

www/apps/cloud/app/not-found.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
hide_content_menu: true
33
---
44

5-
import { CardList, H1 } from "docs-ui"
5+
import { CardList, H1, SimilarPages } from "docs-ui"
66
import {
77
BookOpen,
88
AcademicCapSolid,
@@ -18,14 +18,20 @@ export const metadata = {
1818

1919
The page you were looking for isn't available.
2020

21+
If you're looking for Medusa v1 documentation, it's been moved to [docs.medusajs.com/v1](https://docs.medusajs.com/v1).
22+
2123
If you think this is a mistake, please [report this issue on GitHub](https://github.com/medusajs/medusa/issues/new?assignees=&labels=type%3A+docs&template=docs.yml).
2224

25+
<SimilarPages />
26+
27+
## Other Resources
28+
2329
<CardList
2430
itemsPerRow={2}
2531
items={[
2632
{
2733
title: "Get Started Docs",
28-
href: "/",
34+
href: "!docs!/learn",
2935
icon: BookOpen
3036
},
3137
{

www/apps/resources/app/not-found.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
hide_content_menu: true
33
---
44

5-
import { CardList, H1 } from "docs-ui"
5+
import { CardList, H1, SimilarPages } from "docs-ui"
66
import {
77
BookOpen,
88
AcademicCapSolid,
@@ -18,8 +18,14 @@ export const metadata = {
1818

1919
The page you were looking for isn't available.
2020

21+
If you're looking for Medusa v1 documentation, it's been moved to [docs.medusajs.com/v1](https://docs.medusajs.com/v1).
22+
2123
If you think this is a mistake, please [report this issue on GitHub](https://github.com/medusajs/medusa/issues/new?assignees=&labels=type%3A+docs&template=docs.yml).
2224

25+
<SimilarPages />
26+
27+
## Other Resources
28+
2329
<CardList
2430
itemsPerRow={2}
2531
items={[

www/apps/ui/app/not-found.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
hide_content_menu: true
33
---
44

5-
import { CardList, H1 } from "docs-ui"
5+
import { CardList, H1, SimilarPages } from "docs-ui"
66
import {
77
BookOpen,
88
AcademicCapSolid,
@@ -18,14 +18,20 @@ export const metadata = {
1818

1919
The page you were looking for isn't available.
2020

21+
If you're looking for Medusa v1 documentation, it's been moved to [docs.medusajs.com/v1](https://docs.medusajs.com/v1).
22+
2123
If you think this is a mistake, please [report this issue on GitHub](https://github.com/medusajs/medusa/issues/new?assignees=&labels=type%3A+docs&template=docs.yml).
2224

25+
<SimilarPages />
26+
27+
## Other Resources
28+
2329
<CardList
2430
itemsPerRow={2}
2531
items={[
2632
{
2733
title: "Get Started Docs",
28-
href: "/",
34+
href: "!docs!/learn",
2935
icon: BookOpen
3036
},
3137
{

www/apps/user-guide/app/not-found.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
hide_content_menu: true
33
---
44

5-
import { CardList, H1 } from "docs-ui"
5+
import { CardList, H1, SimilarPages } from "docs-ui"
66
import {
77
BookOpen,
88
AcademicCapSolid,
@@ -18,14 +18,20 @@ export const metadata = {
1818

1919
The page you were looking for isn't available.
2020

21+
If you're looking for Medusa v1 documentation, it's been moved to [docs.medusajs.com/v1](https://docs.medusajs.com/v1).
22+
2123
If you think this is a mistake, please [report this issue on GitHub](https://github.com/medusajs/medusa/issues/new?assignees=&labels=type%3A+docs&template=docs.yml).
2224

25+
<SimilarPages />
26+
27+
## Other Resources
28+
2329
<CardList
2430
itemsPerRow={2}
2531
items={[
2632
{
2733
title: "Get Started Docs",
28-
href: "/",
34+
href: "!docs!/learn",
2935
icon: BookOpen
3036
},
3137
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"use client"
2+
3+
import React from "react"
4+
import { useSimilarPages } from "../../hooks"
5+
import { H2 } from "../Heading"
6+
import { MDXComponents } from "../MDXComponents"
7+
8+
const P = MDXComponents.p as React.FC<
9+
React.HTMLAttributes<HTMLParagraphElement>
10+
>
11+
const Ul = MDXComponents.ul as React.FC<React.HTMLAttributes<HTMLUListElement>>
12+
const Li = MDXComponents.li as React.FC<React.HTMLAttributes<HTMLLIElement>>
13+
const A = MDXComponents.a as React.FC<
14+
React.AnchorHTMLAttributes<HTMLAnchorElement>
15+
>
16+
17+
export const SimilarPages = () => {
18+
const similarPages = useSimilarPages()
19+
20+
if (!similarPages.length) {
21+
return null
22+
}
23+
24+
return (
25+
<div>
26+
<H2>Similar Pages</H2>
27+
<P>Maybe you&apos;re looking for:</P>
28+
<Ul>
29+
{similarPages.map((page) => (
30+
<Li key={page.id}>
31+
<A href={page.url}>{page.title}</A>
32+
</Li>
33+
))}
34+
</Ul>
35+
</div>
36+
)
37+
}

www/packages/docs-ui/src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export * from "./Search/Suggestions/Item"
7676
export * from "./Select"
7777
export * from "./Sidebar"
7878
export * from "./Sidebar/Item"
79+
export * from "./SimilarPages"
7980
export * from "./SourceCodeLink"
8081
export * from "./SplitLists"
8182
export * from "./Table"

www/packages/docs-ui/src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export * from "./use-request-runner"
1616
export * from "./use-scroll-utils"
1717
export * from "./use-search-navigation"
1818
export * from "./use-select"
19+
export * from "./use-similar-pages"
1920
export * from "./use-tabs"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use client"
2+
3+
import { usePathname } from "next/navigation"
4+
import { useSearch } from "../../providers"
5+
import { useEffect, useState } from "react"
6+
import { HitType } from "../../components"
7+
8+
type SimilarPage = {
9+
id: string
10+
title: string
11+
url: string
12+
}
13+
14+
export const useSimilarPages = () => {
15+
const pathname = usePathname()
16+
const { searchClient, selectedIndex } = useSearch()
17+
const [similarPages, setSimilarPages] = useState<SimilarPage[]>([])
18+
19+
useEffect(() => {
20+
void searchClient
21+
.search<HitType>({
22+
requests: [
23+
{
24+
query: pathname.split("/").join(" ").trim(),
25+
indexName: selectedIndex,
26+
hitsPerPage: 3,
27+
distinct: true,
28+
},
29+
],
30+
})
31+
.then(({ results }) => {
32+
const hits = "hits" in results[0] ? results[0].hits : []
33+
const pages = hits.map((hit) => ({
34+
id: hit.objectID,
35+
title: hit.hierarchy.lvl1 || hit.hierarchy.lvl0 || "Untitled",
36+
url: hit.url,
37+
}))
38+
setSimilarPages(pages)
39+
})
40+
.catch(() => {
41+
setSimilarPages([])
42+
})
43+
}, [pathname, searchClient])
44+
45+
return similarPages
46+
}

0 commit comments

Comments
 (0)