From d5bcf66d2bcc828273bfcb6271ce8e31aecfaa62 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 7 Oct 2024 12:42:29 -0700 Subject: [PATCH 1/3] Only add AMP scripts to runtime cache in Standard Mode --- includes/class-amp-service-worker.php | 2 +- tests/php/test-class-amp-service-worker.php | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/includes/class-amp-service-worker.php b/includes/class-amp-service-worker.php index 624e097c1bb..a175b3a7b37 100644 --- a/includes/class-amp-service-worker.php +++ b/includes/class-amp-service-worker.php @@ -54,7 +54,7 @@ public static function init() { ); } - if ( $enabled_options['cdn_script_caching'] ) { + if ( $enabled_options['cdn_script_caching'] && amp_is_canonical() ) { add_action( 'wp_front_service_worker', [ __CLASS__, 'add_cdn_script_caching' ] ); } if ( $enabled_options['image_caching'] ) { diff --git a/tests/php/test-class-amp-service-worker.php b/tests/php/test-class-amp-service-worker.php index a70c57818b7..d4fe17ee885 100644 --- a/tests/php/test-class-amp-service-worker.php +++ b/tests/php/test-class-amp-service-worker.php @@ -27,23 +27,40 @@ public function set_up() { } } + public function data_to_test_default_init_hooks() { + return [ + 'standard' => [ + 'template_mode' => 'standard', + ], + 'legacy' => [ + 'template_mode' => 'reader', + ], + ]; + } + /** * Test default hooks in init. * * @covers \AMP_Service_Worker::init() + * @dataProvider data_to_test_default_init_hooks */ - public function test_default_init_hooks() { + public function test_default_init_hooks( $template_mode ) { remove_all_filters( 'query_vars' ); remove_all_actions( 'parse_request' ); remove_all_actions( 'wp' ); remove_all_actions( 'wp_front_service_worker' ); + AMP_Options_Manager::update_option( Option::THEME_SUPPORT, $template_mode ); AMP_Service_Worker::init(); $this->assertSame( 10, has_filter( 'query_vars', [ 'AMP_Service_Worker', 'add_query_var' ] ) ); $this->assertSame( 10, has_action( 'parse_request', [ 'AMP_Service_Worker', 'handle_service_worker_iframe_install' ] ) ); $this->assertSame( 10, has_action( 'wp', [ 'AMP_Service_Worker', 'add_install_hooks' ] ) ); - $this->assertSame( 10, has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_cdn_script_caching' ] ) ); + if ( amp_is_canonical() ) { + $this->assertEquals( 10, has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_cdn_script_caching' ] ) ); + } else { + $this->assertFalse( has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_cdn_script_caching' ] ) ); + } $this->assertFalse( has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_image_caching' ] ) ); $this->assertFalse( has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_google_fonts_caching' ] ) ); } From f06c830293d7ffeb5d232d329e5d916fc05a697c Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 11 Nov 2024 15:09:57 -0800 Subject: [PATCH 2/3] Eliminate amp-service-worker-runtime-precaching altogether --- .eslintignore | 1 - .gitignore | 1 - Gruntfile.js | 1 - .../amp-service-worker-runtime-precaching.js | 11 ---------- includes/class-amp-service-worker.php | 20 ------------------- tests/php/test-class-amp-service-worker.php | 10 ---------- 6 files changed, 44 deletions(-) delete mode 100644 assets/js/amp-service-worker-runtime-precaching.js diff --git a/.eslintignore b/.eslintignore index 49a3b042518..eadb6ce7c2a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -2,5 +2,4 @@ **/node_modules/** **/vendor/** **/assets/js/*.js -!assets/js/amp-service-worker-runtime-precaching.js build/* diff --git a/.gitignore b/.gitignore index 02c5500f509..6ffad39573d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,6 @@ node_modules !/assets/css/src/ /assets/js/**/*.js /assets/js/*.asset.php -!/assets/js/amp-service-worker-runtime-precaching.js /assets/js/*.map /built /amphtml diff --git a/Gruntfile.js b/Gruntfile.js index 17cb8192074..fb1b9a40711 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -53,7 +53,6 @@ module.exports = function (grunt) { src: [ 'assets/js/**/*.js', 'assets/js/**/*.js.map', - '!assets/js/amp-service-worker-runtime-precaching.js', 'assets/js/**/*.asset.php', 'assets/css/*.css', 'assets/css/*.css.map', diff --git a/assets/js/amp-service-worker-runtime-precaching.js b/assets/js/amp-service-worker-runtime-precaching.js deleted file mode 100644 index 0ad59f81e6d..00000000000 --- a/assets/js/amp-service-worker-runtime-precaching.js +++ /dev/null @@ -1,11 +0,0 @@ -/* global URLS */ -// See AMP_Service_Workers::add_amp_runtime_caching() and . -{ - self.addEventListener('install', (event) => { - event.waitUntil( - caches - .open(wp.serviceWorker.core.cacheNames.runtime) - .then((cache) => cache.addAll(URLS)) - ); - }); -} diff --git a/includes/class-amp-service-worker.php b/includes/class-amp-service-worker.php index a175b3a7b37..1708efda5a2 100644 --- a/includes/class-amp-service-worker.php +++ b/includes/class-amp-service-worker.php @@ -111,26 +111,6 @@ public static function add_cdn_script_caching( $service_workers ) { return; } - // Add AMP scripts to runtime cache which will then get stale-while-revalidate strategy. - $service_workers->register( - 'amp-cdn-runtime-caching', - static function() { - $urls = AMP_Service_Worker::get_precached_script_cdn_urls(); - if ( empty( $urls ) ) { - return ''; - } - - $js = file_get_contents( AMP__DIR__ . '/assets/js/amp-service-worker-runtime-precaching.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents, WordPress.WP.AlternativeFunctions.file_system_read_file_get_contents - $js = preg_replace( '#/\*\s*global.+?\*/#', '', $js ); - $js = str_replace( - 'URLS', - wp_json_encode( $urls ), - $js - ); - return $js; - } - ); - // Serve the AMP Runtime from cache and check for an updated version in the background. See . self::register_caching_route( $service_workers, diff --git a/tests/php/test-class-amp-service-worker.php b/tests/php/test-class-amp-service-worker.php index d4fe17ee885..4b7ce580b35 100644 --- a/tests/php/test-class-amp-service-worker.php +++ b/tests/php/test-class-amp-service-worker.php @@ -105,16 +105,6 @@ public function test_add_query_var() { $this->assertIsString( $query_vars[1] ); } - /** - * Test add_cdn_script_caching(). - * - * @covers \AMP_Service_Worker::add_cdn_script_caching() - */ - public function test_add_cdn_script_caching() { - AMP_Service_Worker::add_cdn_script_caching( wp_service_workers()->get_registry() ); - $this->assertArrayHasKey( 'amp-cdn-runtime-caching', wp_service_workers()->get_registry()->registered ); - } - /** * Test add_image_caching(). * From 44bcf220847f679d77821b0e32af20f8ab68d427 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 11 Nov 2024 15:33:32 -0800 Subject: [PATCH 3/3] Eliminate standard mode requirement for script caching now that precaching is removed --- includes/class-amp-service-worker.php | 2 +- tests/php/test-class-amp-service-worker.php | 21 ++------------------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/includes/class-amp-service-worker.php b/includes/class-amp-service-worker.php index 1708efda5a2..f8eeb9e2886 100644 --- a/includes/class-amp-service-worker.php +++ b/includes/class-amp-service-worker.php @@ -54,7 +54,7 @@ public static function init() { ); } - if ( $enabled_options['cdn_script_caching'] && amp_is_canonical() ) { + if ( $enabled_options['cdn_script_caching'] ) { add_action( 'wp_front_service_worker', [ __CLASS__, 'add_cdn_script_caching' ] ); } if ( $enabled_options['image_caching'] ) { diff --git a/tests/php/test-class-amp-service-worker.php b/tests/php/test-class-amp-service-worker.php index 4b7ce580b35..94e8bb1aa71 100644 --- a/tests/php/test-class-amp-service-worker.php +++ b/tests/php/test-class-amp-service-worker.php @@ -27,40 +27,23 @@ public function set_up() { } } - public function data_to_test_default_init_hooks() { - return [ - 'standard' => [ - 'template_mode' => 'standard', - ], - 'legacy' => [ - 'template_mode' => 'reader', - ], - ]; - } - /** * Test default hooks in init. * * @covers \AMP_Service_Worker::init() - * @dataProvider data_to_test_default_init_hooks */ - public function test_default_init_hooks( $template_mode ) { + public function test_default_init_hooks() { remove_all_filters( 'query_vars' ); remove_all_actions( 'parse_request' ); remove_all_actions( 'wp' ); remove_all_actions( 'wp_front_service_worker' ); - AMP_Options_Manager::update_option( Option::THEME_SUPPORT, $template_mode ); AMP_Service_Worker::init(); $this->assertSame( 10, has_filter( 'query_vars', [ 'AMP_Service_Worker', 'add_query_var' ] ) ); $this->assertSame( 10, has_action( 'parse_request', [ 'AMP_Service_Worker', 'handle_service_worker_iframe_install' ] ) ); $this->assertSame( 10, has_action( 'wp', [ 'AMP_Service_Worker', 'add_install_hooks' ] ) ); - if ( amp_is_canonical() ) { - $this->assertEquals( 10, has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_cdn_script_caching' ] ) ); - } else { - $this->assertFalse( has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_cdn_script_caching' ] ) ); - } + $this->assertSame( 10, has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_cdn_script_caching' ] ) ); $this->assertFalse( has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_image_caching' ] ) ); $this->assertFalse( has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_google_fonts_caching' ] ) ); }