-
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
Eliminate need for disabling navigation preload while adding navigation request caching strategy #178
Conversation
…gyt; add _doing_it_wrong
a62697e
to
e7441a1
Compare
e7441a1
to
0f9306d
Compare
@iandunn Can you test this to ensure that it eliminates the need for this WordCamp.org code: /*
* todo
*
* All of this needs to be understood deeper in order to know if it's the right way to achieve the goals
* of this project, and what the unintended side-effects may be.
*
* See https://developers.google.com/web/updates/2017/02/navigation-preload
*
* Should navigation preloading really be disabled? Maybe it's good to disable it since we're not using it?
* But then why is it enabled by default? Maybe the `pwa` plugin is using it?
*
* We need to clearly document what's going on here, and _why_.
*
* Are the chosen caching strategies and parameters appropriate in this context?
*
* Are there side-effects beyond the day-of template? If so, how should they be addressed?
*
* All of this needs to be tested to verify that it's working as intended.
* What's the best way to do that? Document it here if it's not obvious.
*/
add_filter( 'wp_service_worker_navigation_preload', '__return_false' ); |
@@ -170,7 +165,7 @@ ERROR_OFFLINE_BODY_FRAGMENT_URL, STREAM_HEADER_FRAGMENT_QUERY_VAR, NAVIGATION_BL | |||
|
|||
return stream.response; | |||
} else { | |||
return navigationCacheStrategy.makeRequest( { request: event.request } ) | |||
return navigationCacheStrategy.handle( { event, request: event.request } ) |
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 ultimately the crux of the fix: passing event
. When this is done, it allows for event.preloadResponse
to be accessed:
I didn't get to this today, but will try to tomorrow. |
Yeah, if I'm understanding it correctly, then that looks like it works to me. When using this branch, I can remove the Although, I can do that on |
Did you make sure the service worker updated when you switched back to |
I just re-checked by having PWA on add_filter(
'wp_service_worker_navigation_caching_strategy',
function() {
return WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST;
}
);
add_filter(
'wp_service_worker_navigation_caching_strategy_args',
function( $args ) {
$args['cacheName'] = 'pages';
$args['plugins']['expiration']['maxEntries'] = 50;
return $args;
}
);
add_filter('wp_service_worker_navigation_preload', '__return_false'); I then opened an Incognito Window. I then navigated to each of the nav menu items while online, and then I checked the Offline checkbox in DevTools and tried going to each of those nav menu items, and I was able to successfully go to each previously-navigated page without seeing the Offline page. Them, while still on PWA add_filter('wp_service_worker_navigation_preload', '__return_false'); I went back Online and I reloaded the page, ensuring the updated service worker was installed. (Easy way is to close Incognito Window then open a new one.) 🚫 Then while Online and after I go to each nav menu item, if I then go Offline and try navigating to a previously-visited page, I then get my theme's offline page served to me, even though the page was cached. ✅ Then if I switch to this |
…ation-preload-enabled * 'master' of github.com:xwp/pwa-wp: Prevent SW installation from post embed iframe Move SW installation from head to footer
@iandunn Any luck with reproducing the issue and fix? |
Haven't had time to circle back to this yet, but it's still on my list. I'll see if I can make time for it today or tomorrow. |
…ation-preload-enabled * 'master' of github.com:xwp/pwa-wp: Set the admin cookie path to be the scope Only set wordpress_sw_installed cookie in admin to disable concatenate_scripts
I believe this code is also unnecessary now: |
Nevermind, that code does seem necessary. Without the code, I see an error message in the console when Offline:
Whereas, when that code is present I see: The latter is expected, but the former is not. So let's leave the code there for now. |
Sorry for the delay, I just tested again, and I'm tracking all the way until At that point, I do get the cached versions of the nav menu items while offline, even though I'm on Here's some screenshots in case I'm missing something. The browser screenshots were after trying to load Possibly related to those
...even though |
…ation-preload-enabled * 'master' of github.com:xwp/pwa-wp: Differentiate between client and server being offline Only use enqueued scripts/style handles in revision; exclude ver due to variability
* master: Guard against Workbox doing importScripts() after SW installation
@iandunn Sorry for the delay. I think I see why your testing yielded different results. I believe the difference is that I was trying to do a browser reload of pages which the service worker had cached. Before the fix in this PR, if navigation preload is enabled, then doing a reload of a previously-cached page will show the offline screen. I've created 3 new test environments to illustrate the difference:
Here is how to test:
Again, the missing piece here is that browser reload will not result in the Offline page when navigation preload is enabled with the fixes in this PR. Please confirm 😄 Each environment has this plugin active: <?php
/**
* Plugin Name: Network-First Navigation Caching Strategy
* Description: With runtime caching of images, scripts, and styles.
*/
add_filter(
'wp_service_worker_navigation_caching_strategy',
function () {
return WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST;
}
);
add_filter(
'wp_service_worker_navigation_caching_strategy_args',
function ( $args ) {
$args['cacheName'] = 'pages';
$args['plugins']['expiration']['maxEntries'] = 50;
return $args;
}
);
add_action(
'wp_front_service_worker',
function( WP_Service_Worker_Scripts $scripts ) {
// Cache scripts and styles.
$scripts->caching_routes()->register(
'/.*\.(?:js|css)(\?.*)?$',
array(
'strategy' => WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST,
'cacheName' => 'assets',
'plugins' => array(
'expiration' => array(
'maxEntries' => 60,
),
),
)
);
// Cache images.
$scripts->caching_routes()->register(
'/wp-content/.*\.(?:png|gif|jpg|jpeg|svg|webp)(\?.*)?$',
array(
'strategy' => WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_FIRST,
'cacheName' => 'images',
'plugins' => array(
'expiration' => array(
'maxEntries' => 60,
),
),
)
);
}
); And then only active on https://not-preload-showcase-pwa.pantheonsite.io/ is this plugin: <?php
/**
* Plugin Name: Disable Navigation Preload
*/
add_filter( 'wp_service_worker_navigation_preload', '__return_false' ); |
Again, the point here is that navigation preload should always be enabled when using a navigation caching strategy because it improves network performance when navigating to a site whose service worker is not booted up (the browser doesn't have to wait for the service worker to be available). |
…ation-preload-enabled * 'master' of github.com:xwp/pwa-wp: Fix phpcs errors Fix eslint errors Update npm packages Update composer packages
@iandunn Can you give this another look when you get a chance? |
Will do, sorry for the delay, I'm always juggling a lot of different things :/ |
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.
Hey, sorry for the delay. I was able to test this successfully now.
Part of the problem last time may have been #175 (comment).
Previously, it was necessary to disable it in order to get cached pages instead of the offline page. As of GoogleChromeLabs/pwa-wp#178, that is no longer the case, and it's better to enable it for improved performance. That fix won't be available in production until a new version of the plugin is released and we update to it, but there aren't any sites currently using this, so it's better to get the change committed while everything is still fresh in my mind.
Revisits #80/#67.
At the moment, when trying to enable a navigation request caching strategy (e.g. Network-First or Stale-While-Revalidate) you also have to disable navigation preload in order to successfully get the cached pages to be served instead of the offline page:
Navigation preload was developed to improve performance of navigation requests while the service worker is not booted.
The problem came down to incorrectly using
strategy.makeRequest()
when it should have been usingstrategy.handle()
.Testing
Given a theme that has the following configuration for the service worker to enable a Network-First caching strategy for navigation requests:
Before
Preload is not being used:
🚫 Offline page always displays even though it has been cached using the navigation caching strategy.
After
Preload is being used:
✅ Cached pages are displayed.
Additionally, an admin notice is added to help guide users to correct usage given that the
readme
was incorrect:Even if the user does nothing, the navigation preload will be forced to be
true
, though a_doing_it_wrong()
will be logged. Also, Site Health will inform of the navigation preload status. When it is being erroneously disabled:And when it is left enabled, with test passing:
Lastly, when calling
\WP_Service_Workers::serve_request()
it will turn offdisplay_errors
to prevent debug messages from causing JS syntax errors in the service worker; this is what is done in the WP REST API as well.