Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: search page for non latest doc versions #205

Merged
merged 5 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";
import { useHistory, useLocation } from "@docusaurus/router";
import { translate } from "@docusaurus/Translate";
import { useActiveVersion } from '@docusaurus/plugin-content-docs/client';
import { useDocsPreferredVersion } from '@docusaurus/theme-common';
import { useActivePlugin } from '@docusaurus/plugin-content-docs/client';

import { fetchIndexes } from "./fetchIndexes";
import { SearchSourceFactory } from "../../utils/SearchSourceFactory";
Expand Down Expand Up @@ -48,9 +49,11 @@ export default function SearchBar({
let {
siteConfig: { baseUrl },
} = useDocusaurusContext();
const activeVersion = useActiveVersion();
if (activeVersion && !activeVersion.isLast) {
baseUrl = activeVersion.path + "/";
const { pluginId } = useActivePlugin({failfast: true})!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Building the example website will fail due to this change.

➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/404" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/blog" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/blog/archive" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/blog/hello-world" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/blog/hola" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/blog/tags" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/blog/tags/docusaurus" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/blog/tags/facebook" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/blog/tags/hello" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/blog/tags/hola" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/blog/welcome" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/blog/writing-good-unit-tests" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/search" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs
➤ YN0000: [website]: Error: Can't find active docs plugin for "/docusaurus-search-local/" pathname, while it was expected to be found. Maybe you tried to use a docs feature that can only be used on a docs-related page? Existing docs plugin paths are: /docusaurus-search-local/docs

Copy link
Member

@weareoutman weareoutman Jun 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed that. However, I think we didn't fix #211

const { preferredVersion } = useDocsPreferredVersion(pluginId);
let versionUrl = baseUrl;
if (preferredVersion && !preferredVersion.isLast) {
versionUrl = preferredVersion.path + "/";
}
const history = useHistory();
const location = useLocation();
Expand All @@ -72,7 +75,7 @@ export default function SearchBar({
setLoading(true);

const [{ wrappedIndexes, zhDictionary }, autoComplete] = await Promise.all([
fetchIndexes(baseUrl),
fetchIndexes(versionUrl),
fetchAutoCompleteJS(),
]);

Expand Down Expand Up @@ -165,7 +168,7 @@ export default function SearchBar({
}
input.focus();
}
}, [baseUrl, history]);
}, [baseUrl, versionUrl, history]);

useEffect(() => {
if (!Mark) {
Expand Down Expand Up @@ -254,7 +257,7 @@ export default function SearchBar({
// We always clear these here because in case no match was selected the above history push wont happen
setInputValue("");
search.current?.autocomplete.setVal("");
}, [location.pathname, location.search, location.hash]);
}, [location.pathname, location.search, location.hash, history]);

return (
<div
Expand Down
22 changes: 18 additions & 4 deletions docusaurus-search-local/src/client/theme/SearchPage/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Layout from "@theme/Layout";
import Head from "@docusaurus/Head";
import Link from "@docusaurus/Link";
import { translate } from "@docusaurus/Translate";
import { usePluralForm } from "@docusaurus/theme-common";
import { usePluralForm, useDocsPreferredVersion } from "@docusaurus/theme-common";
import { useActivePlugin } from '@docusaurus/plugin-content-docs/client';

import useSearchQuery from "../hooks/useSearchQuery";
import { fetchIndexes } from "../SearchBar/fetchIndexes";
Expand All @@ -19,9 +20,22 @@ import styles from "./SearchPage.module.css";
import { concatDocumentPath } from "../../utils/concatDocumentPath";

export default function SearchPage(): React.ReactElement {
const {
return (
<Layout>
<SearchPageContent />
</Layout>
);
}

function SearchPageContent(): React.ReactElement {
let {
siteConfig: { baseUrl },
} = useDocusaurusContext();
const { pluginId } = useActivePlugin({failfast: true})!;
const { preferredVersion } = useDocsPreferredVersion(pluginId);
if (preferredVersion && !preferredVersion.isLast) {
baseUrl = preferredVersion.path + "/";
}
const { selectMessage } = usePluralForm();
const { searchValue, updateSearchPath } = useSearchQuery();
const [searchQuery, setSearchQuery] = useState(searchValue);
Expand Down Expand Up @@ -93,7 +107,7 @@ export default function SearchPage(): React.ReactElement {
}, [baseUrl]);

return (
<Layout>
<React.Fragment>
<Head>
{/*
We should not index search pages
Expand Down Expand Up @@ -161,7 +175,7 @@ export default function SearchPage(): React.ReactElement {
))}
</section>
</div>
</Layout>
</React.Fragment>
);
}

Expand Down