-
Notifications
You must be signed in to change notification settings - Fork 98
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
Offline commenting #92
Changes from all commits
7c74cba
93f5c5f
44b7747
ca07c6d
f6472eb
3cd1a33
7d67528
61b7b7f
df6b0dc
4e7381c
20bba99
8a62653
08250a5
78a7dcb
4da1a3f
69eb395
a3ce4b6
138b788
c4c88b3
de7b2eb
e631395
9594969
8c5dd63
544a747
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* global ERROR_MESSAGES, SITE_URL */ | ||
{ | ||
const queue = new wp.serviceWorker.backgroundSync.Queue( 'amp-wpPendingComments' ); | ||
const errorMessages = ERROR_MESSAGES; | ||
|
||
const commentHandler = ( { event } ) => { | ||
const clonedRequest = event.request.clone(); | ||
return fetch( event.request ).catch( () => { | ||
return clonedRequest.blob().then( ( body ) => { | ||
const queuedRequest = new Request( event.request.url, { | ||
method: event.request.method, | ||
headers: event.request.headers, | ||
mode: event.request.mode, | ||
credentials: event.request.credentials, | ||
referrer: event.request.referrer, | ||
redirect: 'follow', | ||
body: body | ||
} ); | ||
|
||
// Add request to queue. @todo Replace when upgrading to Workbox v4! | ||
queue.addRequest( queuedRequest ); | ||
|
||
const jsonBody = JSON.stringify( { 'message': errorMessages.comment } ); | ||
return new Response( jsonBody, { | ||
status: 202, | ||
statusText: 'Accepted', | ||
headers: { | ||
'Access-Control-Allow-Origin': SITE_URL, | ||
'Access-Control-Allow-Credentials': 'true', | ||
'Content-Type': 'application/json; charset=UTF-8', | ||
'Access-Control-Expose-Headers': 'AMP-Access-Control-Allow-Source-Origin', | ||
'AMP-Access-Control-Allow-Source-Origin': SITE_URL, | ||
'Cache-Control': 'no-cache, must-revalidate, max-age=0' | ||
} | ||
} ); | ||
} ); | ||
} ); | ||
}; | ||
|
||
wp.serviceWorker.routing.registerRoute( | ||
/\/wp-comments-post\.php\?.*_wp_amp_action_xhr_converted.*/, | ||
commentHandler, | ||
'POST' | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -358,11 +358,19 @@ public function serve( WP_Service_Worker_Scripts $scripts ) { | |
); | ||
} | ||
|
||
$scripts->register( | ||
'wp-offline-commenting', | ||
array( | ||
'src' => array( $this, 'get_offline_commenting_script' ), | ||
'deps' => array( 'wp-base-config' ), | ||
) | ||
); | ||
|
||
$scripts->register( | ||
'wp-navigation-routing', | ||
array( | ||
'src' => array( $this, 'get_script' ), | ||
'deps' => array( 'wp-base-config' ), | ||
'deps' => array( 'wp-base-config', 'wp-offline-commenting' ), | ||
) | ||
); | ||
|
||
|
@@ -424,6 +432,7 @@ public function serve( WP_Service_Worker_Scripts $scripts ) { | |
'BLACKLIST_PATTERNS' => wp_service_worker_json_encode( $this->get_blacklist_patterns() ), | ||
'SHOULD_STREAM_RESPONSE' => wp_service_worker_json_encode( $should_stream_response ), | ||
'STREAM_HEADER_FRAGMENT_QUERY_VAR' => wp_service_worker_json_encode( self::STREAM_FRAGMENT_QUERY_VAR ), | ||
'ERROR_MESSAGES' => wp_service_worker_json_encode( wp_service_worker_get_error_messages() ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @westonruter: Continued with the PR and addressed your comments, too. Question about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making it a method of the navigation routing component would probably be best. |
||
); | ||
} | ||
|
||
|
@@ -466,7 +475,7 @@ public function get_blacklist_patterns() { | |
* @return int Hook priority. A higher number means a lower priority. | ||
*/ | ||
public function get_priority() { | ||
return -99999; | ||
return 99; | ||
} | ||
|
||
/** | ||
|
@@ -484,4 +493,20 @@ public function get_script() { | |
$script | ||
); | ||
} | ||
|
||
/** | ||
* Get script for offline commenting requests. | ||
* | ||
* @return string Script. | ||
*/ | ||
public function get_offline_commenting_script() { | ||
$script = file_get_contents( PWA_PLUGIN_DIR . '/wp-includes/js/service-worker-offline-commenting.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | ||
$script = preg_replace( '#/\*\s*global.+?\*/#', '', $script ); | ||
|
||
return str_replace( | ||
array_keys( $this->replacements ), | ||
array_values( $this->replacements ), | ||
$script | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This todo can now be todone due to #98
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we'll have to wait until the related PR gets merged, too.