Skip to content

Commit 42032cb

Browse files
committed
Keep rewrite cache out of canonical state
1 parent 8c1b797 commit 42032cb

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

packages/runtime-cloudflare/src/worker.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ async function bootWordPressRuntime(
11301130
siteUrl = SITE_URL,
11311131
authConstants: Partial<Record<WordPressAuthConstant, string>> = {},
11321132
runtimeBucket?: R2Bucket,
1133-
shouldPatchCanonicalDisabledCronSchedulingPolicyAtInit = false,
1133+
shouldPatchCanonicalRuntimePoliciesAtInit = false,
11341134
): Promise<{ php: PHP; requestHandler: PHPRequestHandler; wordpressVersion: string }> {
11351135
const requestHandler = await bootWordPressAndRequestHandler({
11361136
createPhpRuntime,
@@ -1162,7 +1162,7 @@ async function bootWordPressRuntime(
11621162
materializeRuntimeFiles(php, MARKDOWN_ROOT, markdownFiles)
11631163
if (markdownIndexSeed) php.writeFile(MARKDOWN_RESOLVED_INDEX_PATH, markdownIndexSeed)
11641164
}
1165-
if (shouldPatchCanonicalDisabledCronSchedulingPolicyAtInit) patchCanonicalDisabledCronSchedulingPolicyAtInit(php)
1165+
if (shouldPatchCanonicalRuntimePoliciesAtInit) patchCanonicalRuntimePoliciesAtInit(php)
11661166
} : undefined,
11671167
beforeDatabaseSetup: databaseSeed ? (php: PHP) => {
11681168
php.mkdir("/wordpress/wp-content/database")
@@ -1190,19 +1190,23 @@ function materializeRuntimeFiles(php: PHP, root: string, files: RuntimeFile[]):
11901190
}
11911191
}
11921192

1193-
function patchCanonicalDisabledCronSchedulingPolicyAtInit(php: PHP): void {
1193+
function patchCanonicalRuntimePoliciesAtInit(php: PHP): void {
11941194
const settingsPath = "/wordpress/wp-settings.php"
11951195
const needle = "do_action( 'init' );"
11961196
const settings = new TextDecoder().decode(php.readFileAsBuffer(settingsPath))
11971197
const firstNeedle = settings.indexOf(needle)
11981198
if (firstNeedle === -1 || firstNeedle !== settings.lastIndexOf(needle)) {
1199-
throw new Error("WordPress canonical disabled-cron scheduling policy patch needle was not uniquely found.")
1199+
throw new Error("WordPress canonical runtime policy patch needle was not uniquely found.")
12001200
}
12011201
const replacement = `if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
12021202
remove_action( 'init', 'wp_cron' );
12031203
remove_action( 'init', 'wp_schedule_delete_old_privacy_export_files' );
12041204
remove_action( 'init', 'wp_schedule_update_checks' );
12051205
}
1206+
// Rewrite rules are derived for each runtime; keep them out of canonical MDI state.
1207+
add_filter( 'pre_update_option_rewrite_rules', static function ( $value, $old_value ) {
1208+
return $old_value;
1209+
}, PHP_INT_MAX, 2 );
12061210
${needle}`
12071211
php.writeFile(settingsPath, new TextEncoder().encode(`${settings.slice(0, firstNeedle)}${replacement}${settings.slice(firstNeedle + needle.length)}`))
12081212
}

tests/cloudflare-runtime.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,24 +367,26 @@ test("Cloudflare canonical lifecycle diagnostics compose the disabled-cron patch
367367
assert.match(lifecycle, /: "\$stop/)
368368
})
369369

370-
test("Cloudflare canonical runtime patches the unique init call with the disabled-cron scheduling policy", async () => {
370+
test("Cloudflare canonical runtime patches the unique init call with runtime persistence policies", async () => {
371371
const worker = await readFile(new URL("../packages/runtime-cloudflare/src/worker.ts", import.meta.url), "utf8")
372-
const patcher = worker.slice(worker.indexOf("function patchCanonicalDisabledCronSchedulingPolicyAtInit"), worker.indexOf("\nfunction collectRuntimeFiles"))
372+
const patcher = worker.slice(worker.indexOf("function patchCanonicalRuntimePoliciesAtInit"), worker.indexOf("\nfunction collectRuntimeFiles"))
373373

374-
assert.ok(patcher.startsWith("function patchCanonicalDisabledCronSchedulingPolicyAtInit"), "The canonical disabled-cron scheduling policy patcher is present.")
374+
assert.ok(patcher.startsWith("function patchCanonicalRuntimePoliciesAtInit"), "The canonical runtime policy patcher is present.")
375375
assert.match(patcher, /const settingsPath = "\/wordpress\/wp-settings\.php"/)
376376
assert.match(patcher, /php\.readFileAsBuffer\(settingsPath\)/)
377377
assert.match(patcher, /firstNeedle === -1 \|\| firstNeedle !== settings\.lastIndexOf\(needle\)/)
378-
assert.match(patcher, /throw new Error\("WordPress canonical disabled-cron scheduling policy patch needle was not uniquely found\."\)/)
378+
assert.match(patcher, /throw new Error\("WordPress canonical runtime policy patch needle was not uniquely found\."\)/)
379379
assert.match(patcher, /defined\( 'DISABLE_WP_CRON' \) && DISABLE_WP_CRON/)
380380
assert.match(patcher, /remove_action\( 'init', 'wp_cron' \)/)
381381
assert.match(patcher, /remove_action\( 'init', 'wp_schedule_delete_old_privacy_export_files' \)/)
382382
assert.match(patcher, /remove_action\( 'init', 'wp_schedule_update_checks' \)/)
383+
assert.match(patcher, /add_filter\( 'pre_update_option_rewrite_rules'/)
384+
assert.match(patcher, /return \$old_value/)
383385
assert.equal((patcher.match(/do_action\( 'init' \);/g) ?? []).length, 1)
384386
assert.match(patcher, /php\.writeFile\(settingsPath/)
385387
assert.doesNotMatch(patcher, /mu-plugins|add_action\(|WP_Site_Health|wp_schedule_site_health_cron|\$GLOBALS\['wp_filter'\]/)
386388
assert.doesNotMatch(worker, /materializeCanonicalCronAdapter|wp-codebox-canonical-cron-policy/)
387-
assert.match(worker, /runtimeBucket\?: R2Bucket,\n shouldPatchCanonicalDisabledCronSchedulingPolicyAtInit = false,/)
389+
assert.match(worker, /runtimeBucket\?: R2Bucket,\n shouldPatchCanonicalRuntimePoliciesAtInit = false,/)
388390
assert.match(worker, /authConstants, bucket, true\), pointer/)
389391
assert.match(worker, /env\.WORDPRESS_STATE_BUCKET, true\)/)
390392
assert.match(worker, /canonical-probe\.invalid", \{\}, bucket, true\)/)

0 commit comments

Comments
 (0)