Skip to content

Commit

Permalink
Merge pull request #716 from GoogleChromeLabs/add/exit-offline-reload…
Browse files Browse the repository at this point in the history
…ing-when-called-directly

Exit offline/500 page reloading if page called directly
  • Loading branch information
westonruter authored Mar 1, 2022
2 parents 9935ff9 + 11a8019 commit ad09837
Showing 1 changed file with 28 additions and 26 deletions.
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

0 comments on commit ad09837

Please sign in to comment.