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

Fix inefficient rewrite flushing #1096

Merged
merged 2 commits into from
Jan 24, 2024
Merged
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"phpcompatibility/phpcompatibility-wp": "2.1.4",
"wp-cli/wp-cli": "^2.9",
"wp-coding-standards/wpcs": "3.0.1",
"wp-phpunit/wp-phpunit": "^6.4",
"yoast/wp-test-utils": "1.2.0"
},
"minimum-stability": "dev",
Expand Down Expand Up @@ -60,4 +61,4 @@
"npm run lint:staged"
]
}
}
}
52 changes: 50 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 5 additions & 15 deletions wp-includes/class-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,11 @@
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
*/
function pwa_add_rewrite_rules() {
global $wp_rewrite;
$rewrite_rule_regex = '^wp\.serviceworker$';
$rules = $wp_rewrite->wp_rewrite_rules();

if ( ! isset( $rules[ $rewrite_rule_regex ] ) ) {
// Note: This logic will not be required as part of core merge since rewrite rules are flushed upon DB upgrade (as long as the DB version is bumped).
add_action(
'admin_init',
function () {
flush_rewrite_rules( false ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.flush_rewrite_rules_flush_rewrite_rules -- Not theme code.
}
);
}
add_rewrite_rule( $rewrite_rule_regex, 'index.php?' . WP_Service_Workers::QUERY_VAR . '=' . WP_Service_Workers::SCOPE_FRONT, 'top' );

add_rewrite_rule(
'^wp\.serviceworker$',
'index.php?' . WP_Service_Workers::QUERY_VAR . '=' . WP_Service_Workers::SCOPE_FRONT,
'top'
);
add_rewrite_tag( '%' . WP_Service_Workers::QUERY_VAR . '%', '([^?]+)' );
}

Expand Down
2 changes: 1 addition & 1 deletion wp-includes/service-workers.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function wp_service_worker_loaded( WP_Query $query ) {
return;
}

// Handle case where rewrite rules have not yet been flushed.
// Do not require rewrite rules to have been flushed.
if ( 'wp.serviceworker' === $wp->request ) {
$query->set( WP_Service_Workers::QUERY_VAR, 1 );
}
Expand Down
Loading