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

Avoid auto-reload on offline/500 template if page called directly for development #716

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
54 changes: 28 additions & 26 deletions wp-includes/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,35 +188,37 @@ function wp_service_worker_offline_page_reload() {

?>
<script type="module">
/**
* Listen to changes in the network state, reload when online.
* This handles the case when the device is completely offline.
*/
window.addEventListener('online', () => {
window.location.reload();
});

// Create a counter to implement exponential backoff.
let count = 0;

/**
* Check if the server is responding and reload the page if it is.
* This handles the case when the device is online, but the server is offline or misbehaving.
*/
async function checkNetworkAndReload() {
try {
const response = await fetch(location.href, {method: 'HEAD'});
// Verify we get a valid response from the server
if (response.status >= 200 && response.status < 500) {
window.location.reload();
return;
if (!new URLSearchParams(location.search.substr(1)).has("wp_error_template")) {
/**
* Listen to changes in the network state, reload when online.
* This handles the case when the device is completely offline.
*/
window.addEventListener('online', () => {
window.location.reload();
});

// Create a counter to implement exponential backoff.
let count = 0;

/**
* Check if the server is responding and reload the page if it is.
* This handles the case when the device is online, but the server is offline or misbehaving.
*/
async function checkNetworkAndReload() {
try {
const response = await fetch(location.href, {method: 'HEAD'});
// Verify we get a valid response from the server
if (response.status >= 200 && response.status < 500) {
window.location.reload();
return;
}
} catch {
// Unable to connect so do nothing.
}
} catch {
// Unable to connect so do nothing.
window.setTimeout(checkNetworkAndReload, Math.pow(2, count++) * 2500);
}
window.setTimeout(checkNetworkAndReload, Math.pow(2, count++) * 2500);
checkNetworkAndReload();
}
checkNetworkAndReload();
</script>
<?php
}
Expand Down