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

Add service worker streaming #85

Merged
merged 25 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c4c5d13
Rename error-response-handling to navigation-routing
westonruter Oct 11, 2018
7b8e0bf
Adopt streaming logic first prototyped in AMP plugin
westonruter Oct 11, 2018
fcec82a
Hook up streams
westonruter Oct 12, 2018
6ae18b2
Add 500/offline error handling to streams
westonruter Oct 12, 2018
348840f
WIP: DOM Diffing
westonruter Oct 13, 2018
85e2efd
Pull back diffing logic to just target subset of AMP support
westonruter Oct 13, 2018
e7e4522
Prevent streaming from applying in admin
westonruter Oct 13, 2018
baf00f1
Update meta from body fragment and add scripts
westonruter Oct 13, 2018
52a71f6
WIP: Improve DOM diffing
westonruter Oct 14, 2018
992f479
Reduce amount of code needed to apply DOM changes
westonruter Oct 14, 2018
720d187
Prevent unnecessarily removing elements
westonruter Oct 14, 2018
ba3b3a4
Allow loading element to be customized
westonruter Oct 14, 2018
1811ec9
Populate comment nodes in header
westonruter Oct 14, 2018
2f760b6
Prevent streaming on the login screen and other PHP endpoints
westonruter Oct 15, 2018
293bfdb
Prevent streaming responses to direct fragment navigation
westonruter Oct 15, 2018
831bcb6
Fix handling of streaming body for URL that redirects
westonruter Oct 15, 2018
16852cf
WIP: Streaming non-AMP
westonruter Oct 16, 2018
0e6985a
WIP: Streaming non-AMP (2)
westonruter Oct 18, 2018
51fd288
Finish eliminating dependency on AMP
westonruter Oct 18, 2018
9855b53
Skip registering query var for fragment since causes front-page query…
westonruter Oct 18, 2018
9bfe7cc
Improve method ordering and phpdoc
westonruter Oct 18, 2018
3b11698
Add missing jsdoc, remove bad DOMDocument arg, clean up
westonruter Oct 18, 2018
7da958d
Prevent sending streamed responses when navigation preload happens
westonruter Oct 18, 2018
270ab2c
Expand pattern blacklist for non-template URLs
westonruter Oct 19, 2018
65d74ca
Fix copying request props
westonruter Oct 19, 2018
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
1 change: 1 addition & 0 deletions wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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' );
Expand Down
11 changes: 10 additions & 1 deletion wp-includes/js/service-worker-navigation-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ wp.serviceWorker.routing.registerRoute( new wp.serviceWorker.routing.NavigationR

const handleResponse = ( response ) => {
if ( response.status < 500 ) {
return response;
if ( response.redirected ) {
const redirectedUrl = new URL( response.url );
redirectedUrl.searchParams.delete( STREAM_HEADER_FRAGMENT_QUERY_VAR );
const script = `<script>history.replaceState( {}, '', ${ JSON.stringify( redirectedUrl.toString() ) } );</script>`;
return response.text().then( ( body ) => {
return new Response( script + body );
Copy link
Collaborator Author

@westonruter westonruter Oct 16, 2018

Choose a reason for hiding this comment

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

@jeffposnick here's how I ended up dealing with the challenge of handling a streamed body that redirects.

The “Try Redirect” nav menu item points to an old post URL that redirects, so you can see the URL go from "old-post" to "new-post".

Copy link
Collaborator Author

@westonruter westonruter Oct 16, 2018

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

😍

} );
} else {
return response;
}
}
const channel = new BroadcastChannel( 'wordpress-server-errors' );
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not a blocker at this point, but it would be needed to define the scenario for browsers that do not support BroadcastChannel (https://caniuse.com/#search=broadcastchannel)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, this is captured in #81.


Expand Down
16 changes: 16 additions & 0 deletions wp-includes/service-workers.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,19 @@ function wp_prepare_stream_fragment_response( $dom, $fragment_name ) {

return $response;
}

/**
* 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 = get_query_var( WP_Service_Worker_Navigation_Routing_Component::STREAM_FRAGMENT_QUERY_VAR );
if ( $fragment ) {
$link = add_query_arg( WP_Service_Worker_Navigation_Routing_Component::STREAM_FRAGMENT_QUERY_VAR, sanitize_key( $fragment ), $link );
}
return $link;
}