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

Check warning on line 84 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L83-L84

Added lines #L83 - L84 were not covered by tests

$hook_suffix = $this->dashboard->get_hook_suffix( 'stories-dashboard' );
if ( false !== $hook_suffix && $hook_suffix === $hook ) {
$rules = $this->get_rules( 'dashboard' );

Check warning on line 88 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L86-L88

Added lines #L86 - L88 were not covered by tests
}

if ( $this->story_post_type->get_slug() === $this->context->get_screen_post_type() && 'edit' === $this->context->get_screen_base() ) {
$rules = $this->get_rules( 'all_stories' );

Check warning on line 92 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L91-L92

Added lines #L91 - L92 were not covered by tests
}
$this->print_rules( $rules );

Check warning on line 94 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L94

Added line #L94 was not covered by tests
}

/**
* 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 = [

Check warning on line 104 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L103-L104

Added lines #L103 - L104 were not covered by tests
[
'source' => 'document',
'where' => [
'and' => [

Check warning on line 108 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L106-L108

Added lines #L106 - L108 were not covered by tests
[
'href_matches' => $this->get_matches_for_page( $page ),
],
],
],
'eagerness' => 'moderate',
],
];

Check warning on line 116 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L110-L116

Added lines #L110 - L116 were not covered by tests

return [ 'prerender' => $rules ];

Check warning on line 118 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L118

Added line #L118 was not covered by tests
}

/**
* 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 128 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L126-L128

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

$encoded_rules = wp_json_encode( $rules );

Check warning on line 131 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L131

Added line #L131 was not covered by tests

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

Check warning on line 134 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L133-L134

Added lines #L133 - L134 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 = 'post-new.php?post_type=' . $this->story_post_type->get_slug();
$edit_story_url = 'post.php?post=*&action=edit';
$view_story_url = '/web-stories*';

Check warning on line 146 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L143-L146

Added lines #L143 - L146 were not covered by tests

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

Check warning on line 152 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L148-L152

Added lines #L148 - L152 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 163 in includes/Speculative_Prerendering.php

View check run for this annotation

Codecov / codecov/patch

includes/Speculative_Prerendering.php#L161-L163

Added lines #L161 - L163 were not covered by tests
}
}
Loading