Skip to content

Commit 8f25f4f

Browse files
authored
Fix search box on preview sites (#3148)
1 parent ec2bf88 commit 8f25f4f

File tree

1 file changed

+13
-9
lines changed
  • packages/typescriptlang-org/src/components/layout

1 file changed

+13
-9
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ export const SiteNav = (props: Props) => {
1919
const i = createInternational<typeof navCopy>(useIntl())
2020
const IntlLink = createIntlLink(props.lang)
2121
const loadDocSearch = () => {
22-
const isDev = document.location.host.includes('localhost')
23-
let customHandleSelected;
24-
25-
if (isDev) {
26-
customHandleSelected = (input, event, suggestion, datasetNumber, context) => {
27-
const urlToOpen = suggestion.url.replace("www.typescriptlang.org", "localhost:8000").replace("https", "http")
28-
window.open(urlToOpen)
29-
}
22+
const fixURL = (url: string) => {
23+
const u = new URL(url);
24+
if (u.host === document.location.host) return url;
25+
u.host = document.location.host;
26+
u.protocol = document.location.protocol;
27+
return u.toString();
3028
}
3129

3230
// @ts-ignore - this comes from the script above
@@ -35,7 +33,13 @@ export const SiteNav = (props: Props) => {
3533
apiKey: '37ee06fa68db6aef451a490df6df7c60',
3634
indexName: 'typescriptlang',
3735
inputSelector: '.search input',
38-
handleSelected: customHandleSelected,
36+
transformData: (hits) => {
37+
for (const hit of hits) {
38+
if (hit.url) hit.url = fixURL(hit.url);
39+
if (hit.url_without_anchor) hit.url_without_anchor = fixURL(hit.url_without_anchor);
40+
}
41+
return hits;
42+
}
3943
});
4044
}
4145
// This extra bit of mis-direction ensures that non-essential code runs after

0 commit comments

Comments
 (0)