-
Notifications
You must be signed in to change notification settings - Fork 384
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
Install service worker to cache AMP CDN assets, cache Google Fonts, and cache images (following AMP by Example) #1261
Conversation
c139f50
to
19f3106
Compare
19f3106
to
43ba855
Compare
I'm removing this from 1.0. It depends on the PWA plugin and/or core support, so this is something that should land later when WP support for service workers are more mature. |
Refer also to this service worker from @sebastianbenz that implements all of the best practices: https://gist.github.com/sebastianbenz/1d449dee039202d8b7464f1131eae449 His service worker relies on Workbox so we should use this as a use case for GoogleChromeLabs/pwa-wp#5. Note that his service worker also includes support for offline page which is being worked on in GoogleChromeLabs/pwa-wp#23 |
Important note from @sebastianbenz in regards to whether a service worker should be used if a site is not a single-page app:
In this case, if the service worker is merely being used to cache AMP CDN assets then the service worker should only be used during development (when |
This is fantastic, @westonruter! Those Lighthouse scores are quite impressive. Looking forward to seeing this merge in as PWA advances forward in development. |
…ce-worker * 'develop' of github.com:ampproject/amp-wp: (52 commits) Add tests for amp-twitter in STAMP layer and changes to amp-brid-player Update allowed tags/attributes from spec in amphtml 1903262220080 Update dependency eslint to v5.16.0 Remove stray semicolon in AMP_Analytics_Options_Submenu Update install-wp-tests.sh Fix tests when Gutenberg is active in WP>=5.0 Update dependency xwp/wp-dev-lib to v1.1.0 Use render_block filter instead of overridding the render_callback Make archives and categories block overrides DRY Ensure initial clean state for test_register_and_unregister_embed Simplify overriding of render_callback Install Gutenberg even in WordPress 5.x Add tests for actual categories and archives output Add support for Archives widget when displaying as dropdown Add test for amp_normalized_dimension_extractor_image_url filter Prevent tree-shaking class names for amp-geo when component on page Update dependency grunt to v1.0.4 Make IDs unique in cloned elements for sticky nav menu Update dependency @wordpress/i18n to v3.3.0 Add tests for amp-base-carousel, amp-smartlinks, and new scale/translate amp-story attrs ...
A Googler has manually verified that the CLAs look good. (Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.) ℹ️ Googlers: Go here for more info. |
We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google. ℹ️ Googlers: Go here for more info. |
CLA bot is complaining about |
* | ||
* @param WP_Service_Worker_Scripts $service_workers Service workers. | ||
*/ | ||
public static function add_google_fonts_caching( $service_workers ) { |
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 needs to have the check for the correct class as well, like the above (without _doing_it_wrong()
).
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.
Not like this?
diff --git a/includes/class-amp-service-worker.php b/includes/class-amp-service-worker.php
index 37dbc0e1..4de47a37 100644
--- a/includes/class-amp-service-worker.php
+++ b/includes/class-amp-service-worker.php
@@ -157,6 +157,10 @@ class AMP_Service_Worker {
* @param WP_Service_Worker_Scripts $service_workers Service workers.
*/
public static function add_google_fonts_caching( $service_workers ) {
+ if ( ! ( $service_workers instanceof WP_Service_Worker_Scripts ) ) {
+ _doing_it_wrong( __METHOD__, esc_html__( 'Expected argument to be WP_Service_Worker_Scripts.', 'amp' ), '1.1' );
+ return;
+ }
// The PWA plugin also automatically adds runtime caching for Google Fonts when WP_SERVICE_WORKER_INTEGRATIONS_ENABLED is set.
if ( class_exists( 'WP_Service_Worker_Fonts_Integration' ) ) {
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 the other comments above.
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.
Resolution looks good to me now!
…ce-worker * 'develop' of github.com:ampproject/amp-wp: Account for element attributes when determining if a parent is empty and can be removed Fix tests after always including Gutenberg
A Googler has manually verified that the CLAs look good. (Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.) ℹ️ Googlers: Go here for more info. |
This integration can be seen live on https://amp-wp.org/
Changes:
script
for installing the service worker as output by PWA-WP and instead outputs anamp-install-service-worker
tag instead.cdn_script_caching
: The runtime precaching of AMP runtime and key component scripts; using stale-while-revalidate for those and other CDN assets that are loaded at runtime.google_fonts_caching
: Caching of Google Fonts, unless the PWA plugin's integration isn't also running. Disabled by default.image_caching
: Cache-first strategy for images. Disabled by default.To enable all integrations, add to your theme:
To disable all integrations, you can also do:
With this PR and the PWA-WP feature plugin, running Twenty Sixteen in my development environment yields these Lighthouse results:
Todo
“consider using networkFirst for documents. This might be controversial, but it would be amazing. Trying that for AMP only might be a good first step to rolling it out to the rest of WP.” (@cramforce tweet)(We can address this with integration of amp-sw.)Fixes #1210
Depends on:
Build for testing: amp.zip (v1.1-alpha-20190311T031056Z-a1cb0cdc)