diff --git a/wp-admin/error.php b/wp-admin/error.php index 5bd280eeb..f375fa879 100644 --- a/wp-admin/error.php +++ b/wp-admin/error.php @@ -26,22 +26,7 @@ esc_html__( 'Something went wrong which prevented WordPress from serving a response. Please check your error logs.', 'pwa' ) ); ob_start(); - ?> - - add_query_arg( 'code', 'offline', admin_url( 'admin-ajax.php?action=wp_error_template' ) ), // Upon core merge, this would use admin_url( 'error.php' ). - 'revision' => PWA_VERSION, // Upon core merge, this should be the core version. + 'revision' => $revision, // Upon core merge, this should be the core version. ); $server_error_precache_entry = array( 'url' => add_query_arg( 'code', '500', admin_url( 'admin-ajax.php?action=wp_error_template' ) ), // Upon core merge, this would use admin_url( 'error.php' ). - 'revision' => PWA_VERSION, // Upon core merge, this should be the core version. + 'revision' => $revision, // Upon core merge, this should be the core version. ); } diff --git a/wp-includes/js/service-worker-navigation-routing.js b/wp-includes/js/service-worker-navigation-routing.js index e851c95ad..a7c4b07da 100644 --- a/wp-includes/js/service-worker-navigation-routing.js +++ b/wp-includes/js/service-worker-navigation-routing.js @@ -5,8 +5,6 @@ wp.serviceWorker.routing.registerRoute( new wp.serviceWorker.routing.NavigationRoute( async function ( { event } ) { - const { url } = event.request; - let responsePreloaded = false; const canStreamResponse = () => { @@ -31,29 +29,63 @@ return response; } } - const channel = new BroadcastChannel( 'wordpress-server-errors' ); - - // Wait for client to request the error message. - channel.onmessage = ( event ) => { - if ( event.data && event.data.clientUrl && url === event.data.clientUrl ) { - response.text().then( ( text ) => { - channel.postMessage({ - requestUrl: url, - bodyText: text, - status: response.status, - statusText: response.statusText - }); - channel.close(); - } ); - } - }; - // Close the channel if client did not request the message within 30 seconds. - setTimeout( () => { - channel.close(); - }, 30 * 1000 ); + if ( canStreamResponse() ) { + return caches.match( ERROR_500_BODY_FRAGMENT_URL ); + } + + const originalResponse = response.clone(); + return response.text().then( function( responseBody ) { + + // Prevent serving custom error template if WordPress is already responding with a valid error page (e.g. via wp_die()). + if ( -1 !== responseBody.indexOf( '' ) ) { + return originalResponse; + } - return caches.match( canStreamResponse() ? ERROR_500_BODY_FRAGMENT_URL : ERROR_500_URL ); + return caches.match( ERROR_500_URL ).then( function( errorResponse ) { + + if ( ! errorResponse ) { + return response; + } + + return errorResponse.text().then( function( text ) { + let init = { + status: errorResponse.status, + statusText: errorResponse.statusText, + headers: errorResponse.headers + }; + + let body = text.replace( //, errorMessages.error ); + body = body.replace( + /()((?:.|\n)+?)()/, + ( details ) => { + if ( ! responseBody ) { + return ''; // Remove the details from the document entirely. + } + const src = 'data:text/html;base64,' + btoa( responseBody ); // The errorText encoded as a text/html data URL. + const srcdoc = responseBody + .replace( /&/g, '&' ) + .replace( /'/g, ''' ) + .replace( /"/g, '"' ) + .replace( //g, '>' ); + const iframe = ``; + details = details.replace( '{{{error_details_iframe}}}', iframe ); + // The following are in case the user wants to include the `; + details = details.replace( '{{{error_details_iframe}}}', iframe ); + // The following are in case the user wants to include the - - - + +