Skip to content

Commit 288f5e0

Browse files
committed
Improve logic for the callback functions
1 parent a48ce55 commit 288f5e0

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

templates/switchers.js

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,55 +58,62 @@ const _create_language_select = (current_language) => {
5858

5959
const _navigate_to_first_existing = (urls) => {
6060
// Navigate to the first existing URL in urls.
61-
const url = urls.shift();
62-
if (urls.length === 0 || url.startsWith('file:///')) {
63-
window.location.href = url;
64-
return;
61+
for (const url of urls) {
62+
if (url.startsWith('file:///')) {
63+
window.location.href = url;
64+
return;
65+
}
66+
fetch(url)
67+
.then((response) => {
68+
if (response.ok) {
69+
window.location.href = url;
70+
}
71+
})
72+
.catch((err) => void err);
6573
}
66-
fetch(url)
67-
.then((response) => {
68-
if (response.ok) {
69-
window.location.href = url;
70-
} else {
71-
navigate_to_first_existing(urls);
72-
}
73-
})
74-
.catch((err) => {
75-
void err;
76-
navigate_to_first_existing(urls);
77-
});
74+
75+
// if all else fails, redirect to the d.p.o root
76+
window.location.href = '/';
7877
};
7978

8079
const _on_version_switch = (event) => {
81-
const selected_version = event.target.value + '/';
82-
const url = window.location.href;
83-
const new_url = url.replace(
84-
_CURRENT_PREFIX,
85-
'/' + _CURRENT_LANGUAGE + selected_version,
86-
);
87-
if (new_url !== url) {
80+
const selected_version = event.target.value;
81+
// English has no language prefix.
82+
const new_prefix_en = `/${selected_version}/`;
83+
const new_prefix =
84+
_CURRENT_LANGUAGE === 'en'
85+
? new_prefix_en
86+
: `/${_CURRENT_LANGUAGE}/${selected_version}/`;
87+
if (_CURRENT_PREFIX !== new_prefix) {
88+
// Try the following pages in order:
89+
// 1. The current page in the current language with the new version
90+
// 2. The current page in English with the new version
91+
// 3. The documentation home in the current language with the new version
92+
// 4. The documentation home in English with the new version
8893
_navigate_to_first_existing([
89-
new_url,
90-
url.replace(_CURRENT_PREFIX, '/' + selected_version),
91-
'/' + _CURRENT_LANGUAGE + selected_version,
92-
'/' + selected_version,
93-
'/',
94+
window.location.href.replace(_CURRENT_PREFIX, new_prefix),
95+
window.location.href.replace(_CURRENT_PREFIX, new_prefix_en),
96+
new_prefix,
97+
new_prefix_en,
9498
]);
9599
}
96100
};
97101

98102
const _on_language_switch = (event) => {
99-
let selected_language = event.target.value + '/';
100-
const url = window.location.href;
101-
if (selected_language === 'en/')
102-
// Special 'default' case for English.
103-
selected_language = '';
104-
let new_url = url.replace(
105-
_CURRENT_PREFIX,
106-
'/' + selected_language + _CURRENT_VERSION,
107-
);
108-
if (new_url !== url) {
109-
_navigate_to_first_existing([new_url, '/']);
103+
const selected_language = event.target.value;
104+
// English has no language prefix.
105+
const new_prefix =
106+
selected_language === 'en'
107+
? `/${_CURRENT_VERSION}/`
108+
: `/${selected_language}/${_CURRENT_VERSION}/`;
109+
if (_CURRENT_PREFIX !== new_prefix) {
110+
// Try the following pages in order:
111+
// 1. The current page in the new language with the current version
112+
// 2. The documentation home in the new language with the current version
113+
_navigate_to_first_existing([
114+
window.location.href.replace(_CURRENT_PREFIX, new_prefix),
115+
new_prefix,
116+
]);
110117
}
111118
};
112119

0 commit comments

Comments
 (0)