
Attempting to switch from one version to another when navigating <old_version>/applications/hr/employees.html led to the user being redirected to /applications/documentation/<new_version>/hr/employees.html because `hr` was interpreted as a version string, and thus the generated target URLs of the version switcher were all wrong. This commit ensures the redirection now falls back onto the original target URL when all the generated ones point to a 404. closes odoo/documentation#5037 Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
31 lines
1.2 KiB
JavaScript
31 lines
1.2 KiB
JavaScript
(function ($) {
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
// Enable fallback URLs for broken redirects from the version or language switchers.
|
|
_prepareSwitchersFallbacks();
|
|
});
|
|
|
|
/**
|
|
* Add event listeners on links in the version and language switchers.
|
|
*
|
|
* If a link is clicked, the user is redirected to the closest fallback URL (including the
|
|
* original target URL) that is available.
|
|
*/
|
|
const _prepareSwitchersFallbacks = () => {
|
|
document.querySelectorAll('a.o_switcher_item').forEach(element => {
|
|
element.addEventListener('click', async event => {
|
|
if (element.hasAttribute('href')) {
|
|
const targetUrl = element.getAttribute('href');
|
|
if (!targetUrl.startsWith('/')) { // Don't test for valid URLs if in localhost.
|
|
event.preventDefault();
|
|
const fallbackUrls = await _generateFallbackUrls(targetUrl);
|
|
const fallbackUrl = await _getFirstValidUrl(fallbackUrls) ?? targetUrl;
|
|
window.location.href = fallbackUrl;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
})();
|