Skip to content

Commit 0a54072

Browse files
committed
redirect to search page if no match found
1 parent d08e5d9 commit 0a54072

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

apify-docs-theme/src/theme/NotFound.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from 'react';
21
import { PageMetadata } from '@docusaurus/theme-common';
32
import Layout from '@theme/Layout';
3+
import React from 'react';
44

55
export default function NotFound() {
66
return (

apify-docs-theme/src/theme/SearchPage/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from '@docusaurus/theme-common/internal';
1818
import Translate, { translate } from '@docusaurus/Translate';
1919
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
20+
import { useLocation } from '@docusaurus/router';
2021
import {
2122
useAlgoliaThemeConfig,
2223
useSearchResultUrlProcessor,
@@ -120,6 +121,8 @@ function SearchPageContent() {
120121
const documentsFoundPlural = useDocumentsFoundPlural();
121122
const docsSearchVersionsHelpers = useDocsSearchVersionsHelpers();
122123
const [searchQuery, setSearchQuery] = useSearchQueryString();
124+
const location = useLocation();
125+
const notFound = new URLSearchParams(location.search).get('not-found');
123126
const initialSearchResultState = {
124127
items: [],
125128
query: null,
@@ -302,7 +305,14 @@ function SearchPageContent() {
302305
</Head>
303306

304307
<div className="container margin-vert--lg">
305-
<h1>{getTitle()}</h1>
308+
{notFound ? (
309+
<>
310+
<h1>Page Not Found</h1>
311+
<h2>{getTitle()}</h2>
312+
</>
313+
) : (
314+
<h1>{getTitle()}</h1>
315+
)}
306316

307317
<form className="row" onSubmit={(e) => e.preventDefault()}>
308318
<div

apify-docs-theme/static/js/custom.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function scrollOpenApiSidebarItemIntoView() {
7676
}
7777

7878
function redirectOpenApiDocs() {
79-
const { hash, pathname } = new URL(window.location.href);
79+
const { hash, pathname, origin } = new URL(window.location.href);
8080

8181
// TODO change to '/api/v2'
8282
if (pathname.replace(/\/$/, '') !== '/api/v2-new') {
@@ -86,13 +86,21 @@ function redirectOpenApiDocs() {
8686
const sidebarItems = document.querySelectorAll('[data-altids]');
8787

8888
if (hash.startsWith('#/reference/') || hash.startsWith('#tag/')) {
89+
let matched = false;
90+
8991
for (const item of sidebarItems) {
9092
const ids = item.getAttribute('data-altids').split(',');
93+
9194
if (ids.find((variant) => variant === hash)) {
95+
matched = true;
9296
item.click();
9397
setTimeout(() => scrollOpenApiSidebarItemIntoView(), 200);
9498
}
9599
}
100+
101+
if (!matched) {
102+
window.location.href = `${origin}/search?q=${hash.slice(1)}&not-found=1`;
103+
}
96104
}
97105
}
98106

0 commit comments

Comments
 (0)