Skip to content

Commit ec59c33

Browse files
TBarregrenclaude
andcommitted
Prevent page caches from storing content-negotiation redirect
Many full-page caches do not vary their cache key by the Accept request header. When they store the 303 redirect from content negotiation, all subsequent visitors are redirected to the .md URL instead of seeing the HTML page. Two complementary layers prevent this: - Cache-Control: private header prevents shared caches (CDNs, reverse proxies) from storing the redirect while allowing browser caching. - DONOTCACHEPAGE constant and LiteSpeed API call tell WP-level page caches not to store the response. This is behind a filter (markdown_alternate_disable_page_cache_on_redirect) so sites with Vary-aware caching can opt out. The .md URLs themselves remain fully cacheable. Fixes #30 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 1f9979e commit ec59c33

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/Router/RewriteHandler.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,22 @@ public function handle_accept_negotiation(): void {
372372
return;
373373
}
374374

375+
// Prevent shared caches (CDNs, reverse proxies) that don't honour
376+
// Vary from storing this content-negotiation redirect and serving
377+
// the 303 to every visitor regardless of their Accept header.
378+
header('Cache-Control: private');
379+
380+
// Many WordPress page-cache plugins cache at the PHP level and may
381+
// not vary by the Accept request header. Tell them explicitly not to
382+
// cache this response. Sites with Vary-aware caching can disable this:
383+
// add_filter( 'markdown_alternate_disable_page_cache_on_redirect', '__return_false' );
384+
if ( apply_filters( 'markdown_alternate_disable_page_cache_on_redirect', true ) ) {
385+
if ( ! defined( 'DONOTCACHEPAGE' ) ) {
386+
define( 'DONOTCACHEPAGE', true );
387+
}
388+
do_action( 'litespeed_control_set_nocache', 'Content negotiation redirect' );
389+
}
390+
375391
// 303 See Other: redirect to markdown URL with Vary for cache correctness.
376392
header('Vary: Accept');
377393
wp_safe_redirect($md_url, 303);

0 commit comments

Comments
 (0)