Commit e5716a10 authored by jan.koester's avatar jan.koester
Browse files

theme: skip SVG icons for LCP detection, fix fetchpriority on actual content image

parent c5f72e00
Loading
Loading
Loading
Loading
+13 −4
Changes for src/theme.cpp: 13 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -515,22 +515,31 @@ void blogi::Theme::printSite(const int tid,libhtmlpp::HtmlString &output,libhtml
                    const std::string &tag = hel->getTagname();

                    if (tag == "img") {
                        bool isBanner = false;
                        bool isIcon = false;
                        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")
                                isBanner = true;
                                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;
                        }
                        if (!isBanner && firstContentImage && !hasFetchpriority) {
                        // 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
                            hel->setAttribute("fetchpriority", "high");
                            firstContentImage = false;
                        } else if (!isBanner && !hasLoading) {
                        } else if (!isIcon && !hasLoading) {
                            hel->setAttribute("loading", "lazy");
                        }
                    }