Commit 1f68a78d authored by jan.koester's avatar jan.koester
Browse files

theme: scope image optimization to #content div only

parent e5716a10
Loading
Loading
Loading
Loading
+32 −25
Changes for src/theme.cpp: 32 added lines, 25 removed lines.
Original line number Diff line number Diff line
@@ -503,49 +503,56 @@ void blogi::Theme::printSite(const int tid,libhtmlpp::HtmlString &output,libhtml
        // Inject theme color CSS variables into <head>
        injectThemeColors(tid, index);

        // Optimize images: first content image gets fetchpriority="high" (LCP candidate),
        // Optimize images inside #content: first gets fetchpriority="high" (LCP candidate),
        // all others get loading="lazy". Add "defer" to external scripts.
        {
            bool firstContentImage = true;

            std::function<void(libhtmlpp::Element*)> optimizeDOM = [&](libhtmlpp::Element *node) {
            // Optimize only images inside the content area
            std::function<void(libhtmlpp::Element*)> optimizeContentImages = [&](libhtmlpp::Element *node) {
                if (!node) return;
                if (node->getType() == libhtmlpp::HtmlEl) {
                    libhtmlpp::HtmlElement *hel = static_cast<libhtmlpp::HtmlElement*>(node);
                    const std::string &tag = hel->getTagname();

                    if (tag == "img") {
                        bool isIcon = false;
                    if (hel->getTagname() == "img") {
                        bool hasLoading = false;
                        bool hasFetchpriority = false;
                        std::string imgSrc;
                        for (auto *attr = hel->firstAttribute(); attr; attr = attr->nextAttribute()) {
                            if (attr->getKey() == "id" && attr->getValue() == "banner")
                                isIcon = true;
                            if (attr->getKey() == "alt" && attr->getValue() == "sysico")
                                isIcon = true;
                            if (attr->getKey() == "src")
                                imgSrc = attr->getValue();
                            if (attr->getKey() == "loading")
                                hasLoading = true;
                            if (attr->getKey() == "fetchpriority")
                                hasFetchpriority = true;
                        }
                        // Skip small icons (SVG, favicon) — not LCP candidates
                        if (imgSrc.size() > 4 && imgSrc.substr(imgSrc.size() - 4) == ".svg")
                            isIcon = true;

                        if (!isIcon && firstContentImage && !hasFetchpriority) {
                            // First content image is the LCP candidate
                            if (attr->getKey() == "loading") hasLoading = true;
                            if (attr->getKey() == "fetchpriority") hasFetchpriority = true;
                        }
                        if (firstContentImage && !hasFetchpriority) {
                            hel->setAttribute("fetchpriority", "high");
                            firstContentImage = false;
                        } else if (!isIcon && !hasLoading) {
                        } else if (!hasLoading) {
                            hel->setAttribute("loading", "lazy");
                        }
                    }
                    for (auto *child = hel->firstChild(); child; child = child->nextElement())
                        optimizeContentImages(child);
                }
            };

            // Walk the full DOM: find #content div, then optimize images only inside it
            std::function<void(libhtmlpp::Element*)> optimizeDOM = [&](libhtmlpp::Element *node) {
                if (!node) return;
                if (node->getType() == libhtmlpp::HtmlEl) {
                    libhtmlpp::HtmlElement *hel = static_cast<libhtmlpp::HtmlElement*>(node);
                    // Check if this is the content container
                    bool isContent = false;
                    if (hel->getTagname() == "div") {
                        for (auto *attr = hel->firstAttribute(); attr; attr = attr->nextAttribute()) {
                            if (attr->getKey() == "id" && attr->getValue() == "content")
                                isContent = true;
                        }
                    }
                    if (isContent) {
                        // Only optimize images inside #content
                        for (auto *child = hel->firstChild(); child; child = child->nextElement())
                            optimizeContentImages(child);
                    } else {
                        for (auto *child = hel->firstChild(); child; child = child->nextElement())
                            optimizeDOM(child);
                    }
                } else if (node->getType() == libhtmlpp::ScriptEL) {
                    // Add defer only to external script elements (with src attribute)
                    libhtmlpp::ScriptElement *sel = static_cast<libhtmlpp::ScriptElement*>(node);