-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix wrong switcher JSON loaded for dev docs #2048
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -406,23 +406,28 @@ async function checkPageExistsAndRedirect(event) { | |
* @param {string} url The URL to load version switcher entries from. | ||
*/ | ||
async function fetchVersionSwitcherJSON(url) { | ||
const currentPath = getCurrentUrlPath(); | ||
// first check if it's a valid URL | ||
try { | ||
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 | ||
Comment on lines
-422
to
-423
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment about using |
||
const origin = await fetch(window.location.origin, { method: "HEAD" }); | ||
result = new URL(url, origin.url); | ||
// Assume we got a relative path, and fix accordingly. This also handles local | ||
// static sites (i.e., when window.location.protocol == 'file:'). Normally for | ||
// local static sites CORS policy will always block resource requests, so in | ||
// general the version switcher will always fail to populate if you just open up | ||
// the built HTML files (instead of spinning up a local server). Here instead of | ||
// returning `null` we work out what the file path would be anyway (same code path | ||
// as for served docs), as a convenience to folks who routinely disable CORS when | ||
// they boot up their browser. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI @dstansby, since you left the TODO item that I'm now removing. |
||
const cutoff = window.location.href.indexOf(currentPath); | ||
// cutoff == -1 can happen e.g. on the homepage of locally served docs, where you | ||
// get something like http://127.0.0.1:8000/ (no trailing `index.html`) | ||
const origin = | ||
cutoff == -1 | ||
? window.location.href | ||
: window.location.href.substring(0, cutoff); | ||
result = new URL(url, origin); | ||
} else { | ||
// something unexpected happened | ||
throw err; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we print that, or a link to that in the console when we see the
file://
protocol ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered doing a console.log but then I thought: pointing folks to this docs page is way easier than explaining how to view browser console. So to me seemed not worth it but I don't mind adding it if you think is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait I misunderstood you. You're thinking about folks who already are checking browser console when something goes wrong. Yeah, probably worth adding it for their benefit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's what I meant.
I was also going to point out, if we detect this we could literally fill the version switcher itself with this text; but that may be overkill.