Home / Admin / Safe, Universal Performance Optimization Middleware for NexSouk.com
Duplicate Snippet

Embed Snippet on Your Site

Safe, Universal Performance Optimization Middleware for NexSouk.com

Safe, Universal Performance Optimization Middleware for NexSouk.com

Code Preview
php
<?php
<?php
// Safe, Universal Performance Optimization Middleware for NexSouk.com
// Drop this into a common include file, early in the load chain (e.g., header.php or functions.php)
// Output Buffer Start
ob_start(function ($buffer) {
    // 1. Preload critical font and hero image (if <head> exists)
    $buffer = preg_replace('/<head>/i', "<head>\n" .
        "  <link rel='preload' href='/assets/fonts/your-font.woff2' as='font' type='font/woff2' crossorigin>\n" .
        "  <link rel='preload' href='/assets/images/hero.jpg' as='image'>\n", $buffer);
    // 2. Enforce font-display: swap (safe text rendering)
    $buffer = str_replace("font-display: optional;", "font-display: swap;", $buffer);
    // 3. Add loading="lazy" to <img> tags missing it
    $buffer = preg_replace_callback('/<img\s+[^>]*>/i', function ($match) {
        $tag = $match[0];
        return (stripos($tag, 'loading=') === false) ? preg_replace('/<img/i', '<img loading="lazy"', $tag) : $tag;
    }, $buffer);
    // 4. Add fetchpriority="low" to images in footer (non-blocking for offscreen content)
    $buffer = preg_replace_callback('/<img([^>]+footer[^>]+)>/i', function ($match) {
        return (stripos($match[1], 'fetchpriority=') === false) ? "<img fetchpriority=\"low\"{$match[1]}>" : $match[0];
    }, $buffer);
    // 5. Replace .js with .min.js if file exists
    $buffer = preg_replace_callback('/<script\s[^>]*src="([^"]+)\.js"[^>]*><\/script>/i', function ($match) {
        $minPath = $match[1] . '.min.js';
        $fullPath = $_SERVER['DOCUMENT_ROOT'] . parse_url($minPath, PHP_URL_PATH);
        return file_exists($fullPath) ? str_replace($match[1] . '.js', $minPath, $match[0]) : $match[0];
    }, $buffer);
    // 6. Replace .css with .min.css if file exists
    $buffer = preg_replace_callback('/<link\s[^>]*href="([^"]+)\.css"[^>]*>/i', function ($match) {
        $minPath = $match[1] . '.min.css';
        $fullPath = $_SERVER['DOCUMENT_ROOT'] . parse_url($minPath, PHP_URL_PATH);
        return file_exists($fullPath) ? str_replace($match[1] . '.css', $minPath, $match[0]) : $match[0];
    }, $buffer);
    // 7. Convert non-critical CSS to async (using preload + onload)
    $buffer = preg_replace_callback('/<link\s+rel="stylesheet"\s+href="([^"]+)"[^>]*>/i', function ($match) {
        $href = $match[1];
        return (stripos($href, 'critical') === false)
            ? "<link rel=\"preload\" href=\"$href\" as=\"style\" onload=\"this.onload=null;this.rel='stylesheet'\">"
            : $match[0];
    }, $buffer);
    // 8. WebP support: wrap .jpg/.png in <picture> if .webp version exists
    $buffer = preg_replace_callback('/<img\s[^>]*src="([^"]+\.(jpg|jpeg|png))"[^>]*>/i', function ($match) {
        $originalTag = $match[0];
        $src = $match[1];
        $webp = preg_replace('/\.(jpg|jpeg|png)$/i', '.webp', $src);
        $webpPath = $_SERVER['DOCUMENT_ROOT'] . parse_url($webp, PHP_URL_PATH);
        return file_exists($webpPath)
            ? "<picture><source srcset=\"$webp\" type=\"image/webp\">$originalTag</picture>"
            : $originalTag;
    }, $buffer);
    return $buffer;
});
// 9. Set cache-control headers (long-lived assets)
if (!headers_sent()) {
    header("Cache-Control: public, max-age=31536000, immutable");
    header("Vary: Accept-Encoding, User-Agent");
}
?>

Comments

Add a Comment