Skip to content
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

Attempt to reload current page in newly-selected version #60

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Resources/themes/default/doctum.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,20 @@ var Doctum = {
{# Enable the version switcher #}
var versionSwitcher = document.getElementById('version-switcher');
if (versionSwitcher !== null) {
versionSwitcher.addEventListener('change', function () {
window.location = this.value;
var currentVersion = versionSwitcher.options[versionSwitcher.selectedIndex].dataset.version;
versionSwitcher.addEventListener('change', function (event) {
var targetVersion = event.target.options[event.target.selectedIndex].dataset.version;
var candidateUrl = window.location.pathname.replace(currentVersion, targetVersion);
// Check if the page exists before redirecting to it
var testRequest = new XMLHttpRequest();
testRequest.open('HEAD', candidateUrl, false);
testRequest.send();
if (testRequest.status < 200 || testRequest.status > 399) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=200 && < 400

@andrewandante I think that's what you wanted?

Copy link
Contributor Author

@andrewandante andrewandante Apr 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@williamdes I think I just have line 88 and line 91 around the wrong way.

If the status is lower than 200 or higher than 399, that means the page "doesn't exist" and so we should default.

The pseudo-code I wrote has it that way around, not sure how I managed to mix it up 😅

window.location = candidateUrl;
} else {
// otherwise reroute to the home page of the new version
window.location = this.value;
}
});
}
{% endif %}
Expand Down
Loading