Skip to content

Commit 6e492c2

Browse files
authored
fix wrong switcher JSON loaded for dev docs (#2048)
closes #2035
1 parent 458415f commit 6e492c2

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

docs/user_guide/version-dropdown.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ The switcher requires the following configuration steps:
2929

3030
Below is a more in-depth description of each of these configuration steps.
3131

32+
.. warning::
33+
Modern web browsers do not allow loading data when the request is made from a page loaded via the ``file:`` protocol. This means that if you build your documentation locally and then directly open one of its pages in the browser, **the version switcher will not be allowed to load its JSON data source** and you'll end up with an empty switcher menu. Possible work-arounds are:
34+
35+
1. View the locally-built files through a local webserver (Python provides a builtin module for this: https://docs.python.org/3/library/http.server.html).
36+
2. Disabling your browser's security settings (either by passing a command-line flag when launching the browser, or through a browser add-on).
37+
3238

3339
Add a JSON file to define your switcher's versions
3440
--------------------------------------------------

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -406,23 +406,31 @@ async function checkPageExistsAndRedirect(event) {
406406
* @param {string} url The URL to load version switcher entries from.
407407
*/
408408
async function fetchVersionSwitcherJSON(url) {
409+
const currentPath = getCurrentUrlPath();
409410
// first check if it's a valid URL
410411
try {
411412
var result = new URL(url);
412413
} catch (err) {
413414
if (err instanceof TypeError) {
414-
if (!window.location.origin) {
415-
// window.location.origin is null for local static sites
416-
// (ie. window.location.protocol == 'file:')
417-
//
418-
// TODO: Fix this to return the static version switcher by working out
419-
// how to get the correct path to the switcher JSON file on local static builds
420-
return null;
415+
// Assume we got a relative path, and fix accordingly.
416+
if (window.location.protocol == "file:") {
417+
// Here instead of returning `null` we work out what the file path would be
418+
// anyway (same code path as for served docs), as a convenience to folks who
419+
// routinely disable CORS when they boot up their browser.
420+
console.info(
421+
"[PST] looks like you're viewing this site from a local filesystem, so " +
422+
"the version switcher won't work unless you've disabled CORS. See " +
423+
"https://pydata-sphinx-theme.readthedocs.io/en/stable/user_guide/version-dropdown.html",
424+
);
421425
}
422-
// assume we got a relative path, and fix accordingly. But first, we need to
423-
// use `fetch()` to follow redirects so we get the correct final base URL
424-
const origin = await fetch(window.location.origin, { method: "HEAD" });
425-
result = new URL(url, origin.url);
426+
const cutoff = window.location.href.indexOf(currentPath);
427+
// cutoff == -1 can happen e.g. on the homepage of locally served docs, where you
428+
// get something like http://127.0.0.1:8000/ (no trailing `index.html`)
429+
const origin =
430+
cutoff == -1
431+
? window.location.href
432+
: window.location.href.substring(0, cutoff);
433+
result = new URL(url, origin);
426434
} else {
427435
// something unexpected happened
428436
throw err;

0 commit comments

Comments
 (0)