Skip to content

Commit

Permalink
Only add AMP scripts to runtime cache in Standard Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Oct 7, 2024
1 parent 60a655c commit d5bcf66
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion includes/class-amp-service-worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) {
Expand Down
21 changes: 19 additions & 2 deletions tests/php/test-class-amp-service-worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] ) );
}
Expand Down

0 comments on commit d5bcf66

Please sign in to comment.