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

Implement speculative prerendering #13616

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
7d3ccaf
Add the `Speculative_Prerendering` service class
Swanand01 Apr 3, 2024
6fbdbe7
Update `SERVICES` to list the `Speculative_Prerendering` service
Swanand01 Apr 3, 2024
b11a77a
Fix: Update 2020 to 2024 and fix phpstan linting issue
Swanand01 Apr 3, 2024
99052d6
Remove the `Admin` namespace
Swanand01 Apr 4, 2024
25db67a
Use Context and Dashboard services to dynamically build URLs
Swanand01 Apr 4, 2024
b652865
Add `Dashboard` class to `Plugin::get_shared_instances()`
Swanand01 Apr 4, 2024
54990f5
Use `story_post_type::REWRITE_SLUG` for view story URLs
Swanand01 Apr 4, 2024
ace9622
Add `Speculative_Prerendering` service class to `.phpstorm.meta.php`
Swanand01 Apr 4, 2024
68a6acf
Use `elseif` instead of `if`
Swanand01 Apr 9, 2024
7c9ba61
Add test for `load_rules`
Swanand01 Apr 9, 2024
d6f2216
Fix: Update priority to 10
Swanand01 Apr 9, 2024
1967e6c
Move instance creation and mock object creation to `set_up`
Swanand01 Apr 9, 2024
d7342b9
Remove types from class variables and make call
Swanand01 Apr 9, 2024
737b6f5
Make `print_rules` and `get_rules` private
Swanand01 Apr 9, 2024
0d13e6a
Check if `print_rules` was called
Swanand01 Apr 9, 2024
afd008c
Revert: Make print_rules and get_rules private
Swanand01 Apr 9, 2024
289baa4
Add tests for `get_rules`
Swanand01 Apr 9, 2024
d77c251
Use `injector` to build `story_post_type`
Swanand01 Apr 9, 2024
da1f746
Merge branch 'main' into feat/13603-speculative-prerendering
Swanand01 Apr 11, 2024
e0c6b9f
Merge branch 'main' into feat/13603-speculative-prerendering
AnuragVasanwala Apr 22, 2024
0b9256b
Merge branch 'main' into feat/13603-speculative-prerendering
AnuragVasanwala Apr 30, 2024
6ee4302
Rename Speculative_Prerendering to Speculation_Rules
Swanand01 May 2, 2024
f4c9750
Update Speculation_Rules.php
Swanand01 May 9, 2024
e85b4dc
Add speculation rules in Dashboard using the new filter
Swanand01 May 9, 2024
8ac8d38
Update test for `get_rules`
Swanand01 May 10, 2024
cbe2537
Add test for `print_rules`
Swanand01 May 10, 2024
7e09c83
Fix indentation
Swanand01 May 15, 2024
354e306
Add the since tag
Swanand01 May 15, 2024
118cc79
Remove test for `get_rules` and modify test for `print_rules`
Swanand01 May 15, 2024
4d65d2e
Rename `load_speculation_rules` to `add_speculation_rules`
Swanand01 May 15, 2024
c5e54c0
Use `JSON_UNESCAPED_SLASHES` in `wp_json_encode`
Swanand01 May 15, 2024
55ca62f
Make the `Dashboard` class implement `Conditional`
Swanand01 May 15, 2024
5b558a6
Add what the array consists of in doc comment
Swanand01 May 15, 2024
b948dc1
Fix: Use `assertNotFalse` in test
Swanand01 May 15, 2024
566a73f
Merge branch 'main' into feat/13603-speculative-prerendering
Swanand01 May 15, 2024
7d1c4ff
Simplify speculation rules, and add `$view_story_url` to Dashboard rule
Swanand01 May 15, 2024
0af6756
SImplify rules in test
Swanand01 May 15, 2024
56f19cf
Skip flaky test
Swanand01 May 16, 2024
18199a4
Add `eslint-disable-next-line` to the skipped test
Swanand01 May 16, 2024
33b5c38
Merge branch 'main' into feat/13603-speculative-prerendering
Swanand01 May 16, 2024
30361eb
Use fixture.events.sleep() in the failing test
Swanand01 May 16, 2024
422dd67
Merge branch 'feat/13603-speculative-prerendering' of https://github.…
Swanand01 May 16, 2024
f6f99e4
Merge branch 'main' into feat/13603-speculative-prerendering
AnuragVasanwala Jun 6, 2024
7922abb
Merge branch 'main' into feat/13603-speculative-prerendering
swissspidy Jun 17, 2024
7bbc544
Merge branch 'main' into feat/13603-speculative-prerendering
Swanand01 Jul 10, 2024
4706bbb
Merge branch 'main' into feat/13603-speculative-prerendering
Swanand01 Jul 19, 2024
6dfe9ce
Merge branch 'main' into feat/13603-speculative-prerendering
Swanand01 Jul 26, 2024
d3a49d0
Merge branch 'main' into feat/13603-speculative-prerendering
swissspidy Nov 11, 2024
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
1 change: 1 addition & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
'remove_transients' => \Google\Web_Stories\Remove_Transients::class,
'web_stories_block' => \Google\Web_Stories\Block\Web_Stories_Block::class,
'injector' => \Google\Web_Stories\Infrastructure\Injector::class,
'speculative_prerendering' => \Google\Web_Stories\Speculative_Prerendering::class,
] )
);

Expand Down
2 changes: 2 additions & 0 deletions includes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class Plugin extends ServiceBasedPlugin {
'user_preferences' => User\Preferences::class,
'remove_transients' => Remove_Transients::class,
'web_stories_block' => Block\Web_Stories_Block::class,
'speculative_prerendering' => Speculative_Prerendering::class,
Swanand01 marked this conversation as resolved.
Show resolved Hide resolved
];

/**
Expand Down Expand Up @@ -192,6 +193,7 @@ protected function get_shared_instances(): array {
Settings::class,
Stories_Script_Data::class,
User\Preferences::class,
Admin\Dashboard::class,
];
}

Expand Down
168 changes: 168 additions & 0 deletions includes/Speculative_Prerendering.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php
/**
* Class Speculative_Prerendering
*
* @link https://github.com/googleforcreators/web-stories-wp
*
* @copyright 2024 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/

/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types = 1);

namespace Google\Web_Stories;

use Google\Web_Stories\Admin\Dashboard;

/**
* Speculative_Prerendering class.
*/
class Speculative_Prerendering extends Service_Base {
Swanand01 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Context instance.
*
* @var Context Context instance.
*/
private Context $context;

/**
* Story_Post_Type instance.
*
* @var Story_Post_Type Story_Post_Type instance.
*/
private Story_Post_Type $story_post_type;

/**
* Dashboard instance.
*
* @var Dashboard Dashboard instance.
*/
private Dashboard $dashboard;

/**
* Speculative_Prerendering constructor.
*
* @param Context $context Context instance.
* @param Story_Post_Type $story_post_type Story_Post_Type instance.
* @param Dashboard $dashboard Dashboard instance.
*/
public function __construct( Context $context, Story_Post_Type $story_post_type, Dashboard $dashboard ) {
$this->context = $context;
$this->story_post_type = $story_post_type;
$this->dashboard = $dashboard;
}

/**
* Runs on instantiation.
*/
public function register(): void {
add_action( 'admin_enqueue_scripts', [ $this, 'load_rules' ] );
}

/**
* Loads the prerendering rules based on the current page.
*
* @param string $hook The current page hook.
*/
public function load_rules( string $hook ): void {
$rules = [];

$hook_suffix = $this->dashboard->get_hook_suffix( 'stories-dashboard' );
if ( false !== $hook_suffix && $hook_suffix === $hook ) {
$rules = $this->get_rules( 'dashboard' );
} elseif ( $this->story_post_type->get_slug() === $this->context->get_screen_post_type() && 'edit' === $this->context->get_screen_base() ) {
$rules = $this->get_rules( 'all_stories' );
}
$this->print_rules( $rules );
}

/**
* Retrieves the prerendering rules for a specific page.
*
* @param string $page The page identifier.
* @return array An array containing prerendering rules.
*/
public function get_rules( string $page ): array {
$rules = [
[
'source' => 'document',
'where' => [
'and' => [
[
'href_matches' => $this->get_matches_for_page( $page ),
],
],
],
'eagerness' => 'moderate',
],
];

return [ 'prerender' => $rules ];
}

/**
* Prints the prerendering rules as an inline script tag.
*
* @param array $rules The prerendering rules to print.
*/
public function print_rules( array $rules ): void {
if ( empty( $rules ) ) {
return;

Check warning on line 126 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L124-L126

Added lines #L124 - L126 were not covered by tests
}

$encoded_rules = wp_json_encode( $rules );

Check warning on line 129 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L129

Added line #L129 was not covered by tests

if ( false !== $encoded_rules ) {
wp_print_inline_script_tag( $encoded_rules, [ 'type' => 'speculationrules' ] );

Check warning on line 132 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L131-L132

Added lines #L131 - L132 were not covered by tests
}
}

/**
* Generates the URL matches array for different pages.
*
* @return array The URL matches array containing URLs for different pages.
*/
private function generate_matches(): array {
$new_story_url = sprintf(
'post-new.php?post_type=%s',
$this->story_post_type->get_slug()

Check warning on line 144 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L141-L144

Added lines #L141 - L144 were not covered by tests
);
$edit_story_url = 'post.php?post=*&action=edit';
$view_story_url = sprintf(
'/%s/*',
$this->story_post_type::REWRITE_SLUG

Check warning on line 149 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L146-L149

Added lines #L146 - L149 were not covered by tests
);

return [
'dashboard' => [ $edit_story_url, $new_story_url ],
'all_stories' => [ $edit_story_url, $new_story_url, $view_story_url ],
];

Check warning on line 155 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L152-L155

Added lines #L152 - L155 were not covered by tests
}

/**
* Retrieves the URL matches for the specified page.
*
* @param string $page The page for which to retrieve URL matches.
* @return array The URL matches for the specified page.
*/
private function get_matches_for_page( string $page ): array {
$matches = $this->generate_matches();
return $matches[ $page ];

Check warning on line 166 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L164-L166

Added lines #L164 - L166 were not covered by tests
}
}
176 changes: 176 additions & 0 deletions tests/phpunit/integration/tests/Speculative_Prerendering.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

declare(strict_types = 1);

/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Web_Stories\Tests\Integration;

use Google\Web_Stories\Admin\Dashboard;
use Google\Web_Stories\Context;
use Google\Web_Stories\Story_Post_Type;
use PHPUnit\Framework\MockObject\MockObject;

/**
* @coversDefaultClass \Google\Web_Stories\Speculative_Prerendering
*/
class Speculative_Prerendering extends DependencyInjectedTestCase {
/**
* @var Context & MockObject
*/
private $context;

/**
* @var Story_Post_Type & MockObject
*/
private $story_post_type;

/**
* @var Dashboard & MockObject
*/
private $dashboard;

protected \Google\Web_Stories\Speculative_Prerendering $instance;

public function set_up(): void {
parent::set_up();

$this->dashboard = $this->createMock( Dashboard::class );
$this->story_post_type = $this->createMock( Story_Post_Type::class );
Swanand01 marked this conversation as resolved.
Show resolved Hide resolved
$this->context = $this->createMock( Context::class );

$this->instance = new \Google\Web_Stories\Speculative_Prerendering(
$this->context,
$this->story_post_type,
$this->dashboard,
);
}

/**
* @covers ::register
*/
public function test_register(): void {
$this->instance->register();
$this->assertSame( 10, has_action( 'admin_enqueue_scripts', [ $this->instance, 'load_rules' ] ) );
}

/**
* @covers ::load_rules
*/
public function test_load_rules_dashboard(): void {
$this->dashboard->method( 'get_hook_suffix' )->willReturn( 'web-story_page_stories-dashboard' );

$prerendering_class = $this->getMockBuilder( \Google\Web_Stories\Speculative_Prerendering::class )
->onlyMethods( [ 'get_rules', 'print_rules' ] )
->setConstructorArgs( [ $this->context, $this->story_post_type, $this->dashboard ] )
->getMock();

$prerendering_class->expects( $this->once() )
->method( 'get_rules' )
->with( 'dashboard' );

$prerendering_class->expects( $this->once() )
->method( 'print_rules' );

$prerendering_class->load_rules( 'web-story_page_stories-dashboard' );
}

/**
* @covers ::load_rules
*/
public function test_load_rules_all_stories(): void {
$this->story_post_type->method( 'get_slug' )->willReturn( 'web-story' );
$this->context->method( 'get_screen_post_type' )->willReturn( 'web-story' );
$this->context->method( 'get_screen_base' )->willReturn( 'edit' );

$prerendering_class = $this->getMockBuilder( \Google\Web_Stories\Speculative_Prerendering::class )
->onlyMethods( [ 'get_rules', 'print_rules' ] )
->setConstructorArgs( [ $this->context, $this->story_post_type, $this->dashboard ] )
->getMock();

$prerendering_class->expects( $this->once() )
->method( 'get_rules' )
->with( 'all_stories' );

$prerendering_class->expects( $this->once() )
->method( 'print_rules' );

$prerendering_class->load_rules( 'edit' );
}

/**
* @covers ::get_rules
*/
public function test_get_rules_for_dashboard(): void {
$new_story_url = sprintf(
'post-new.php?post_type=%s',
$this->story_post_type->get_slug()
);
$edit_story_url = 'post.php?post=*&action=edit';

$expected = [
'prerender' => [
[
'source' => 'document',
'where' => [
'and' => [
[
'href_matches' => [ $edit_story_url, $new_story_url ],
],
],
],
'eagerness' => 'moderate',
],
],
];

$this->assertEquals( $expected, $this->instance->get_rules( 'dashboard' ) );
}

/**
* @covers ::get_rules
*/
public function test_get_rules_for_all_stories(): void {
$new_story_url = sprintf(
'post-new.php?post_type=%s',
$this->story_post_type->get_slug()
);
$edit_story_url = 'post.php?post=*&action=edit';
$view_story_url = sprintf(
'/%s/*',
$this->story_post_type::REWRITE_SLUG
);

$expected = [
'prerender' => [
[
'source' => 'document',
'where' => [
'and' => [
[
'href_matches' => [ $edit_story_url, $new_story_url, $view_story_url ],
],
],
],
'eagerness' => 'moderate',
],
],
];

$this->assertEquals( $expected, $this->instance->get_rules( 'all_stories' ) );
}
}
Loading