Skip to content

Commit 7457ba1

Browse files
dstansbydrammock
authored andcommitted
Don't try to populate version switcher w/ relative path on local static site (pydata#1660)
* Don't try to populate version switcher on static sites * Fix comment spelling * Update src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js --------- Co-authored-by: Daniel McCloy <[email protected]>
1 parent bcf88b9 commit 7457ba1

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,14 @@ async function fetchVersionSwitcherJSON(url) {
347347
var result = new URL(url);
348348
} catch (err) {
349349
if (err instanceof TypeError) {
350+
if (!window.location.origin) {
351+
// window.location.origin is null for local static sites
352+
// (ie. window.location.protocol == 'file:')
353+
//
354+
// TODO: Fix this to return the static version switcher by working out
355+
// how to get the correct path to the switcher JSON file on local static builds
356+
return null;
357+
}
350358
// assume we got a relative path, and fix accordingly. But first, we need to
351359
// use `fetch()` to follow redirects so we get the correct final base URL
352360
const origin = await fetch(window.location.origin, { method: "HEAD" });
@@ -556,9 +564,13 @@ if (hasVersionsJSON && (hasSwitcherMenu || wantsWarningBanner)) {
556564
const data = await fetchVersionSwitcherJSON(
557565
DOCUMENTATION_OPTIONS.theme_switcher_json_url
558566
);
559-
populateVersionSwitcher(data, versionSwitcherBtns);
560-
if (wantsWarningBanner) {
561-
showVersionWarningBanner(data);
567+
// TODO: remove the `if(data)` once the `return null` is fixed within fetchVersionSwitcherJSON.
568+
// We don't really want the switcher and warning bar to silently not work.
569+
if (data) {
570+
populateVersionSwitcher(data, versionSwitcherBtns);
571+
if (wantsWarningBanner) {
572+
showVersionWarningBanner(data);
573+
}
562574
}
563575
}
564576

0 commit comments

Comments
 (0)