Changeset 3358113
- Timestamp:
- 09/08/2025 06:23:01 PM (7 months ago)
- Location:
- simple-page-cache/trunk
- Files:
-
- 2 edited
-
readme.txt (modified) (2 diffs)
-
simple-page-cache.php (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
simple-page-cache/trunk/readme.txt
r3356518 r3358113 4 4 Requires at least: 5.0 5 5 Tested up to: 6.8 6 Stable tag: 1.1. 06 Stable tag: 1.1.1 7 7 License: GPLv2 or later 8 8 … … 109 109 110 110 == Changelog == 111 = 1.1.1 = 112 * **Bug fix**: Fixed 404 errors when browsing the website while logged in 113 * **Query parameter handling**: Plugin now properly checks for $_GET parameters and skips caching for requests with query strings 114 111 115 = 1.1.0 = 112 116 * **Path-based cache structure**: Cache files now use `/host/path/index.html` structure for nginx direct serving -
simple-page-cache/trunk/simple-page-cache.php
r3356518 r3358113 3 3 Plugin Name: Simple Page Cache 4 4 Description: Extremely simple page cache to disk for non-logged-in users. No expiry, only clears on manual action. 5 Version: 1.1. 05 Version: 1.1.1 6 6 Requires at least: 5.0 7 7 Tested up to: 6.8 … … 40 40 } 41 41 42 // Only cache for non-logged-in users and only GET requests 42 // Only cache for non-logged-in users and only GET requests without query parameters 43 43 function simplepagecache_should_cache() 44 44 { 45 return !is_user_logged_in() && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET'; 45 // Don't cache if user is logged in 46 if (is_user_logged_in()) { 47 return false; 48 } 49 50 // Don't cache if not a GET request 51 if (!isset($_SERVER['REQUEST_METHOD']) || $_SERVER['REQUEST_METHOD'] !== 'GET') { 52 return false; 53 } 54 55 // Don't cache if there are query parameters 56 if (!empty($_GET)) { 57 return false; 58 } 59 60 return true; 46 61 } 47 62 … … 285 300 # MUST end with a trailing slash. This is the *real* filesystem path where 286 301 # your plugin writes cache files using the <host>/<uri>/index.html layout. 287 alias " . $cache_abs_path. "/;302 alias " . rtrim($cache_abs_path, "/") . "/; 288 303 289 304 # Optional debug headers (remove in production if you don't want to expose paths) … … 291 306 #add_header simple-page-cache-file \$request_filename always; 292 307 293 # We expect the internal rewrite to point to an existing file under alias. 294 # If it doesn't exist for some reason, fail closed with 404. 308 # Only serve existing files under alias. Anything else -> 404. 295 309 try_files \$uri =404; 296 310 } … … 300 314 # - \"/\" (home), \"/about\", \"/blog/\", \"/foo/bar\" (extensionless URLs) 301 315 # - Does NOT match assets like \".css\", \".js\", \".png\", \".xml\", etc. (they contain a dot) 316 # - Does NOT match urls containingwp-admin 302 317 # 303 318 # Behavior: … … 305 320 # 2) Bypass cache if query string is present or user is logged in. 306 321 # 3) If the file exists -> internal rewrite to /__simple-page-cache__/... to serve it. 307 # 4) Otherwise: do nothing here; request will continue and be handled by your 308 # existing locations (e.g., your current `location /` → WordPress fallback). 309 location ~ ^/[^.]*$ { 322 # 4) On MISS/BYPASS -> hand off directly to WordPress front controller. 323 location ~* ^/(?!.*wp-admin)[^.]*$ { 310 324 # Absolute cache root and default file name 311 set \$simple_page_cache_root " . $cache_abs_path . "/; 312 set \$simple_page_cache_index index.html; 313 314 # Build: 315 # - \$simple_page_cache_abs : absolute filesystem path to the cache candidate 316 # - \$simple_page_cache_rel : internal URI that maps to the same file via /__simple-page-cache__/ + alias 317 # 318 # Layout on disk (produced by your drop-in): 319 # <cache_root>/<host><uri>/index.html 320 set \$simple_page_cache_abs \$simple_page_cache_root\$host\$uri/\$simple_page_cache_index; 321 set \$simple_page_cache_rel /__simple-page-cache__/\$host\$uri/\$simple_page_cache_index; 325 326 # Absolute on-disk candidate and the internal URI to serve it 327 set \$simple_page_cache_abs " . rtrim($cache_abs_path, "/") . "/\$host\$uri/index.html; 328 set \$simple_page_cache_rel /__simple-page-cache__/\$host\$uri/index.html; 322 329 323 330 # Bypass cache if: 324 331 # - the request has a query string (we don't cache query variants), OR 325 332 # - the user is logged in (WordPress logged-in cookie). 326 # We do this by clearing \$simple_page_cache_abs so the -f check fails and we won't rewrite.327 if (\$args != \"\") { set \$simple_page_cache_abs\"\"; }328 if (\$http_cookie ~* \"wordpress_logged_in_\") { set \$simple_page_cache_abs \"\"; }333 # Clear BOTH vars so we never jump to the internal serve location on MISS. 334 if (\$args != \"\") { set \$simple_page_cache_abs \"\"; set \$simple_page_cache_rel \"\"; } 335 if (\$http_cookie ~* \"wordpress_logged_in_\") { set \$simple_page_cache_abs \"\"; set \$simple_page_cache_rel \"\"; } 329 336 330 337 # Cache HIT: … … 335 342 } 336 343 337 # Cache MISS or bypass:338 339 try_files \$uri \$uri/ /index.php?\$args;344 # MISS/BYPASS -> explicitly hand off to WordPress front controller 345 # No FastCGI params here; your existing PHP handler will catch /index.php 346 rewrite ^ /index.php\$is_args\$args last; 340 347 } 341 348 … … 343 350 # 1. Add the above configuration to your nginx server block for " . $host . " 344 351 # 2. Reload nginx: sudo nginx -t && sudo systemctl reload nginx 345 # 3. Test by visiting your site -cached pages should be served directly by nginx346 # 4. Check nginx access logs to see cache hits(look for 'simple-page-cache-webserver: Hit' header)347 # 5. Remove debug headers in production by commenting out the add_header lines above352 # 3. Test by visiting your site — cached pages should be served directly by nginx 353 # 4. Check in your browser (private/incognito mode) to see if the cache is working (look for 'simple-page-cache-webserver: Hit' header) 354 # 5. Uncomment additional add_header lines to debug further or enjoy your faster site! 348 355 "; 356 349 357 350 358 return $nginx_config; … … 365 373 . " }\n" 366 374 . "}\n" 367 . "if (!isset(\$_SERVER['REQUEST_METHOD']) || \$_SERVER['REQUEST_METHOD'] !== 'GET') return;\n\n" 375 . "if (!isset(\$_SERVER['REQUEST_METHOD']) || \$_SERVER['REQUEST_METHOD'] !== 'GET') return;\n" 376 . "if (!empty(\$_GET)) return;\n\n" 368 377 . "\$settings_file = '" . $settings_dir . $settings_filename . "';\n" 369 378 . "\$settings = [];\n" … … 500 509 'cache_dir' => $cache_dir, 501 510 'exclude_uris' => [], 502 'plugin_version' => '1.1. 0'511 'plugin_version' => '1.1.1' 503 512 ]; 504 513 file_put_contents($settings_file, json_encode($settings, JSON_PRETTY_PRINT)); … … 552 561 $settings = json_decode($settings_content, true); 553 562 if (is_array($settings)) { 554 $current_version = '1.1. 0'; // Current plugin version563 $current_version = '1.1.1'; // Current plugin version 555 564 $saved_version = $settings['plugin_version'] ?? '0.0'; 556 565
Note: See TracChangeset
for help on using the changeset viewer.