Skip to content

Commit 86a5883

Browse files
authored
Fix: Algolia search not working when directly entering the /play page (microsoft#3203)
1 parent e28e9aa commit 86a5883

File tree

1 file changed

+21
-12
lines changed
  • packages/typescriptlang-org/src/components/layout

1 file changed

+21
-12
lines changed

packages/typescriptlang-org/src/components/layout/TopNav.tsx

+21-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { OpenInMyLangQuickJump } from "./LanguageRecommendation";
1818
export const SiteNav = (props: Props) => {
1919
const i = createInternational<typeof navCopy>(useIntl())
2020
const IntlLink = createIntlLink(props.lang)
21-
const loadDocSearch = () => {
21+
const loadDocSearch = (docsearch) => {
2222
const fixURL = (url: string) => {
2323
const u = new URL(url);
2424
if (u.host === document.location.host) return url;
@@ -47,26 +47,35 @@ export const SiteNav = (props: Props) => {
4747
useEffect(() => {
4848
setupStickyNavigation()
4949

50-
// @ts-ignore - this comes from the script above
51-
if (window.docsearch) {
52-
loadDocSearch();
53-
}
54-
if (document.getElementById("algolia-search")) return
55-
5650
const searchScript = document.createElement('script');
5751
searchScript.id = "algolia-search"
58-
const searchCSS = document.createElement('link');
5952

6053
searchScript.src = withPrefix("/js/docsearch.js");
6154
searchScript.async = true;
62-
searchScript.onload = () => {
63-
// @ts-ignore - this comes from the script above
64-
if (window.docsearch) {
65-
loadDocSearch();
55+
searchScript.onload = async () => {
56+
// @ts-ignore this comes from the script above
57+
let universalDocSearch = window.docsearch;
58+
59+
if (global.require) {
60+
universalDocSearch = await new Promise(resolve => {
61+
const re: any = global.require;
62+
re(['/js/docsearch.js'], (docsearch) => {
63+
resolve(docsearch);
64+
});
65+
});
66+
}
67+
68+
if (universalDocSearch) {
69+
loadDocSearch(universalDocSearch);
70+
}
71+
72+
if (!document.querySelector("#docsearch-css")) {
73+
const searchCSS = document.createElement('link');
6674

6775
searchCSS.rel = 'stylesheet';
6876
searchCSS.href = withPrefix('/css/docsearch.css');
6977
searchCSS.type = 'text/css';
78+
searchCSS.id = 'docsearch-css';
7079
document.body.appendChild(searchCSS);
7180

7281
document.getElementById("search-form")?.classList.add("search-enabled")

0 commit comments

Comments
 (0)