From 69378e67944ca3abd15e179621b5ca53903eecce Mon Sep 17 00:00:00 2001 From: Shenwei Wang Date: Sun, 12 Nov 2023 11:10:39 +0800 Subject: [PATCH] fix: fix SSR warning, closes #364 --- .../src/client/theme/SearchBar/SearchBar.tsx | 11 ++++++----- .../src/client/theme/hooks/useSearchQuery.ts | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx b/docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx index 1dc3d723..70adbbfe 100644 --- a/docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx +++ b/docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx @@ -7,7 +7,7 @@ import React, { } from "react"; import clsx from "clsx"; import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; -import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment"; +import useIsBrowser from "@docusaurus/useIsBrowser"; import { useHistory, useLocation } from "@docusaurus/router"; import { translate } from "@docusaurus/Translate"; import { @@ -59,6 +59,7 @@ interface SearchBarProps { export default function SearchBar({ handleSearchBarToggle, }: SearchBarProps): ReactElement { + const isBrowser = useIsBrowser(); const { siteConfig: { baseUrl }, } = useDocusaurusContext(); @@ -296,7 +297,7 @@ export default function SearchBar({ if (!Mark) { return; } - const keywords = ExecutionEnvironment.canUseDOM + const keywords = isBrowser ? new URLSearchParams(location.search).getAll(SEARCH_PARAM_HIGHLIGHT) : []; // A workaround to fix an issue of highlighting in code blocks. @@ -318,7 +319,7 @@ export default function SearchBar({ setInputValue(keywords.join(" ")); search.current?.autocomplete.setVal(keywords.join(" ")); }); - }, [location.search, location.pathname]); + }, [isBrowser, location.search, location.pathname]); const [focused, setFocused] = useState(false); @@ -349,7 +350,7 @@ export default function SearchBar({ ); // Implement hint icons for the search shortcuts on mac and the rest operating systems. - const isMac = ExecutionEnvironment.canUseDOM + const isMac = isBrowser ? /mac/i.test( (navigator as any).userAgentData?.platform ?? navigator.platform ) @@ -425,7 +426,7 @@ export default function SearchBar({ ✕ ) : ( - ExecutionEnvironment.canUseDOM && ( + isBrowser && (
{isMac ? "⌘" : "ctrl"} K diff --git a/docusaurus-search-local/src/client/theme/hooks/useSearchQuery.ts b/docusaurus-search-local/src/client/theme/hooks/useSearchQuery.ts index ce61612a..6038e205 100644 --- a/docusaurus-search-local/src/client/theme/hooks/useSearchQuery.ts +++ b/docusaurus-search-local/src/client/theme/hooks/useSearchQuery.ts @@ -6,7 +6,7 @@ */ import { useHistory, useLocation } from "@docusaurus/router"; -import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment"; +import useIsBrowser from "@docusaurus/useIsBrowser"; import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; const SEARCH_PARAM_QUERY = "q"; @@ -14,13 +14,14 @@ const SEARCH_PARAM_CONTEXT = "ctx"; const SEARCH_PARAM_VERSION = "version"; function useSearchQuery(): any { + const isBrowser = useIsBrowser(); const history = useHistory(); const location = useLocation(); const { siteConfig: { baseUrl }, } = useDocusaurusContext(); - const params = ExecutionEnvironment.canUseDOM ? new URLSearchParams(location.search) : null; + const params = isBrowser ? new URLSearchParams(location.search) : null; const searchValue = params?.get(SEARCH_PARAM_QUERY) || ""; const searchContext = params?.get(SEARCH_PARAM_CONTEXT) || ""; const searchVersion = params?.get(SEARCH_PARAM_VERSION) || "";