Skip to content

Commit

Permalink
Don't try to populate version switcher w/ relative path on local stat…
Browse files Browse the repository at this point in the history
…ic site (#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]>
  • Loading branch information
dstansby and drammock committed Jan 24, 2024
1 parent 3e44d1c commit c04f042
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ async function fetchVersionSwitcherJSON(url) {
var result = new URL(url);
} catch (err) {
if (err instanceof TypeError) {
if (!window.location.origin) {
// window.location.origin is null for local static sites
// (ie. window.location.protocol == 'file:')
//
// TODO: Fix this to return the static version switcher by working out
// how to get the correct path to the switcher JSON file on local static builds
return null;
}
// assume we got a relative path, and fix accordingly. But first, we need to
// use `fetch()` to follow redirects so we get the correct final base URL
const origin = await fetch(window.location.origin, { method: "HEAD" });
Expand Down Expand Up @@ -556,9 +564,13 @@ if (hasVersionsJSON && (hasSwitcherMenu || wantsWarningBanner)) {
const data = await fetchVersionSwitcherJSON(
DOCUMENTATION_OPTIONS.theme_switcher_json_url
);
populateVersionSwitcher(data, versionSwitcherBtns);
if (wantsWarningBanner) {
showVersionWarningBanner(data);
// TODO: remove the `if(data)` once the `return null` is fixed within fetchVersionSwitcherJSON.
// We don't really want the switcher and warning bar to silently not work.
if (data) {
populateVersionSwitcher(data, versionSwitcherBtns);
if (wantsWarningBanner) {
showVersionWarningBanner(data);
}
}
}

Expand Down

0 comments on commit c04f042

Please sign in to comment.