Skip to content

Commit

Permalink
Merge pull request #1096 from GoogleChromeLabs/fix/inefficient-rewrit…
Browse files Browse the repository at this point in the history
…e-flushing

Fix inefficient rewrite flushing
  • Loading branch information
westonruter authored Jan 24, 2024
2 parents ffda13a + eadf98d commit 0bc41e5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
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

0 comments on commit 0bc41e5

Please sign in to comment.