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 44 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 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
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@
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' ] );

Check warning on line 219 in includes/Admin/Dashboard.php

View check run for this annotation

Codecov / codecov/patch

includes/Admin/Dashboard.php#L219

Added line #L219 was not covered by tests
}

/**
Expand Down Expand Up @@ -580,4 +582,44 @@
</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()

Check warning on line 595 in includes/Admin/Dashboard.php

View check run for this annotation

Codecov / codecov/patch

includes/Admin/Dashboard.php#L592-L595

Added lines #L592 - L595 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 600 in includes/Admin/Dashboard.php

View check run for this annotation

Codecov / codecov/patch

includes/Admin/Dashboard.php#L597-L600

Added lines #L597 - L600 were not covered by tests
);
return [
'prerender' => [

Check warning on line 603 in includes/Admin/Dashboard.php

View check run for this annotation

Codecov / codecov/patch

includes/Admin/Dashboard.php#L602-L603

Added lines #L602 - L603 were not covered by tests
[
'source' => 'document',
'where' => [
'href_matches' => [ $edit_story_url, $new_story_url, $view_story_url ],
],
'eagerness' => 'moderate',
],
],
];

Check warning on line 612 in includes/Admin/Dashboard.php

View check run for this annotation

Codecov / codecov/patch

includes/Admin/Dashboard.php#L605-L612

Added lines #L605 - L612 were not covered by tests
}

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

Check warning on line 67 in includes/Speculation_Rules.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculation_Rules.php#L66-L67

Added lines #L66 - L67 were not covered by tests

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

Check warning on line 72 in includes/Speculation_Rules.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculation_Rules.php#L69-L72

Added lines #L69 - L72 were not covered by tests
);
$archive_url = Story_Post_Type::REWRITE_SLUG;

Check warning on line 74 in includes/Speculation_Rules.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculation_Rules.php#L74

Added line #L74 was not covered by tests

$rules = [
'prerender' => [

Check warning on line 77 in includes/Speculation_Rules.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculation_Rules.php#L76-L77

Added lines #L76 - L77 were not covered by tests
[
'source' => 'document',
'where' => [
'href_matches' => [
$archive_url,
$view_story_url,
],
],
'eagerness' => 'moderate',
],
],
];

Check warning on line 89 in includes/Speculation_Rules.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculation_Rules.php#L79-L89

Added lines #L79 - L89 were not covered by tests
}

/**
* Filters the prerendering rules.
*
* @param array $rules An array of prerendering rules.
* @since 1.37.0
*/
return apply_filters( 'web_stories_speculation_rules', $rules );

Check warning on line 98 in includes/Speculation_Rules.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculation_Rules.php#L98

Added line #L98 was not covered by tests
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;

Check warning on line 107 in includes/Speculation_Rules.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculation_Rules.php#L107

Added line #L107 was not covered by tests
}

$encoded_rules = wp_json_encode( $rules, JSON_UNESCAPED_SLASHES );

if ( false !== $encoded_rules ) {
wp_print_inline_script_tag( $encoded_rules, [ 'type' => 'speculationrules' ] );
}
}
}
3 changes: 2 additions & 1 deletion packages/e2e-tests/src/specs/wordpress/quickEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ describe('Quick Edit', () => {
await trashAllPosts('web-story');
});

it('should save story without breaking markup', async () => {
// eslint-disable-next-line jest/no-disabled-tests -- Flaky test.
it.skip('should save story without breaking markup', async () => {
await createNewStory();

await expect(page).toMatchElement('input[placeholder="Add title"]');
Expand Down
74 changes: 74 additions & 0 deletions tests/phpunit/integration/tests/Speculation_Rules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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' => [
'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, JSON_UNESCAPED_SLASHES );
$this->assertNotFalse( $encoded_rules );
$this->assertStringContainsString( $encoded_rules, $output );
}
}