-
Notifications
You must be signed in to change notification settings - Fork 100
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 page reloading in offline.php templates #697
Conversation
* Automatically reload the offline page once the user is back online.
Codecov Report
@@ Coverage Diff @@
## develop #697 +/- ##
=============================================
- Coverage 19.05% 19.00% -0.06%
Complexity 324 324
=============================================
Files 55 55
Lines 2062 2068 +6
=============================================
Hits 393 393
- Misses 1669 1675 +6
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
wp-includes/template.php
Outdated
* @since 0.7 | ||
*/ | ||
function wp_service_worker_offline_page_reload() { | ||
if ( ! is_offline() ) { |
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.
It would be useful to also do the reload in the case of server being down.
Also, something else that comes to mind: what happens if the error page is served in response to a POST request? If so, each reload will cause a re-POST message from the browser and that's probably not desired.
if ( ! is_offline() ) { | |
if ( ! is_offline() || ! is_500() || 'GET' !== $_SERVER['REQUEST_METHOD'] ) { |
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.
Thanks for pointing this out. For sure we need to check the method of the request. I have implemented these changes.
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.
As part of #728, I just realized that this check will never do anything:
pwa-wp/wp-includes/template.php
Lines 181 to 184 in 124baab
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' !== $_SERVER['REQUEST_METHOD'] ) { | |
return; | |
} | |
This is because the offline template is always fetched via a GET
request by the service worker when it is installing, so it will never not be GET
. We need an alternative way of preventing the code from running on responses to non-GET requests.
Contrary to what I thought above, each reload does not cause the browser re-POST message: #728 (comment)
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.
I believe I've addressed this via #763
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.
See #763
Making fetch requests to the base origin i.e. fetch('.') will drop the query params. Co-authored-by: Weston Ruter <[email protected]>
Note that testing this is a bit tricky because the script is part of the precached template, so without a version bump it won't show up. |
Let's just add a PHPUnit test to ensure that the cases are covered for this function outputting the |
@westonruter Tests added 👍 |
@westonruter Can you please confirm if this PR needs no more improvements? |
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 is good, yes.
I just realized one thing that should be done to improve this. Namely, if you try going to ( new URLSearchParams(location.search.substr(1)) ).has( 'wp_error_template' ) |
Thanks for pointing it out and it's a great finding. I just checked it after your comment. We should add it else it will cause infinite reloads during development. @westonruter Should I push a commit for this? |
@thelovekesh Yes please! Thanks. |
Fixes #438
Automatically reload the offline page once the user is back online.
Tasks Done:
offline-page-reload.mp4