Skip to content

Commit

Permalink
Merge pull request #215 from xwp/remove/streaming
Browse files Browse the repository at this point in the history
Eliminate experimental service worker streaming functionality
  • Loading branch information
westonruter authored Aug 21, 2019
2 parents 4e2b382 + 0ad3972 commit 261c15e
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 547 deletions.
1 change: 0 additions & 1 deletion .jshintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
wp-includes/js/service-worker.js
wp-includes/js/service-worker-navigation-routing.js
wp-includes/js/service-worker-precaching.js
wp-includes/js/service-worker-stream-combiner.js
wp-includes/js/service-worker-offline-commenting.js
4 changes: 0 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,6 @@ Here is a list of all available actions and filters added by the feature plugin.
- Has one argument with array of messages:
- `$default` The message to display on the default offline template;
- `$comment` The message to display on the offline template in case of commenting;
- `wp_streaming_header_precache_entry`: Filters what is precached to serve as the streaming header.
- Has one `$entry` param which is an array with the following arguments:
- `$url` URL to streaming header fragment.
- `$revision` Revision for the entry. Care must be taken to keep this updated based on the content that is output before the stream boundary.

**Actions:**
- `wp_front_service_worker`: Fires before serving the frontend service worker, when its scripts should be registered, caching routes established, and assets precached.
Expand Down
4 changes: 0 additions & 4 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,6 @@ Here is a list of all available actions and filters added by the feature plugin.
- Has one argument with array of messages:
- `$default` The message to display on the default offline template;
- `$comment` The message to display on the offline template in case of commenting;
- `wp_streaming_header_precache_entry`: Filters what is precached to serve as the streaming header.
- Has one `$entry` param which is an array with the following arguments:
- `$url` URL to streaming header fragment.
- `$revision` Revision for the entry. Care must be taken to keep this updated based on the content that is output before the stream boundary.

**Actions:**
- `wp_front_service_worker`: Fires before serving the frontend service worker, when its scripts should be registered, caching routes established, and assets precached.
Expand Down

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
add_action( 'wp_ajax_wp_service_worker', 'wp_ajax_wp_service_worker' );
add_action( 'wp_ajax_nopriv_wp_service_worker', 'wp_ajax_wp_service_worker' );
add_action( 'parse_query', 'wp_hide_admin_bar_offline' );
add_filter( 'old_slug_redirect_url', 'wp_service_worker_fragment_redirect_old_slug_to_new_url' );

add_action( 'wp_head', 'wp_add_error_template_no_robots' );
add_filter( 'pre_get_document_title', 'WP_Service_Worker_Navigation_Routing_Component::filter_title_for_streaming_header' );
add_action( 'error_head', 'wp_add_error_template_no_robots' );

add_action( 'admin_init', 'wp_disable_script_concatenation' );

add_action( 'template_redirect', 'WP_Service_Worker_Navigation_Routing_Component::start_output_buffering_stream_fragment', PHP_INT_MAX );

// Service Worker Updating.
add_action( 'admin_bar_menu', 'wp_service_worker_update_node', 999 );

Expand Down
62 changes: 1 addition & 61 deletions wp-includes/js/service-worker-navigation-routing.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* global NAVIGATION_PRELOAD, CACHING_STRATEGY, CACHING_STRATEGY_ARGS, NAVIGATION_ROUTE_ENTRY,
ERROR_OFFLINE_URL, ERROR_500_URL, SHOULD_STREAM_RESPONSE, STREAM_HEADER_FRAGMENT_URL, ERROR_500_BODY_FRAGMENT_URL,
ERROR_OFFLINE_BODY_FRAGMENT_URL, STREAM_HEADER_FRAGMENT_QUERY_VAR, NAVIGATION_BLACKLIST_PATTERNS, ERROR_MESSAGES */
ERROR_OFFLINE_URL, ERROR_500_URL, NAVIGATION_BLACKLIST_PATTERNS, ERROR_MESSAGES */

// IIFE is used for lexical scoping instead of just a braces block due to bug with const in Safari.
( () => {
const navigationPreload = NAVIGATION_PRELOAD;
const isStreamingResponses = SHOULD_STREAM_RESPONSE && wp.serviceWorker.streams.isSupported();
const errorMessages = ERROR_MESSAGES;
const navigationRouteEntry = NAVIGATION_ROUTE_ENTRY;

Expand Down Expand Up @@ -36,32 +34,11 @@ ERROR_OFFLINE_BODY_FRAGMENT_URL, STREAM_HEADER_FRAGMENT_QUERY_VAR, NAVIGATION_BL
* @return {Promise<Response>} Response.
*/
async function handleNavigationRequest( { event } ) {
const canStreamResponse = () => {
return isStreamingResponses && ! navigationPreload;
};

const handleResponse = ( response ) => {
if ( response.status < 500 ) {
if ( response.redirected && canStreamResponse() ) {
const redirectedUrl = new URL( response.url );
redirectedUrl.searchParams.delete( STREAM_HEADER_FRAGMENT_QUERY_VAR );
const script = `
<script id="wp-stream-fragment-replace-state">
history.replaceState( {}, '', ${ JSON.stringify( redirectedUrl.toString() ) } );
document.getElementById( 'wp-stream-fragment-replace-state' ).remove();
</script>
`;
return response.text().then( ( body ) => {
return new Response( script + body );
} );
}
return response;
}

if ( canStreamResponse() ) {
return caches.match( wp.serviceWorker.precaching.getCacheKeyForURL( 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()).
Expand Down Expand Up @@ -114,10 +91,6 @@ ERROR_OFFLINE_BODY_FRAGMENT_URL, STREAM_HEADER_FRAGMENT_QUERY_VAR, NAVIGATION_BL
};

const sendOfflineResponse = () => {
if ( canStreamResponse() ) {
return caches.match( wp.serviceWorker.precaching.getCacheKeyForURL( ERROR_OFFLINE_BODY_FRAGMENT_URL ) );
}

return caches.match( wp.serviceWorker.precaching.getCacheKeyForURL( ERROR_OFFLINE_URL ) ).then( function( response ) {
return response.text().then( function( text ) {
const init = {
Expand All @@ -133,39 +106,6 @@ ERROR_OFFLINE_BODY_FRAGMENT_URL, STREAM_HEADER_FRAGMENT_QUERY_VAR, NAVIGATION_BL
} );
};

if ( canStreamResponse() ) {
const streamHeaderFragmentURL = STREAM_HEADER_FRAGMENT_URL;
const precacheStrategy = new wp.serviceWorker.strategies.cacheFirst( {
cacheName: wp.serviceWorker.core.cacheNames.precache,
} );

const url = new URL( event.request.url );
url.searchParams.append( STREAM_HEADER_FRAGMENT_QUERY_VAR, 'body' );
const init = {
mode: 'same-origin',
};
const copiedProps = [
'method',
'headers',
'credentials',
'cache',
'redirect',
'referrer',
'integrity',
];
for ( const initProp of copiedProps ) {
init[ initProp ] = event.request[ initProp ];
}
const request = new Request( url.toString(), init );
const stream = wp.serviceWorker.streams.concatenateToResponse( [
precacheStrategy.makeRequest( { request: streamHeaderFragmentURL } ),
navigationCacheStrategy.makeRequest( { request } )
.then( handleResponse )
.catch( sendOfflineResponse ),
] );

return stream.response;
}
return navigationCacheStrategy.handle( { event, request: event.request } )
.then( handleResponse )
.catch( sendOfflineResponse );
Expand Down
141 changes: 0 additions & 141 deletions wp-includes/js/service-worker-stream-combiner.js

This file was deleted.

16 changes: 0 additions & 16 deletions wp-includes/service-workers.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,22 +277,6 @@ function wp_disable_script_concatenation() {
// phpcs:enable
}

/**
* Preserve stream fragment query param on canonical redirects.
*
* @since 0.2
*
* @param string $link New URL of the post.
* @return string URL to be redirected.
*/
function wp_service_worker_fragment_redirect_old_slug_to_new_url( $link ) {
$fragment = WP_Service_Worker_Navigation_Routing_Component::get_stream_fragment_query_var();
if ( $fragment ) {
$link = add_query_arg( WP_Service_Worker_Navigation_Routing_Component::STREAM_FRAGMENT_QUERY_VAR, $fragment, $link );
}
return $link;
}

/**
* Service worker styles.
*
Expand Down

0 comments on commit 261c15e

Please sign in to comment.