Skip to content
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

Only add AMP scripts to runtime cache in Standard Mode #7895

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading