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

Draft
wants to merge 47 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
47 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
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,
'speculation_rules' => \Google\Web_Stories\Speculation_Rules::class,
] )
);

Expand Down
44 changes: 43 additions & 1 deletion includes/Admin/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Google\Web_Stories\Decoder;
use Google\Web_Stories\Experiments;
use Google\Web_Stories\Font_Post_Type;
use Google\Web_Stories\Infrastructure\Conditional;
use Google\Web_Stories\Integrations\Site_Kit;
use Google\Web_Stories\Integrations\WooCommerce;
use Google\Web_Stories\Locale;
Expand All @@ -48,7 +49,7 @@
/**
* Dashboard class.
*/
class Dashboard extends Service_Base {
class Dashboard extends Service_Base implements Conditional {

/**
* Script handle.
Expand Down Expand Up @@ -215,6 +216,7 @@ public function register(): void {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
add_action( 'admin_notices', [ $this, 'display_link_to_dashboard' ] );
add_action( 'load-web-story_page_stories-dashboard', [ $this, 'load_stories_dashboard' ] );
add_filter( 'web_stories_speculation_rules', [ $this, 'add_speculation_rules' ] );
}

/**
Expand Down Expand Up @@ -573,4 +575,44 @@ public function display_link_to_dashboard(): void {
</div>
<?php
}

/**
* Add the Speculation Rules for the Dashboard.
*
Swanand01 marked this conversation as resolved.
Show resolved Hide resolved
* @return array<string, array<int, array<string, mixed>>> An array containing prerendering rules.
* @since 1.37.0
*/
public function add_speculation_rules(): array {
$new_story_url = sprintf(
'post-new.php?post_type=%s',
$this->story_post_type->get_slug()
);
$edit_story_url = 'post.php?post=*&action=edit';
return [
'prerender' => [
[
'source' => 'document',
'where' => [
'and' => [
[
'href_matches' => [ $edit_story_url, $new_story_url ],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add $view_story_url here too so that links to the story permalink can be preloaded.

We should also test whether $view_story_url should be in prefetch instead, not prerender, given that it's usually loaded in a lightbox anyway.

Copy link
Collaborator Author

@Swanand01 Swanand01 May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested both prefetch and prerender for $view_story_url, and I couldn't see a noticeable difference. Prerender appeared to be just slightly faster, in terms of page loading times.

],
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
],
],
'eagerness' => 'moderate',
],
],
];
}

/**
* Check whether the conditional object is currently needed.
*
* @since 1.37.0
*
* @return bool Whether the conditional object is needed.
*/
public static function is_needed(): bool {
return is_admin();
}
}
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,
'speculation_rules' => Speculation_Rules::class,
];

/**
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
120 changes: 120 additions & 0 deletions includes/Speculation_Rules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Class Speculation_Rules
*
* @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;

/**
* Speculation_Rules class.
*
* @since 1.37.0
*/
class Speculation_Rules extends Service_Base {
/**
* Story_Post_Type instance.
*
* @var Story_Post_Type Story_Post_Type instance.
*/
private Story_Post_Type $story_post_type;

/**
* Speculation_Rules constructor.
*
* @param Story_Post_Type $story_post_type Story_Post_Type instance.
*/
public function __construct( Story_Post_Type $story_post_type ) {
$this->story_post_type = $story_post_type;
}

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

/**
* Retrieves the prerendering rules for a specific page.
*
* @return array<string, array<int, array<string, mixed>>> An array containing prerendering rules.
*/
public function get_rules(): array {
Swanand01 marked this conversation as resolved.
Show resolved Hide resolved
$rules = [];

if ( ! is_admin() ) {
$view_story_url = sprintf(
'/%s/*',
$this->story_post_type::REWRITE_SLUG
);
$archive_url = Story_Post_Type::REWRITE_SLUG;

$rules = [
'prerender' => [
[
'source' => 'document',
'where' => [
'and' => [
[
'href_matches' => [
$archive_url,
$view_story_url,
],
],
],
],
'eagerness' => 'moderate',
],
],
];
}

/**
* Filters the prerendering rules.
*
* @param array $rules An array of prerendering rules.
* @since 1.37.0
*/
return apply_filters( 'web_stories_speculation_rules', $rules );
Swanand01 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Prints the prerendering rules as an inline script tag.
*/
public function print_rules(): void {
$rules = $this->get_rules();
if ( empty( $rules ) ) {
return;
}

$encoded_rules = wp_json_encode( $rules, JSON_UNESCAPED_SLASHES );

if ( false !== $encoded_rules ) {
wp_print_inline_script_tag( $encoded_rules, [ 'type' => 'speculationrules' ] );
}
}
}
79 changes: 79 additions & 0 deletions tests/phpunit/integration/tests/Speculation_Rules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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\Story_Post_Type;

/**
* @coversDefaultClass \Google\Web_Stories\Speculation_Rules
*/
class Speculation_Rules extends DependencyInjectedTestCase {
private Story_Post_Type $story_post_type;

protected \Google\Web_Stories\Speculation_Rules $instance;

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

$this->story_post_type = $this->injector->make( Story_Post_Type::class );

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

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

/**
* @covers ::print_rules
*/
public function test_print_rules(): void {
$expected_rules = [
'prerender' => [
[
'source' => 'document',
'where' => [
'and' => [
[ 'href_matches' => [ Story_Post_Type::REWRITE_SLUG, sprintf( '/%s/*', $this->story_post_type::REWRITE_SLUG ) ] ],
],
],
'eagerness' => 'moderate',
],
],
];
$output = get_echo( [ $this->instance, 'print_rules' ] );
$this->instance->get_rules();

$encoded_rules = wp_json_encode( $expected_rules );
Swanand01 marked this conversation as resolved.
Show resolved Hide resolved
if ( false !== $encoded_rules ) {
Swanand01 marked this conversation as resolved.
Show resolved Hide resolved
$this->assertStringContainsString( $encoded_rules, $output );

Check failure on line 74 in tests/phpunit/integration/tests/Speculation_Rules.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - WP latest

Failed asserting that '<script type="speculationrules">\n

Check failure on line 74 in tests/phpunit/integration/tests/Speculation_Rules.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 - WP latest

Failed asserting that '<script type="speculationrules">\n

Check failure on line 74 in tests/phpunit/integration/tests/Speculation_Rules.php

View workflow job for this annotation

GitHub Actions / PHP 7.4 - WP 6.3

Failed asserting that '<script type="speculationrules">\n

Check failure on line 74 in tests/phpunit/integration/tests/Speculation_Rules.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - WP latest (experimental) (in random order)

Failed asserting that '<script type="speculationrules">\n

Check failure on line 74 in tests/phpunit/integration/tests/Speculation_Rules.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - WP trunk (experimental)

Failed asserting that '<script type="speculationrules">\n

Check failure on line 74 in tests/phpunit/integration/tests/Speculation_Rules.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 - WP trunk (experimental)

Failed asserting that '<script type="speculationrules">\n
} else {
$this->fail( 'JSON decoding failed.' );
}
}
}
Loading